The Request Logging Middleware is a custom ASP.NET Core middleware designed to log detailed information about incoming HTTP requests and their corresponding responses. It provides functionality to capture various metrics, including CPU and memory usage, response times, and request details, enhancing monitoring and troubleshooting capabilities in your application.
- Request and Response Logging: Logs the HTTP method, path, timestamp, and response status, along with request and response bodies (configurable).
- Performance Metrics: Tracks CPU usage, memory consumption, and request duration for each processed request.
- Error Handling: Captures and logs detailed error information if exceptions occur during request processing.
- Configurable Options: Easily configurable options to choose what data to log (request body, headers, response, etc.).
- Database Integration: Utilizes a database context to persist log entries for further analysis.
insert the following configuration into appSettings.json
file:
{
"HTTPLogConnection": {
"DatabaseProvider": "", // SQL or PostgreSQL, the default is SQL
"DefaultConnection": ""
}
}
To use this middleware, configure it in your ASP.NET Core application’s startup/program.cs:
services.ConfigureDMiddlewareServices(options =>
{
options.SaveRequestBody = true;
options.SaveRequesterInfo = true;
options.SaveRequestHeader = true;
options.SaveResponse = true;
options.LogTheLoggDashboard = false;
});
Then add the middleware to the request pipeline:
app.UseRequestLogging();
To access the logs dashboard, you can map the dashboard route by adding the following to your Configure method in Startup.cs or Program.cs:
app.MapDLoggerDashboard();
Once the dashboard is configured, you can access it via the following URL:
yourURL/DLogger/index.html
This middleware helps developers monitor application behavior and diagnose issues effectively, making it an essential tool for robust application logging.