Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Serilog logging for Microsoft.Extensions.Hosting

License

NotificationsYou must be signed in to change notification settings

serilog/serilog-extensions-hosting

Repository files navigation

Serilog logging forMicrosoft.Extensions.Hosting. This package routes framework log messages through Serilog, so you can get information about the framework's internal operations written to the same Serilog sinks as your application events.

Versioning: This package tracks the versioning and target framework support of itsMicrosoft.Extensions.Hosting dependency. Most users should choose the version ofSerilog.Extensions.Hosting that matchestheir application's target framework. I.e. if you're targeting .NET 7.x, choose a 7.x version ofSerilog.Extensions.Hosting. Ifyou're targeting .NET 8.x, choose an 8.xSerilog.Extensions.Hosting version, and so on.

Instructions

First, install theSerilog.Extensions.HostingNuGet package into your app. You will need a way to view the log messages -Serilog.Sinks.Console writes these to the console; there aremany more sinks available on NuGet.

dotnet add package Serilog.Extensions.Hostingdotnet add package Serilog.Sinks.Console

Next, in your application'sProgram.cs file, configure Serilog first. Atry/catch block will ensure any configuration issues are appropriately logged. CallAddSerilog() on the host application builder:

usingSerilog;Log.Logger=newLoggerConfiguration().Enrich.FromLogContext().WriteTo.Console().CreateLogger();try{Log.Information("Starting host");varbuilder=Host.CreateApplicationBuilder(args);builder.Services.AddHostedService<PrintTimeService>();builder.Services.AddSerilog();varapp=builder.Build();awaitapp.RunAsync();return0;}catch(Exceptionex){Log.Fatal(ex,"Host terminated unexpectedly");return1;}finally{awaitLog.CloseAndFlushAsync();}

Finally, clean up by removing the remaining"Logging" section fromappsettings.json files (this can be replaced withSerilog configuration as shown inthis example, if required)

That's it! You will see log output like:

[22:10:39 INF] Getting the motors running...[22:10:39 INF] The current time is: 12/05/2018 10:10:39 +00:00

A more complete example, showingappsettings.json configuration, can be found inthe sample project here.

Using the package

WithSerilog.Extensions.Hosting installed and configured, you can write log messages directly through Serilog or anyILogger interface injected by .NET. All loggers will use the same underlying implementation, levels, and destinations.

Inline initialization

You can alternatively configure Serilog using a delegate as shown below:

// dotnet add package Serilog.Settings.Configurationbuilder.Services.AddSerilog((services,loggerConfiguration)=>loggerConfiguration.ReadFrom.Configuration(builder.Configuration).Enrich.FromLogContext().WriteTo.Console())

This has the advantage of makingbuilder'sConfiguration object available for configuration of the logger, but at the expense of ignoringExceptions raised earlier in program startup.

If this method is used,Log.Logger is assigned implicitly, and closed when the app is shut down.

About

Serilog logging for Microsoft.Extensions.Hosting

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp