如何修复Hangfire配置错误

本文关键字:错误 配置 Hangfire 何修复 | 更新日期: 2023-09-27 18:26:07

我似乎需要配置日志记录。我可以添加作业&在/hangfire页面中看到它们,但如果我禁用应用程序,它们不会启动。使用HangfireServer()。

由于这是一个简单的基于文件夹的网站,我已经从运行示例Hangfire MVC项目中将必要的dll复制到了我的bin文件夹中。如果需要,我如何配置记录器?

Error location:
Line 17:         {
Line 18:             app.UseHangfireServer();
Line 19:             app.UseHangfireDashboard();
Line 20: 
Source File: f:'hangfire'App_Code'Startup.cs    Line: 18 

堆栈跟踪:

[ConfigurationErrorsException: The configuration section for Logging cannot be found in the configuration source.]
   Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolderCustomFactory.ValidateLoggingSettings(LoggingSettings loggingSettings) +64
   Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterStructureHolderCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) +49
   Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterCustomFactory.CreateObject(IBuilderContext context, String name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache) +66
   Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id) +83
   Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) +59
   Microsoft.Practices.ObjectBuilder.SingletonStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) +169
   Microsoft.Practices.ObjectBuilder.BuilderStrategy.BuildUp(IBuilderContext context, Type typeToBuild, Object existing, String idToBuild) +59
   Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfigurationNameMappingStrategy.BuildUp(IBuilderContext context, Type t, Object existing, String id) +102
   Microsoft.Practices.ObjectBuilder.BuilderBase`1.DoBuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) +217
   Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, Type typeToBuild, String idToBuild, Object existing, PolicyList[] transientPolicies) +127
   Microsoft.Practices.ObjectBuilder.BuilderBase`1.BuildUp(IReadWriteLocator locator, String idToBuild, Object existing, PolicyList[] transientPolicies) +87
   Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IReadWriteLocator locator, IConfigurationSource configurationSource) +135
   Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.EnterpriseLibraryFactory.BuildUp(IConfigurationSource configurationSource) +53
   Microsoft.Practices.EnterpriseLibrary.Logging.LogWriterFactory.Create() +29
   Microsoft.Practices.EnterpriseLibrary.Logging.Logger.get_Writer() +106
   lambda_method(Closure , String , TraceEventType ) +252
   Hangfire.Logging.LogProviders.EntLibLogger.Log(LogLevel logLevel, Func`1 messageFunc, Exception exception) +60
   Hangfire.Logging.LoggerExecutionWrapper.Log(LogLevel logLevel, Func`1 messageFunc, Exception exception) +87
   Hangfire.Logging.LogExtensions.IsInfoEnabled(ILog logger) +42
   Hangfire.Logging.LogExtensions.Info(ILog logger, String message) +27
   Hangfire.BackgroundJobServer..ctor(BackgroundJobServerOptions options, JobStorage storage, IEnumerable`1 additionalProcesses) +236
   Hangfire.AppBuilderExtensions.UseHangfireServer(IAppBuilder builder, JobStorage storage, BackgroundJobServerOptions options, IBackgroundProcess[] additionalProcesses) +90
   Hangfire.AppBuilderExtensions.UseHangfireServer(IAppBuilder builder, BackgroundJobServerOptions options, JobStorage storage) +42
   Hangfire.AppBuilderExtensions.UseHangfireServer(IAppBuilder builder, BackgroundJobServerOptions options) +35
   Hangfire.AppBuilderExtensions.UseHangfireServer(IAppBuilder builder) +46
   MyWebApplication.Startup.Configuration(IAppBuilder app) in f:'hangfire'App_Code'Startup.cs:18

如何修复Hangfire配置错误

我个人使用Elmah(Hangfire在新版本中自动检测到)。

但是,如果你想关闭它,将日志提供程序设置为null就可以了(你可以将其放在应用程序的启动方法中,根据你使用的启动机制的不同而有所不同)

LogProvider.SetCurrentLogProvider(null);

来源:https://discuss.hangfire.io/t/turn-off-logging/1150

您的解决方案中应用了任何记录器库吗?正如Hangfire文件所说:

从Hangfire 1.3.0开始,如果您的应用程序已通过使用以下库之一反射(这样Hangfire本身就不依赖于他们)。通过检查自动选择日志记录实现以如下所示的顺序显示相应类型的存在。

SerilogNLogLog4NetEntLib伐木百叶窗Elmah如果您想登录Hangfire事件并且没有安装日志库,请选择其中之一,并参考其文件

据我所知,您需要上面列出的一些日志库。

我有两个启动点:

[assembly: OwinStartup(typeof(MyWebApplication.Startup))]

[ApplicationStartup]

删除[ApplicationStartup]并将Hangfire相关代码移到其他位置解决了问题。

您必须添加一个日志提供程序。作为示例,您可以添加一个内置提供程序,如下所示:

LogProvider.SetCurrentLogProvider(新的ColouredConsoleLogProvider())

并且它应该解决错误。