DotNetBuildingBlocks.Serilog
1.0.1
dotnet add package DotNetBuildingBlocks.Serilog --version 1.0.1
NuGet\Install-Package DotNetBuildingBlocks.Serilog -Version 1.0.1
<PackageReference Include="DotNetBuildingBlocks.Serilog" Version="1.0.1" />
<PackageVersion Include="DotNetBuildingBlocks.Serilog" Version="1.0.1" />
<PackageReference Include="DotNetBuildingBlocks.Serilog" />
paket add DotNetBuildingBlocks.Serilog --version 1.0.1
#r "nuget: DotNetBuildingBlocks.Serilog, 1.0.1"
#:package DotNetBuildingBlocks.Serilog@1.0.1
#addin nuget:?package=DotNetBuildingBlocks.Serilog&version=1.0.1
#tool nuget:?package=DotNetBuildingBlocks.Serilog&version=1.0.1
DotNetBuildingBlocks.Serilog
Serilog-specific integration for the DotNetBuildingBlocks Diagnostics solution.
Purpose
DotNetBuildingBlocks.Serilog provides the vendor-specific glue that lets services adopt
Serilog consistently across the DotNetBuildingBlocks ecosystem
without leaking Serilog references into the generic DotNetBuildingBlocks.Logging package.
It exists so that:
- generic structured logging conventions stay in
DotNetBuildingBlocks.Logging - Serilog provider setup, enrichers, and
LoggerConfigurationhelpers live here - consumers can opt into Serilog with a single, predictable host registration call
- the same registration works in console apps, workers, generic hosts, and ASP.NET Core apps
When to use it
Use this package when your service has standardized on Serilog and you want a small, opinionated way to:
- bridge
Microsoft.Extensions.Loggingand Serilog - attach stable application identity properties (
ApplicationName,ApplicationVersion) to every log event - enrich logs with
TraceId/SpanIdfromActivity.Current - flow
CorrelationIdfromSerilog.Context.LogContext, Microsoft logger scopes, orActivity.Currentbaggage - read additional Serilog settings from
IConfiguration(appsettings.json)
What this package provides
DotNetSerilogOptions— the focused options modelIHostBuilder.UseDotNetBuildingBlocksSerilog(...)— the primary host registration entry pointIServiceCollection.AddDotNetBuildingBlocksSerilog(...)— service-collection registrationLoggerConfiguration.ApplyDotNetBuildingBlocksDefaults(...)— for manual logger compositionApplicationIdentityEnricher— addsApplicationNameandApplicationVersionActivityEnricher— addsTraceId,SpanId, andParentSpanIdfromActivity.Current- optional console / debug sinks via the options model
- optional
IConfigurationbinding viaSerilog.Settings.Configuration
What this package intentionally does not provide
- Seq, Elastic, Graylog, Application Insights, or other backend sinks
- file, database, or transport-specific sinks
- HTTP exception handling middleware
- request / response body logging
- ASP.NET Core request logging helpers (use
Serilog.AspNetCoredirectly when needed) - OpenTelemetry SDK setup, exporters, or instrumentations
- metrics or tracing implementations
- business-domain logging extensions
- a giant logging "framework"
This package keeps the public API small on purpose. Anything beyond Serilog provider setup and a few stable enrichers belongs in a different package.
Relationship to the rest of the ecosystem
DotNetBuildingBlocks.Logging— the generic structured logging conventions package. It does not depend on Serilog. This package depends on it and aligns property names where practical.DotNetBuildingBlocks.Tracing— the generic tracing helpers package. This package does not depend on it; it readsActivity.Currentdirectly so the activity enricher works whether or not the tracing package is registered.DotNetBuildingBlocks.Metrics/DotNetBuildingBlocks.Observation/DotNetBuildingBlocks.Observability— not referenced. Pair them at the application composition root if you need full OpenTelemetry observability.
Installation
dotnet add package DotNetBuildingBlocks.Serilog
Host registration
The recommended entry point is IHostBuilder.UseDotNetBuildingBlocksSerilog. It works for
worker services, generic hosts, and ASP.NET Core apps because
WebApplicationBuilder.Host implements IHostBuilder.
Worker / generic host
using DotNetBuildingBlocks.Serilog.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateDefaultBuilder(args);
builder.UseDotNetBuildingBlocksSerilog(options =>
{
options.ApplicationName = "Samples.OrderWorker";
options.ApplicationVersion = "1.0.0";
options.UseConsole = true;
options.IncludeActivityEnricher = true;
options.IncludeCorrelationEnricher = true;
});
using var host = builder.Build();
await host.RunAsync();
ASP.NET Core minimal API
using DotNetBuildingBlocks.Serilog.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseDotNetBuildingBlocksSerilog(options =>
{
options.ApplicationName = "Samples.OrderApi";
options.ApplicationVersion = "1.0.0";
options.UseConsole = true;
});
var app = builder.Build();
app.MapGet("/", () => "Hello, world!");
app.Run();
Manual LoggerConfiguration composition
Use ApplyDotNetBuildingBlocksDefaults if you need to build a Logger outside of the host.
using DotNetBuildingBlocks.Serilog.Configuration;
using DotNetBuildingBlocks.Serilog.Options;
using Serilog;
var logger = new LoggerConfiguration()
.ApplyDotNetBuildingBlocksDefaults(
new DotNetSerilogOptions
{
ApplicationName = "Samples.Manual",
ApplicationVersion = "1.0.0",
UseConsole = true
},
environmentName: "Development")
.CreateLogger();
logger.Information("Manual logger ready.");
Enrichers
| Enricher | Properties added | Notes |
|---|---|---|
ApplicationIdentityEnricher |
ApplicationName, ApplicationVersion |
Always applied. ApplicationVersion is added only when supplied. |
ActivityEnricher |
TraceId, SpanId, ParentSpanId |
Read from Activity.Current. Skipped when no activity exists. |
| Internal correlation enricher | CorrelationId |
Promotes CorrelationId from Activity.Current baggage when not already present on the event. |
| Built-in machine name | MachineName |
Static, set at configuration time. |
| Built-in environment name | EnvironmentName |
Read from IHostEnvironment or DOTNET_ENVIRONMENT / ASPNETCORE_ENVIRONMENT. |
| Built-in thread id | ThreadId |
Off by default. |
Enrich.FromLogContext() is always wired so any properties pushed via
Serilog.Context.LogContext.PushProperty(...) — or via Microsoft logger scopes
exposed by Serilog.Extensions.Logging — flow into structured output.
Activity / correlation enrichment
When IncludeActivityEnricher is enabled, every log event written while an
Activity.Current exists includes:
TraceId = <hex trace id>
SpanId = <hex span id>
ParentSpanId = <hex parent span id, if any>
using var activity = new Activity("ProcessOrder").Start();
logger.LogInformation("Processing order {OrderId}.", 42);
The internal correlation enricher additionally promotes a CorrelationId value from
Activity.Current.Baggage when the log event does not already carry one. Pushing the
correlation id directly via LogContext (or via a Microsoft logger scope) is preferred
and always wins.
Configuration
When ReadFromConfiguration is enabled (the default), the package reads the
configured Serilog section via Serilog.Settings.Configuration. The package then applies
its own defaults on top, so explicit code-based options take precedence over configuration.
// appsettings.json
{
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft.AspNetCore": "Warning"
}
},
"WriteTo": [
{ "Name": "Console" }
]
}
}
Configuration precedence
Serilog.Settings.Configurationreads the configuredSerilogsection (ifReadFromConfigurationis enabled).- The package then calls
ApplyDotNetBuildingBlocksDefaults, which sets the minimum level, adds the application identity / activity / correlation enrichers, and enables the console / debug sinks based on the options.
In other words, code-based options applied through DotNetSerilogOptions always win over
values read from configuration. This is deterministic and the same in every host type.
Sample output
A typical log event written by a worker looks like this when the JSON formatter is enabled:
{
"@t": "2026-04-10T08:30:00.1234567Z",
"@mt": "Processing order {OrderId}.",
"@l": "Information",
"OrderId": 42,
"ApplicationName": "Samples.OrderWorker",
"ApplicationVersion": "1.0.0",
"EnvironmentName": "Development",
"MachineName": "LOCAL-DEV-01",
"TraceId": "8a3c60f7d188f8fa79d48a392c986a36",
"SpanId": "1f1d6a2e1f6f3b8c",
"CorrelationId": "0d3f8c4a-a2c0-4b71-a4c3-1f3b8f8e0a11"
}
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- DotNetBuildingBlocks.Logging (>= 1.0.0)
- Microsoft.Extensions.Configuration.Abstractions (>= 10.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.5)
- Microsoft.Extensions.Options (>= 10.0.5)
- Serilog (>= 4.1.0)
- Serilog.Extensions.Hosting (>= 8.0.0)
- Serilog.Extensions.Logging (>= 8.0.0)
- Serilog.Settings.Configuration (>= 8.0.4)
- Serilog.Sinks.Console (>= 6.0.0)
- Serilog.Sinks.Debug (>= 3.0.0)
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 |
|---|