Log error using serilog when host. crashes .net 6 top level statements program.cs

Viewed 396

We are using serilog in .net 6 console application. Below code is sample program.cs. When host crashes how do we capture the exception in the configured logger?

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

IHost host = Host.CreateDefaultBuilder(args)
    .UseSerilog((ctx, lc) => lc
    .ReadFrom.Configuration(ctx.Configuration))
    .ConfigureServices((context, services) =>
    {
        services.AddHostedService<Worker>();
    })
    .Build();
try
{
   await host.RunAsync();
}
catch(Exception ex)
{
   // Need to log the error
   // 
   throw;
}
0 Answers
Related