Crip.AspNetCore.Logging
1.0.0
See the version list below for details.
dotnet add package Crip.AspNetCore.Logging --version 1.0.0
NuGet\Install-Package Crip.AspNetCore.Logging -Version 1.0.0
<PackageReference Include="Crip.AspNetCore.Logging" Version="1.0.0" />
paket add Crip.AspNetCore.Logging --version 1.0.0
#r "nuget: Crip.AspNetCore.Logging, 1.0.0"
// Install Crip.AspNetCore.Logging as a Cake Addin #addin nuget:?package=Crip.AspNetCore.Logging&version=1.0.0 // Install Crip.AspNetCore.Logging as a Cake Tool #tool nuget:?package=Crip.AspNetCore.Logging&version=1.0.0
Crip.AspNetCore.Logging
Make HTTP request logging ease.
Setup request/response logging in application
Configure log level for a service:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Crip.AspNetCore.Logging.RequestLoggingMiddleware": "Trace"
}
}
}
Configure dependency injection in Startup.ConfigureServices
:
services.AddRequestLogging();
Add logging middleware in Startup.Configure
:
app.UseRouting();
// After routing
app.UseRequestLoggingMiddleware();
// And before endpoints
app.UseEndpoints(endpoints => ... );
And now you are ready to see all request/response in logging output:
Setup HTTP client request/response logging
Configure log level for a service:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Crip.AspNetCore.Logging.LoggingHandler": "Trace"
}
}
}
Configure dependency injection in Startup.ConfigureServices
:
services.AddRequestLogging();
Register HTTP client with message handler:
services
.AddRequestLoggingHandler()
.AddHttpClient<MyHttpClient>()
.AddHttpMessageHandler<LoggingHandler<MyHttpClient>>();
// Or use predefined extension method:
services.AddLoggableHttpClient<MyHttpClient>();
Configuration options
Change verbosity level to reduce logs
With different verbosity level, different output will be written to logs
Trace
- Writes log message with incoming request headers and body
- Writes log message with returned response headers and body
- Writes basic response timing/status message
Debug
- Writes log message with incoming request headers
- Writes log message with returned response headers
- Writes basic response timing/status message
Information
- Writes basic response timing/status message
- Any other level will not write logs at all.
Configure verbosity for controller
Each controller has its own source context. This allows configure specific verbosity for a controller. If controller is
named OrdersController
, you can set verbosity for it in configuration:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Crip.AspNetCore.Logging.RequestLoggingMiddleware": "None",
"Crip.AspNetCore.Logging.RequestLoggingMiddleware.Orders": "Trace"
}
}
}
In this case only OrdersController
requests/responses will be written to logs.
Configure verbosity for HTTP client
When you register client message handler with type .AddHttpMessageHandler<LoggingHandler<MyHttpClient>>()
, this value
is used as source context postfix.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Crip.AspNetCore.Logging.LoggingHandler": "None",
"Crip.AspNetCore.Logging.LoggingHandler.MyHttpClient": "Trace"
}
}
}
In this case only MyHttpClient
requests/responses will be written to logs.
Filter endpoints
If there is some endpoints you would like to exclude from logs, you can configure predicate:
services.AddRequestLoggingExclude("/images*", "/swagger*")
Or if you like to include only API requests in logging:
services.AddSingleton<IHttpRequestPredicate>(provider =>
new EndpointPredicate(false, "/api*"));
Or create your own IHttpRequestPredicate
implementation and add it to service collection.
Filter logged content
By default AuthorizationHeaderLoggingMiddleware
and LongJsonContentMiddleware
are added in to logger. You can create
own implementations of the IHeaderLogMiddleware
or IRequestContentLogMiddleware
to modify logged content for your
own needs.
IHeaderLogMiddleware
AuthorizationHeaderLoggingMiddleware
implements IHeaderLogMiddleware
interface and will hide Authorization header
values replacing Basic
auth header value with Basic *****
and Bearer
auth header value with Bearer *****
.
You can add CookieHeaderLoggingMiddleware
to avoid cookie value write to logs:
services
.AddRequestLogging()
.AddRequestLoggingCookieValueMiddleware();
IRequestContentLogMiddleware
LongJsonContentMiddleware
implements IRequestContentLogMiddleware
interface and will hide properties value if its
length exceeds 500 characters and will output only first 10 symbols:
{
"fileBase64": "SGVsbG8gV2***"
}
This middleware runs only if request content type is application/json
. You can change this middleware values within
configuration file:
{
"Logging": {
"Request": {
"MaxCharCountInField": 1000,
"LeaveOnTrimCharCountInField": 3
}
}
}
For more technical details take a look in example project Startup file.
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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Http (>= 2.2.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Crip.AspNetCore.Logging:
Package | Downloads |
---|---|
Crip.AspNetCore.Logging.LongJsonContent
AspNetCore request logging middleware. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 247 | 7/4/2023 |
2.0.0-rc.11 | 92 | 7/3/2023 |
2.0.0-rc.10 | 86 | 7/3/2023 |
2.0.0-rc.7 | 126 | 7/3/2023 |
2.0.0-rc.6 | 91 | 7/2/2023 |
2.0.0-rc.2 | 89 | 7/2/2023 |
1.1.3 | 429 | 8/26/2022 |
1.1.2 | 413 | 8/26/2022 |
1.1.0 | 397 | 8/26/2022 |
1.0.4 | 407 | 8/25/2022 |
1.0.2 | 432 | 12/1/2020 |
1.0.2-rc.13 | 214 | 12/1/2020 |
1.0.2-beta.12 | 211 | 12/1/2020 |
1.0.2-alpha.11 | 212 | 12/1/2020 |
1.0.1.5 | 434 | 12/1/2020 |
1.0.0.22 | 399 | 11/28/2020 |
1.0.0.21 | 391 | 11/28/2020 |
1.0.0 | 400 | 8/25/2022 |