MillerByte.Logging.Api is a production-ready API logging package for .NET 8 applications. It provides automatic request/response logging, session tracking, and MongoDB storage with built-in resilience patterns.
Installation
Install the package via NuGet:
dotnet add package MillerByte.Logging.Api
Prerequisites
- .NET 8.0 or later
- MongoDB 4.4+ (for data storage)
- ASP.NET Core Web API project
Basic Setup
Add the logging services to your Program.cs:
using MillerByte.Logging.Api; var builder = WebApplication.CreateBuilder(args); // Add API logging with configuration binding builder.Services.AddApiLogging(builder.Configuration); builder.Services.AddControllers(); var app = builder.Build(); // CRITICAL: Middleware order matters! app.UseApiLoggingRequestBuffering(); // NEW v1.2.0 - Must be BEFORE UseRouting() app.UseApiLoggingResponseCapture(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();
Important (v1.2.0+): The
UseApiLoggingRequestBuffering() middleware must be called before UseRouting() to properly capture request bodies. Without this, request body logging may not work correctly.Configuration
Add the required settings to your appsettings.json:
{
"ApiLogging": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "ApiLogs",
"SessionsCollectionName": "LoginSessions",
"ActionsCollectionName": "ApiActions"
}
}Alternatively, use the connection string format:
{
"ConnectionStrings": {
"ApiLogging": "mongodb://localhost:27017"
},
"ApiLogging": {
"DatabaseName": "ApiLogs"
}
}What Gets Logged
By default, the package logs:
- Request method, path, and query string
- Request and response bodies (configurable)
- Request headers (sensitive data filtered)
- Response status codes and timing
- User session information
- Exception details when errors occur
Next Steps
- Configure advanced options like sampling rate and data capture
- Learn about the ApiLogging attribute for automatic logging
- Explore session strategies for user tracking
- Set up resilience and OpenTelemetry for production
- Implement GDPR compliance features
