autologger 1.1.22

dotnet add package autologger --version 1.1.22
                    
NuGet\Install-Package autologger -Version 1.1.22
                    
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="autologger" Version="1.1.22" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="autologger" Version="1.1.22" />
                    
Directory.Packages.props
<PackageReference Include="autologger" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add autologger --version 1.1.22
                    
#r "nuget: autologger, 1.1.22"
                    
#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.
#:package autologger@1.1.22
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=autologger&version=1.1.22
                    
Install as a Cake Addin
#tool nuget:?package=autologger&version=1.1.22
                    
Install as a Cake Tool

AutoLogger

A flexible and customizable logging middleware for ASP.NET Core applications. This NuGet package provides a robust system to log HTTP requests and responses with configurable granularity, using attributes to control logging behavior at the controller or action level.

Features

  • Configurable Log Levels: Choose from multiple logging levels (None, Basic, Headers, Query, Body, Full, All, Custom) to control the amount of information logged.
  • Attribute-Based Configuration: Use the MustLog attribute to specify logging behavior for controllers or actions.
  • Custom Logging: Log specific headers or request/response bodies with the Custom log level.
  • Seamless Integration: Integrates with ASP.NET Core’s middleware pipeline and logging infrastructure.
  • Performance Optimized: Efficient handling of request/response streams to minimize overhead.
  • Security Considerations: Supports selective logging to avoid capturing sensitive data.

Installation

Prerequisites

  • .NET 9
  • ASP.NET Core application with dependency injection configured
  • A logging provider (e.g., Microsoft.Extensions.Logging.Console)

Install via NuGet

Install the AutoLogger package using the NuGet Package Manager or the .NET CLI:

dotnet add package AutoLogger

Or, via the Package Manager Console:

Install-Package AutoLogger

Getting Started

Step 1: Register the Middleware

In your Program.cs, register the LoggingMiddleware in the ASP.NET Core pipeline.

using AutoLogger;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddLogging(logging => logging.AddConsole());

var app = builder.Build();
app.UseMiddleware<LoggingMiddleware>();
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
app.Run();

Step 2: Apply the MustLog Attribute

Use the MustLog attribute on controllers or action methods to enable logging with the desired log level.

using AutoLogger;

// Basic logging: Logs HTTP method, URL, and status code
[MustLog(LogLevelOption.Basic)]
public class BasicController : ControllerBase
{
    [HttpGet("basic")]
    public IActionResult GetBasic() => Ok("Basic response");
}

// Custom logging: Logs specific headers, body and query
[MustLog(LogLevelOption.Custom, new[] { "Authorization", "Content-Type" }, logBody: true, logQuery: true)]
public class CustomController : ControllerBase
{
    [HttpPost("custom")]
    public IActionResult PostCustom([—fromBody] string data) => Ok("Custom response");
}

Step 3: Configure Logging

Ensure your appsettings.json enables the Information log level for the middleware.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "AspNetCore.Logging.LoggingMiddleware": "Information"
    }
  }
}

Log Levels

The package supports the following log levels, defined in the LogLevelOption enum:

Log Level Description Request Logging Response Logging
None No logging None None
Basic Minimal info Method, URL Status code
Headers Headers only Headers Headers
Query Query params Query parameters None
Body Body only Body Body
Full Detailed info Method, URL, headers, body Status code, headers, body
All Comprehensive Method, URL, query params, body Status code, body
Custom Selective logging Specific headers/body (configurable) Specific headers/body (configurable)

Usage Examples

Example 1: Basic Logging

Log only the HTTP method, URL, and status code.

[MustLog(LogLevelOption.Basic)]
public class BasicController : ControllerBase
{
    [HttpGet("basic")]
    public IActionResult GetBasic() => Ok("Basic response");
}

Log Output:

INFO: Request: GET /basic
INFO: Response: 200

Example 2: Body Logging

Log only the request and response bodies.

[MustLog(LogLevelOption.Body)]
public class BodyController : ControllerBase
{
    [HttpPost("body")]
    public IActionResult PostBody([FromBody] string data) => Ok("Body response");
}

Log Output:

INFO: Request Body: {"data":"example"}
INFO: Response Body: Body response

Example 3: Custom Logging

Log specific headers and the request/response body.

[MustLog(LogLevelOption.Custom, new[] { "Authorization", "Content-Type" }, logBody: true)]
public class CustomController : ControllerBase
{
    [HttpPost("custom")]
    public IActionResult PostCustom([FromBody] string data) => Ok("Custom response");
}

Log Output:

INFO: Request Headers: Authorization: Bearer token, Content-Type: application/json
INFO: Request Body: {"data":"example"}
INFO: Response Headers: Content-Type: text/plain; charset=utf-8
INFO: Response Body: Custom response

Configuration

Logging Provider

The middleware uses ILogger<LoggingMiddleware>. Configure your preferred logging provider (e.g., Console, Serilog, NLog) in the service configuration.

Security Considerations

  • Sensitive Data: Avoid logging sensitive headers (e.g., Authorization) or bodies containing personal data. Use the Custom log level to selectively log non-sensitive information.
  • Redaction: Consider implementing custom redaction logic for sensitive fields if needed.

Performance Tuning

  • Body Logging: Logging large request/response bodies can increase memory usage. Limit body logging to specific endpoints or use sampling.
  • Buffering: The middleware enables request buffering for body logging, which may impact performance for large payloads.

Troubleshooting

  • No Logs: Verify the MustLog attribute is applied and the middleware is registered. Ensure the logger’s log level is Information or lower.
  • Body Not Logged: Confirm the request/response content type is supported (e.g., JSON, text) and logBody is true for Custom or Body levels.
  • Stream Issues: Ensure streams are properly reset after logging to avoid pipeline errors.

Changelog

Version 1.1.0

  • Added: Body log level to log only request/response bodies.
  • Added: logQuery parameter to log queries if custom
  • Fixed: Bug in body reading that caused unexpected errors.
  • Changed: LogLevelOption.All now ignores headers instead of query parameters, logging method, URL, query params, and bodies.

See the full Changelog for details.

License

This package is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.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
1.1.22 166 5/18/2025
1.1.4 154 5/18/2025
1.1.3 158 5/18/2025
1.1.1 159 5/18/2025
1.1.0 246 5/13/2025
1.0.0 152 5/11/2025