Skip to content

SerilogBestPractices: retarget net10.0, switch to AddSerilog, fix level moniker width - #2122

Open
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/110031-dotnet-best-practices-for-logging-with-serilog
Open

SerilogBestPractices: retarget net10.0, switch to AddSerilog, fix level moniker width#2122
vladimir-pecanac-main wants to merge 2 commits into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/110031-dotnet-best-practices-for-logging-with-serilog

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Updates the dotnet-logging/SerilogBestPractices sample for the refreshed Serilog Best Practices for Logging in .NET article.

  • Retarget both projects to net10.0.
  • builder.Host.UseSerilog(...) -> builder.Services.AddSerilog(...), the form serilog-aspnetcore now documents. Not a one-for-one rename: the lambda receives an IServiceProvider instead of a HostBuilderContext, so the configuration is re-sourced from builder.Configuration.
  • appsettings.json output template {Level:u11} -> {Level:u3}. The number is a maximum width, not a field width, so u11 printed every level in full (INFORMATION, WARNING) rather than the conventional aligned INF/WRN/ERR.
  • Move .MinimumLevel.Information() above the sinks in the bootstrap configuration (readability only; both are consumed inside CreateLogger(), so chain order does not affect behaviour).
  • Bring the Serilog packages current: Serilog 4.4.0, Serilog.AspNetCore 10.0.0, Serilog.Settings.Configuration 10.0.1, Enrichers.Thread 4.0.0, Enrichers.Process 3.0.0, Enrichers.Environment 3.0.1, Sinks.Console 6.1.1, Sinks.Seq 9.1.0, Enrichers.ClientInfo 2.9.0, plus Microsoft.AspNetCore.OpenApi and Mvc.Testing 10.0.11.
  • Swashbuckle.AspNetCore 6.4.0 -> 10.2.3. On net10.0 the old version throws TypeLoadException from SwaggerGenerator.GetSwagger against Microsoft.OpenApi 2.0.0, and the sample's own catch-all disguises that as a host that never got built.

dotnet build -c Release succeeds and dotnet test passes 1/1 on .NET 10.0.10.

…el moniker width

- Retarget both projects to net10.0.
- builder.Host.UseSerilog(...) -> builder.Services.AddSerilog(...), the form
  serilog-aspnetcore now documents. The lambda receives an IServiceProvider
  instead of a HostBuilderContext, so the configuration is re-sourced from
  builder.Configuration.
- appsettings.json output template: {Level:u11} -> {Level:u3}. The width is a
  maximum, not a field width, so u11 printed every level in full (INFORMATION,
  WARNING) instead of the conventional aligned INF/WRN/ERR.
- Move .MinimumLevel.Information() above the sinks in the bootstrap
  configuration so it reads configuration-then-destinations. Chain order does
  not affect behaviour; both are consumed inside CreateLogger().
- Bring packages current: Serilog 4.4.0, Serilog.AspNetCore 10.0.0,
  Serilog.Settings.Configuration 10.0.1, Serilog.Enrichers.Thread 4.0.0,
  Serilog.Enrichers.Process 3.0.0, Serilog.Enrichers.Environment 3.0.1,
  Serilog.Sinks.Console 6.1.1, Serilog.Sinks.Seq 9.1.0,
  Serilog.Enrichers.ClientInfo 2.9.0, Microsoft.AspNetCore.OpenApi 10.0.11,
  Microsoft.AspNetCore.Mvc.Testing 10.0.11.
- Swashbuckle.AspNetCore 6.4.0 -> 10.2.3. 6.4.0 throws TypeLoadException from
  SwaggerGenerator.GetSwagger against Microsoft.OpenApi 2.0.0 on net10.0, and
  the sample's own catch-all disguises it as a host that never got built.
The host now replaces the startup logger once appsettings.json is read, which
is the two-stage initialization serilog-aspnetcore documents. CreateLogger()
left the static Log.Logger pinned to the pre-host configuration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant