RA.Utilities.Logging.Core 10.1.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package RA.Utilities.Logging.Core --version 10.1.0
                    
NuGet\Install-Package RA.Utilities.Logging.Core -Version 10.1.0
                    
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="RA.Utilities.Logging.Core" Version="10.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RA.Utilities.Logging.Core" Version="10.1.0" />
                    
Directory.Packages.props
<PackageReference Include="RA.Utilities.Logging.Core" />
                    
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 RA.Utilities.Logging.Core --version 10.1.0
                    
#r "nuget: RA.Utilities.Logging.Core, 10.1.0"
                    
#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 RA.Utilities.Logging.Core@10.1.0
                    
#: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=RA.Utilities.Logging.Core&version=10.1.0
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Logging.Core&version=10.1.0
                    
Install as a Cake Tool

RA.Utilities.Logging.Core

NuGet version NuGet Downloads Codecov Documentation GitHub license

RA.Utilities.Logging.Core provides a set of opinionated helpers and configurations for setting up structured logging with Serilog in .NET applications. It ships with pre-wired Serilog packages (Console, File, Async sinks; Exception, Sensitive Data enrichers) so that consumers can configure them declaratively via appsettings.json without adding individual NuGet references.

📚 Table of Contents

  • Purpose
  • Key Features
  • Installation
  • How to Use
  • Contributing

Purpose

Setting up a comprehensive logging solution from scratch can be repetitive. This package abstracts away the boilerplate Serilog wiring by providing a single extension method, AddLoggingWithConfiguration, that bootstraps a production-ready foundation.

Key Features

Configured automatically by AddLoggingWithConfiguration:

  • Configuration-driven: Reads Serilog settings from appsettings.json via ReadFrom.Configuration, so log levels, sinks, and enrichers are controlled declaratively.
  • Exception details: Uses Serilog.Exceptions to destructure exceptions with full stack trace detail.
  • System.Text.Json destructuring: Uses Destructurama.SystemTextJson so that JsonElement and related types log as readable values instead of ValueKind enum names.

Optional — activate RequestIdEnricher yourself:

  • Request ID enrichment: Adds XRequestId and TraceId properties from the x-request-id header, HttpContext.TraceIdentifier, or the current Activity. Add { "Name": "WithRequestIdEnricher" } to Serilog → Enrich, or call .Enrich.WithRequestIdEnricher() manually, and register IHttpContextAccessor with builder.Services.AddHttpContextAccessor().

Available for appsettings.json configuration (packages are included as dependencies — no additional NuGet install needed):

  • Console sink — Serilog.Sinks.Console
  • File sink — Serilog.Sinks.File with rolling and size limits
  • Async wrapper — Serilog.Sinks.Async to offload I/O from the request thread
  • Sensitive data masking — Serilog.Enrichers.Sensitive

🛠️ Installation

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.


How to Use

Integrating the logger into your ASP.NET Core application is a two-step process.

Step 1: Configure appsettings.json

Add a Serilog section to your appsettings.json file. The AddLoggingWithConfiguration method reads from this section automatically.

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information",
        "System": "Warning"
      }
    },
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Sinks.Async",
      "Serilog.Enrichers.Sensitive",
      "Serilog.Exceptions"
    ],
    "Enrich": [
      { "Name": "WithExceptionDetails" },
      { "Name": "WithRequestIdEnricher" },
      {
        "Name": "WithSensitiveDataMasking",
        "Args": {
          "options": {
            "MaskValue": "***SECRET***",
            "Mode": "Globally"
          }
        }
      }
    ],
    "WriteTo:Async": {
      "Name": "Async",
      "Args": {
        "configure": [
          {
            "Name": "File",
            "Args": {
              "path": "Logs/app-.log",
              "rollingInterval": "Day",
              "rollOnFileSizeLimit": true,
              "outputTemplate": "{Timestamp:G} [{Level}] ({XRequestId})({TraceId}) {Message:lj} ({SourceContext}){NewLine}{Exception}"
            }
          },
          {
            "Name": "Console",
            "Args": {
              "outputTemplate": "{Timestamp:G} [{Level}] ({XRequestId})({TraceId}) {Message:lj} ({SourceContext}){NewLine}{Exception}"
            }
          }
        ]
      }
    }
  }
}

Step 2: Add the Logger in Program.cs

Call AddLoggingWithConfiguration() 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);

// Required when WithRequestIdEnricher is enabled in appsettings.json
builder.Services.AddHttpContextAccessor();

// Configure Serilog with opinionated defaults
builder.AddLoggingWithConfiguration();

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.


Contributing

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.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.

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
10.1.2 125 10/19/2026
10.1.1 110 10/18/2026
10.1.0 116 9/28/2026
10.0.0 393 11/23/2025
10.0.0-rc.2 228 10/30/2025