gpt4 book ai didi

Hangfire 不使用配置的记录器记录异常

转载 作者:行者123 更新时间:2023-12-05 07:34:17 27 4
gpt4 key购买 nike

根据 hangfire documentation

Starting from Hangfire 1.3.0, you are not required to do anything, if your application already uses one of the following libraries through the reflection (so that Hangfire itself does not depend on any of them). Logging implementation is automatically chosen by checking for the presence of corresponding types in the order shown below.

Serilog
NLog
Log4Net
EntLib
Logging
Loupe
Elmah

我的 ASP.NET Core 2.0 应用程序正在使用 Serilog,如下所示

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseApplicationInsights()
.UseUrls("http://*:40006")
.ConfigureAppConfiguration((hostingContext, config) =>
{
// removed for bravity
})
.ConfigureLogging((hostingContext, logging) =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostingContext.Configuration)
.CreateLogger();

logging.AddSerilog();

})
.Build();
}

应用程序配置为使用 Hangfire。在后台作业处理期间,如果发生任何异常,Hangfire 会按照预期的延迟重试作业 10 次,并在仪表板中显示异常。

问题
Hangfire 仪表板在 UI 上显示异常,但它不会将异常记录到配置的 Serilog 接收器中。

注意:Hangfire 仪表板显示异常但它格式化异常 see here它隐藏了有关异常的关键信息。我认为如果它记录异常,Serilog 记录器将记录完整的异常。

最佳答案

我在使用 .NET Core 3.1 的最新 Hangfire 版本中遇到了同样的问题。

解决方案是在StartUp.cs中调用IGlobalConfigurationUseSerilogLogProvider()方法。

例如:

 GlobalConfiguration.Configuration.UseSerilogLogProvider();

services.AddHangfire(configuration => configuration
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSerilogLogProvider());

通常不需要此调用,但在某些情况下自动连接不起作用(例如引用多个日志记录提供程序)。您可以在 Hangfire 官方文档中阅读更多相关信息:Configure Logging

关于Hangfire 不使用配置的记录器记录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com