SimpleRequestLogger 1.1.4
See the version list below for details.
dotnet add package SimpleRequestLogger --version 1.1.4
NuGet\Install-Package SimpleRequestLogger -Version 1.1.4
<PackageReference Include="SimpleRequestLogger" Version="1.1.4" />
paket add SimpleRequestLogger --version 1.1.4
#r "nuget: SimpleRequestLogger, 1.1.4"
// Install SimpleRequestLogger as a Cake Addin #addin nuget:?package=SimpleRequestLogger&version=1.1.4 // Install SimpleRequestLogger as a Cake Tool #tool nuget:?package=SimpleRequestLogger&version=1.1.4
SimpleRequestLogger
This package provides 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 INFO] GET / responded 200 in 31 ms.
// JSON
{
"Time": "21:51:46.5705",
"Level": "INFO",
"Message": "GET \/ responded 200 in 31 ms.",
"Properties": {
"Method": "GET",
"Path": "\/",
"QueryString": "",
"StatusCode": 200,
"ElapsedMs": 31
}
}
Installation
You can install the NuGet package via the NuGet Package Manager inside Visual Studio or via the console:
// Package Manager Console
Install-Package SimpleRequestLogger
// .NET Core CLI
dotnet add package SimpleRequestLogger
Usage
The only thing you should do is simply add the middleware to the request pipeline.
Default configuration
By default, SimpleRequestLogger
logs all requests at information log level with message template "{Method} {Path}{QueryString} responded {StatusCode} in {ElapsedMs} ms."
. To use the middleware you should only add a single line:
app.UseSimpleRequestLogging();
Custom configuration
It is possible to customize the message template, to change the log level based on status code and to disable logging for specific paths.
app.UseSimpleRequestLogging(config =>
{
config.MessageTemplate = "{Scheme} {Method} {Path} => {StatusCode}";
config.LogLevelSelector = (statusCode) =>
(statusCode < 400) ? LogLevel.Information : LogLevel.Error;
config.IgnorePath("/health");
config.IgnorePath("/static/*");
});
Properties
- Method
- Path
- QueryString
- Protocol
- Scheme
- UserAgent
- StatusCode
- ElapsedMs
Pipeline placement
You might want to consider placing SimpleRequestLogger
after request-heavy middlewares 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.
Exceptions
In normal circumstances, SimpleRequestLogger
should not throw exceptions when handling requests.
On startup, when the middleware is instantiated, the configuration is verified. MessageTemplate
and the ignored paths are checked for validity. Additionally, it is also ensured that LogLevelSelector
delegate will not throw for the standard response status codes. In case of a problem with the configuration, an InvalidOperationException
is thrown.
Performance
SimpleRequestLogger
adds a negligible performance overhead to every request.
Benchmarks
The scenarios are run on a test host without other middleware.
Method | Mean | Error | StdDev | Gen 0 | Allocated |
---|---|---|---|---|---|
NoSimpleRequestLogger | 12.01 μs | 0.197 μs | 0.175 μs | 2.3804 | 7 KB |
DefaultConfig | 15.93 μs | 0.317 μs | 0.530 μs | 2.5024 | 8 KB |
CustomConfigOneIgnoredPath | 16.46 μs | 0.309 μs | 0.624 μs | 2.5024 | 8 KB |
CustomConfigFiveIgnoredPaths | 20.09 μs | 0.389 μs | 0.558 μs | 2.5330 | 8 KB |
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
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 | 550 | 4/14/2023 |
3.2.2 | 453 | 2/16/2023 |
3.2.1 | 234 | 2/14/2023 |
3.2.0 | 299 | 2/6/2023 |
3.1.1 | 488 | 1/2/2023 |
3.1.0 | 364 | 11/26/2022 |
3.0.0 | 339 | 11/18/2022 |
2.0.3 | 442 | 9/25/2022 |
2.0.2 | 428 | 8/27/2022 |
2.0.1 | 580 | 4/19/2022 |
2.0.0 | 413 | 4/7/2022 |
1.1.4 | 426 | 3/13/2022 |
1.1.3 | 411 | 3/8/2022 |
1.1.2 | 412 | 2/24/2022 |
1.1.1 | 411 | 2/23/2022 |
1.1.0 | 407 | 2/22/2022 |
1.0.0 | 242 | 1/12/2022 |