Crip.AspNetCore.Logging 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Crip.AspNetCore.Logging --version 1.0.4                
NuGet\Install-Package Crip.AspNetCore.Logging -Version 1.0.4                
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="Crip.AspNetCore.Logging" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Crip.AspNetCore.Logging --version 1.0.4                
#r "nuget: Crip.AspNetCore.Logging, 1.0.4"                
#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 Crip.AspNetCore.Logging as a Cake Addin
#addin nuget:?package=Crip.AspNetCore.Logging&version=1.0.4

// Install Crip.AspNetCore.Logging as a Cake Tool
#tool nuget:?package=Crip.AspNetCore.Logging&version=1.0.4                

Crip.AspNetCore.Logging

issues forks stars license

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:

Verbose log 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

Verbose log output

  • Debug
    • Writes log message with incoming request headers
    • Writes log message with returned response headers
    • Writes basic response timing/status message

Debug log output

  • Information
    • Writes basic response timing/status message

Information log output

  • 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 *****.

Authorization header value is hidden

You can add CookieHeaderLoggingMiddleware to avoid cookie value write to logs:

services
    .AddRequestLogging()
    .AddRequestLoggingCookieValueMiddleware();

Cookie header value is hidden

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 246 7/4/2023
2.0.0-rc.11 91 7/3/2023
2.0.0-rc.10 85 7/3/2023
2.0.0-rc.7 125 7/3/2023
2.0.0-rc.6 90 7/2/2023
2.0.0-rc.2 88 7/2/2023
1.1.3 428 8/26/2022
1.1.2 412 8/26/2022
1.1.0 396 8/26/2022
1.0.4 406 8/25/2022
1.0.2 431 12/1/2020
1.0.2-rc.13 213 12/1/2020
1.0.2-beta.12 210 12/1/2020
1.0.2-alpha.11 211 12/1/2020
1.0.1.5 433 12/1/2020
1.0.0.22 398 11/28/2020
1.0.0.21 390 11/28/2020
1.0.0 399 8/25/2022