【问题标题】:.NET core: Hangfire setup with NLog.NET 核心:使用 NLog 设置 Hangfire
【发布时间】:2018-04-20 04:23:42
【问题描述】:

我有一个支持 .NET Core 2 的 API,我想将 Hangfire 添加到其中。该项目已经在使用 NLog 记录到 MySQL 数据库并且工作正常,但是当我尝试设置和使用 Hangfire 时,出现以下错误:

Method not found: 'Hangfire.Logging.ILog Hangfire.Logging.LogProvider.GetCurrentClassLogger()'.

Hangfire 仪表板可以工作,但我在尝试像这样将我的第一份工作加入队列时出现该错误:

BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget"));

我已在以下位置阅读了 Hangfire 文档: http://docs.hangfire.io/en/latest/configuration/configuring-logging.html

它说:

从 Hangfire 1.3.0 开始,如果您的应用程序已经通过反射使用了以下库之一(因此 Hangfire 本身不依赖于其中任何一个),您无需执行任何操作。通过按如下所示顺序检查相应类型的存在来自动选择日志记录实现。

该列表包括 NLog,所以显然我做错了什么。

在我的csproj 我有:

<PackageReference Include="Hangfire" Version="1.6.19" />
<PackageReference Include="Hangfire.MySqlStorage" Version="1.0.5" />
<PackageReference Include="MySql.Data" Version="8.0.11" />
<PackageReference Include="NLog" Version="4.5.3" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.5.2" />

Startup.csConfigureServices 我有:

services.AddHangfire(config => config.UseStorage(new MySqlStorage(appSettings.GetConnectionString("HangfireConnectionString"))));

Configure 我有:

loggerFactory.AddNLog();
env.ConfigureNLog("nlog.config");

app.UseHangfireDashboard();
app.UseHangfireServer();

我的nlog.config 包含:

<target name="database" xsi:type="Database" dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" connectionString="server=localhost;Database=nlog;user id=root;password=;SslMode=none;">

它确实在没有 Hangfire 的情况下登录到 MySQL 数据库,所以这似乎是有效的。

查看 Nlog 文档:

https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2

他们似乎在Program.cs 而不是Startup.cs 中添加了NLog,所以我也尝试了这种方法,但我仍然遇到同样的错误。

【问题讨论】:

    标签: asp.net-core .net-core nlog hangfire


    【解决方案1】:

    看起来Hangfire.MySqlStorage 库是此错误的根本原因。更改为Hangfire.MySql.Core 后,一切正常,无需对 NLog 进行任何更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多