Provides a set of opinionated helpers and configurations for setting up structured logging with Serilog in .NET applications. This package simplifies the integration of common sinks (Console, File), enrichers (Exceptions, Sensitive Data), and performance features like asynchronous logging, enabling a robust and consistent logging strategy out of the box.
$ dotnet add package RA.Utilities.Logging.CoreRA.Utilities.Logging.Core provides a set of opinionated helpers and configurations for setting up structured logging with Serilog in .NET applications. This package simplifies the integration of common sinks (Console, File), enrichers (Exceptions, Sensitive Data), and performance features like asynchronous logging, enabling a robust and consistent logging strategy out of the box.
Setting up a comprehensive logging solution from scratch can be repetitive. This package abstracts away the boilerplate configuration for Serilog by providing a single extension method, AddRaSerilog, that configures a production-ready logger with sensible defaults.
Key Features Configured by Default:
Serilog.Sinks.Async to minimize the performance impact of logging on your application's request thread.FromLogContext: Adds contextual information to logs.WithExceptionDetails: Destructures exceptions to include detailed information like the stack trace.WithSensitiveDataMasking: Automatically finds and masks sensitive data in log messages based on property names (e.g., "Password", "CreditCard").appsettings.json, allowing you to easily adjust log levels and other parameters without changing code.You can install the package via the .NET CLI:
dotnet add package RA.Utilities.Logging.Core
Or through the NuGet Package Manager in Visual Studio.
Integrating the logger into your ASP.NET Core application is a two-step process.
appsettings.jsonAdd a Serilog section to your appsettings.json file. The AddRaSerilog method will automatically read from this section. You can override log levels for different sources as needed.
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"AllowedHosts": "*"
}
Program.csCall the AddRaSerilog() extension method on your WebApplicationBuilder. This should be one of the first things you do to ensure all application startup events are logged.
using RA.Utilities.Logging.Core.Extensions; // Add this using statement
var builder = WebApplication.CreateBuilder(args);
// Add the RA Serilog configuration
builder.AddRaSerilog();
try
{
// Add other services
builder.Services.AddControllers();
var app = builder.Build();
// Your middleware pipeline
app.UseHttpsRedirection();
app.MapControllers();
Log.Information("Starting application...");
app.Run();
}
catch (Exception ex)
{
// Log fatal exceptions that prevent the app from starting
Log.Fatal(ex, "Application failed to start.");
}
finally
{
// Ensure all buffered logs are written to sinks before the app closes
Log.CloseAndFlush();
}
That's it! Your application is now configured with structured, asynchronous, and enriched logging. You can inject ILogger<T> anywhere in your application via dependency injection to write logs.
Contributions are welcome! If you have a suggestion or find a bug, please open an issue to discuss it. Please follow the contribution guidelines outlined in the other RA.Utilities packages.