Skip to content

mthamil/AspNetCore.Serilog.RequestLoggingMiddleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET Core Serilog Request Logging Middleware

Middleware for AspNet Core that uses Serilog to log requests. This project was inspired directly by the following post https://blog.getseq.net/smart-logging-middleware-for-asp-net-core/.

Build status

Download

Visit NuGet to download.

Usage

To use, when configuring the IApplicationBuilder, such as in the Configure method of Startup.cs, add the following:

    using AspNetCore.Serilog.RequestLoggingMiddleware;
    ...
    public void Configure(IApplicationBuilder app)
    {
        ...
        app.UseSerilogRequestLogging();
        ...
    }

or, when configuring the IWebHostBuilder in Program.cs, add the following:

    using AspNetCore.Serilog.RequestLoggingMiddleware;
    ...
    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                   .UseSerilogRequestLogging()
    ...
    

Options

For both methods, an action that configures an instance of RequestLoggingOptions can be provided like so:

    app.UseSerilogRequestLogging(options => // Configure options here.

Currently there is only one option, RequestProjection, a delegate that defines a mapping determining which properties of an HttpRequest should be logged. By default, the following properties are logged:

  • ContentLength
  • ContentType
  • Protocol
  • Host
  • IsAuthenticated

This can be customized using the aforementioned options, such as in the following example which logs only the properties IsHttps and QueryString:

    app.UseSerilogRequestLogging(options => 
        options.RequestProjection = 
            r => new { r.IsHttps, QueryString = r.QueryString.Value });

An anonymous object or a named class instance can be provided specifying the properties to log. Note: the object provided will be logged using Serilog object destructuring.

About

Middleware for ASP.NET Core that uses Serilog to log requests.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages