A .NET 6 Request and Response logging library. It uses Serilog library and implements a middleware to be used with .NET Core.
DotLog is installed from NuGet.
Install-Package DotLogNet
The simplest way to set up DotLog is configure the extension method in your application's Program.cs.
using DotLog;
public class Program
{
public stati void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
.UseDotLog();
}
By default the log level is set to "Information" and the only sink configured is the Console sink. This is because the library is designed to be used in cloud environments, like Kubernetes, in combination with FluentBit and Fluentd.
Then, the next step is to configure the logging middleware in your application's Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseLoggingMiddleware();
}
There is also an override of the UseLogging Middleware() method that allows you not to log the request and response of certain routes.To do this it is necessary to specify the routes to be excluded as middleware options.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
var routesToBeExcluded = new List<string>() { "/health" };
app.UseLoggingMiddleware(new LoggerMiddleware() { RoutesToBeExcluded = routesToBeExcluded });
}
Each request and response log will have the following JSON format:
{
"Level": "Information",
"Time": "1611932812",
"CorrelationId": "29494797-d298-4d33-8229-35a6c28b8948",
"Message": "My log message",
"Exception": null,
"Http": {
"Request": {
"Path": "/api/v1/customers/1",
"Method": "GET",
"Query": null,
"Body": null,
"ContentType": "application/json; charset=utf-8",
"Scheme": "http",
"Protocol": "HTTP/1.1",
"UserAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
},
"Response": {
"StatusCode": 200,
"ResponseTime": 0.017,
"Bytes": null
}
},
"Host": {
"Hostname": "localhost",
"ForwardedHostname": "id42.example-cdn.com",
"Ip": "127.0.0.1"
}
}
In addition, the middleware is responsible for recovering the CorrelationId from the HTTP Request Header to use it for each log entry, as follows:
{
"Level": "Information",
"Time": "1611932812",
"CorrelationId": "29494797-d298-4d33-8229-35a6c28b8948",
"Message": "My other log message",
"Exception": null
}
This project use SemVer for versioning. For availabe version, see the tags on this repository.
This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details