如何修复 Hangfire 配置错误?

How can I fix Hangfire configuration error?

看来我需要配置日志记录。我可以添加作业并在 /hangfire 页面中查看它们,但是如果我禁用 app.UseHangfireServer().

它们就不会触发

由于它是一个简单的基于文件夹的网站,我已经从 运行 示例 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

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

但是如果你想关闭它,将日志提供程序设置为 null 应该可以解决问题(你可以将它放在你的应用程序的启动方法中,这将根据你的机制而有所不同'重新用来执行启动)

LogProvider.SetCurrentLogProvider(null);

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

您的解决方案是否应用了任何记录器库?正如 Hangfire 文档所说:

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 If you want to log Hangfire events and have no logging library installed, please choose one of the above and refer to its documentation

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

我有两个启动点:

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

[ApplicationStartup]

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

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

LogProvider.SetCurrentLogProvider(new ColouredConsoleLogProvider());

它应该可以解决错误。