Logging/Clean arch Flashcards
Logging
Logging is the process of recording run-time actions as they happen in real-time.
Helps us to understand the failures and performance bottlenecks of the application.
ILogger levels
Debug
ILogger.LogDebug(“log_message”);
Logs that provide details & values of variables for debugging purpose.
Information
ILogger.LogInformation(“log_message”);
Logs that track the general flow of the application execution.
Warning
ILogger.LogWarning(“log_message”);
Logs that highlight an abnormal or unexpected event.
Error
ILogger.LogError(“log_message”);
Logs to indicate that flow of execution is stopped due to a failure.
Critical
ILogger.LogCritical(“log_message”)
Logs to indicate an unrecoverable application crash.
Serilog
Serilog is a structured logging library for Asp.Net Core.
Supports variety of logging destinations, referred as “Sinks” - starts with Console, Azure, DataDog, ElasticSearch, Amazon CloudWatch, Email and Seq.
Serilog - File Sink
The “Serilog.Sinks.File” logs into a specified file.
You can configure the filename, rolling interval, file size limit etc., using configuration settings.
Serilog - Seq Sink
The “Serilog.Sinks.Seq” is a real-time search and analysis server for structured application log data.
Seq server can run on Windows, Linux or Docker.
Serilog - RequestId
“RequestId” is the unique number (guid) of each individual requests, used to identify to which request the log belongs to.
RequestId is “TraceIdentifier” internally, that is generated by Asp.Net Core.
Serilog - Enrichers
Enrichers are additional details that are added to LogContext; so they’re stored in logs.
Eg: MachineName[or]Custom Properties.
Filters
Filters are the code blocks that execute before / after specific stages in “Filter Pipeline”.
Filters perform specific tasks such as authorization, caching, exeption handling etc.
Overview of Types of Filters
Authorization Filter
Determines whether the user is authorized to access the action method.
Resource Filter
Invoking custom model binder explicitly
Caching the response.
Action Filter
Manipulating & validating the action method parameters.
Manipulating the ViewData.
Overriding the IActionResult provided by action method.
Exception Filter
Handling unhandled exception that occur in model binding, action filters or action methods.
Result Filter
Preventing IActionResult from execution.
Adding last-moment changes to response (such as adding response headers).
Action Filter
When it runs
Runs immediately before and after an action method executes
‘OnActionExecuting’ method
It can access the action method parameters, read them & do necessary manipulations on them.
It can validate action method parameters.
It can short-circuit the action (prevent action method from execution) and return a different IActionResult.
‘OnActionExecuted’ method
It can manipulate the ViewData.
It can change the result returned from the action method.
What are global filters?
Global filters are applied to all action methods of all controllers in the project.
Filters [vs] Middleware
Middleware
Middleware pipeline is a superset of Filter pipeline, which contains the full-set of all middlewares added to the ApplicationBuilder in the application’s startup code (Program.cs).
Middleware pipeline execute for all requests.
Middleware handles application-level functionality such as Logging, HTTPS redirection, Performance profiling, Exception handling, Static files, Authentication etc., by accessing low-level abstractions such as HttpContext.
Filter
Filter pipeline is a subset of Middleware pipeline which executes under “EndPoint Middleware”.
In addition, filter pipeline executes for requests that reach “EndPoint Middleware”.
Filters handle MVC-specific functionality such as manipulating or accessing ViewData, ViewBag, ModleState, Action result, Action parameters etc.
Exception Handling Middleware
Handles all errors occurred in filter pipeline (including model binding, controllers and filters).
Should be added to the application pipeline, before RoutingMiddleware.
Custom Exceptions
A custom exception class is an exception class that inherits from System.Exception class & represents a domain-specific excpetion
Used to represent the domain-specific errors stand-out of system-related (.NET) related exceptions.
UseExceptionHandler()
The built-in UseExceptionHandler() middleware redirects to the specified route path, when an unhandled exception occurs during the application execution.
Can be used as an alternative to custom exception handling middleware.
Catches and logs unhandled exceptions.
Re-executes the request in an alternative pipeline using the specified route path.