SimpleRequestLogger 3.2.3

dotnet add package SimpleRequestLogger --version 3.2.3                
NuGet\Install-Package SimpleRequestLogger -Version 3.2.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleRequestLogger" Version="3.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleRequestLogger --version 3.2.3                
#r "nuget: SimpleRequestLogger, 3.2.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleRequestLogger as a Cake Addin
#addin nuget:?package=SimpleRequestLogger&version=3.2.3

// Install SimpleRequestLogger as a Cake Tool
#tool nuget:?package=SimpleRequestLogger&version=3.2.3                

SimpleRequestLogger

nuget downloads cd codeql loc test coverage mutation score

SimpleRequestLogger is a small and customizable ASP.NET Core middleware for structured logging of requests using Microsoft.Extensions.Logging. The built-in request logging is a bit noisy and emits multiple events per request. With SimpleRequestLogger you can fit all the information you need in a single log entry:

// Plaintext
21:51:46.5705 [INF] GET / responded 200 in 31 ms.

// JSON
{
    "Time": "21:51:46.5705",
    "Level": "INF",
    "Message": "GET / responded 200 in 31 ms.",
    "Properties": {
        "Method": "GET",
        "Path": "/",
        "QueryString": "",
        "StatusCode": 200,
        "ElapsedMs": 31
    }
}

Getting started

Install the NuGet package and add the middleware at the beginning of your request pipeline:

app.UseRequestLogging();

Configuration

By default, SimpleRequestLogger logs all requests at information log level with message template "{Method} {Path}{QueryString} responded {StatusCode} in {ElapsedMs} ms.".

It is possible to customize the message template, to change the log level based on status code and to disable logging for specific paths. SimpleRequestLogger uses Microsoft.Extensions.Configuration and would by default expect a section RequestLogging.

"RequestLogging": {
    "MessageTemplate": "{Scheme} {Method} {Path} => {StatusCode}",
    "IgnorePaths": [ "/health", "/static/*" ]
}

It is also possible to pass a custom configuration section:

app.UseRequestLogging("YourCustomSection:CustomSubsectionRequestLogging");

To change the log level based on status code, you should pass a delegate to the middleware:

app.UseRequestLogging(statusCode => (statusCode < 400) ? LogLevel.Information : LogLevel.Error);

You might as well have both custom configuration section and a log level selector.

app.UseRequestLogging("YourCustomSection:CustomSubsectionRequestLoging",
    statusCode => (statusCode < 400) ? LogLevel.Information : LogLevel.Error);

Properties

  • Method
  • Path
  • QueryString
  • Header* - A Pascal case field name will be transformed to Kebab case. Example: HeaderFooBar ⇒ foo-bar
  • Protocol
  • Scheme
  • RemoteIpAddress - If you log this property you might want to consider adding UseForwardedHeaders() to your pipeline.
  • Claim* - A Pascal case claim type will be transformed to Kebab case. Example: ClaimFooBar ⇒ foo-bar
  • StatusCode
  • ElapsedMs

Pipeline placement

You might want to consider placing SimpleRequestLogger after request-heavy middleware like UseStaticFiles() if those requests are not interesting for you. Alternatively, you might ignore those via the configuration.

If SimpleRequestLogger catches an exception, the request will be logged with a status code 500 and the exception will be rethrown. If you have an error handling middleware that alters the response status code based on exception type, you should consider adding SimpleRequestLogger before it.

Self-checks

On startup, when the middleware is instantiated, the configuration is verified. MessageTemplate and IgnorePaths are checked for validity. Additionally, it is also ensured that the log level selector delegate will not throw for the standard response status codes. In case of a problem with the configuration, an InvalidOperationException is thrown.

Additional resources

Help and support

For bug reports and feature requests, please use the issue tracker. For questions, ideas and other topics, please check the discussions.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.2.3 557 4/14/2023
3.2.2 454 2/16/2023
3.2.1 236 2/14/2023
3.2.0 301 2/6/2023
3.1.1 490 1/2/2023
3.1.0 366 11/26/2022
3.0.0 343 11/18/2022
2.0.3 444 9/25/2022
2.0.2 430 8/27/2022
2.0.1 582 4/19/2022
2.0.0 417 4/7/2022
1.1.4 428 3/13/2022
1.1.3 413 3/8/2022
1.1.2 415 2/24/2022
1.1.1 413 2/23/2022
1.1.0 409 2/22/2022
1.0.0 243 1/12/2022