主机时使用Serilog的日志错误。崩溃.NET 6顶级语句程序
我们在.NET 6控制台应用程序中使用Serilog。以下代码是示例程序。当主机崩溃时,我们如何在配置的记录器中捕获异常?
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以提供日志吗?
你可以尝试这个
Can you provide the logs ?
You can try this