- Notifications
You must be signed in to change notification settings - Fork34
Serilog logging for Microsoft.Extensions.Hosting
License
serilog/serilog-extensions-hosting
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
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.
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:00A more complete example, showingappsettings.json configuration, can be found inthe sample project here.
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.
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
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.