Skip to main content

RA.Utilities.Logging.Core.Extensions

Namespace: RA.Utilities.Logging.Core.Extensions

The classes in this namespace are the configuration and setup entry points of the logging framework. Each class extends exactly one type, and every member is declared as a C# 14 extension member (extension(Target) { ... }).

Classes​

1. WebApplicationBuilderExtensions​

This is the most important class in the namespace. Its purpose is to provide a "one-liner" setup for a production-ready Serilog configuration in a .NET application.

  • AddLoggingWithConfiguration(): The main entry point. It abstracts away the boilerplate needed to configure Serilog correctly. In a single call it:

    • Hooks Serilog into the host (builder.Host.UseSerilog(...)).
    • Reads configuration from appsettings.json (ReadFrom.Configuration).
    • Adds WithExceptionDetails to log detailed exception information.
    • Enables destructuring of System.Text.Json objects for better structured logs.

    Request ID enrichment remains opt-in. Activate WithRequestIdEnricher through the Serilog → Enrich configuration or by calling the fluent extension manually, then register IHttpContextAccessor with builder.Services.AddHttpContextAccessor().

2. LoggerEnrichmentConfigurationExtensions​

Extends Serilog.Configuration.LoggerEnrichmentConfiguration.

  • WithRequestIdEnricher(): A fluent helper that adds the custom RequestIdEnricher to the Serilog pipeline, contributing the TraceId correlation property. It returns the LoggerConfiguration so it can be chained, and throws ArgumentNullException when the receiver is null.

3. ActivityExtensions​

Extends System.Diagnostics.Activity.

  • GetActivityId(): Returns the activity's id as a string based on its IdFormat (Hierarchical → Activity.Id, W3C → SpanId.ToHexString()). Returns null when there is no trace context, so downstream code can decide whether to add a trace property. This is the fallback RequestIdEnricher uses when no HttpContext is available.
v10.1.1

AddLoggingWithConfiguration() and WithRequestIdEnricher() were previously declared together in a single LoggingExtensions class. Since v10.1.1 the methods live in one class per extended type (WebApplicationBuilderExtensions and LoggerEnrichmentConfigurationExtensions), and ActivityExtensions was converted to a C# 14 extension member. Method names, signatures, and the namespace are unchanged for call sites — see the migration guide for the rare cases that need updating.

🧠 Summary​

In summary, the RA.Utilities.Logging.Core.Extensions namespace is the "developer-friendly API" for the logging library. It makes it simple to get a powerful, pre-configured, and consistent logging implementation up and running with minimal effort, while keeping each extension class focused on the type it extends.