.NET 核心:使用 NLog 设置 Hangfire [英] .NET core: Hangfire setup with NLog

查看:34
本文介绍了.NET 核心:使用 NLog 设置 Hangfire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an .NET Core 2 powered API that I would like to add Hangfire to. The project is already using NLog to log to a MySQL database and it works fine, but when I try to setup and use Hangfire I get the following error:

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

Hangfire 仪表板工作正常,但在尝试像这样排列我的第一份工作时出现该错误:

The Hangfire dashboard works, but I get that error when trying to enqueue my first job like this:

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

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

I have read the Hangfire documentation over at: http://docs.hangfire.io/en/latest/configuration/configuring-logging.html

它说:

从 Hangfire 1.3.0 开始,如果您的应用程序已经通过反射使用了以下库之一(因此 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.

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

That list includes NLog, so apparently I am doing something wrong.

在我的 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 中,我有:

In Startup.cs and ConfigureServices I have:

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 数据库,所以这似乎是有效的.

and it does log to the MySQL database without Hangfire, so that seems to be working.

查看 Nlog 文档:

Looking at the Nlog documentation at:

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

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

They seem to add NLog in Program.cs instead of Startup.cs, so I tried that approach as well, but I still get the same error.

推荐答案

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

Looks like the Hangfire.MySqlStorage library was the root cause of this error. After changing to Hangfire.MySql.Core everything works great without any changes being made to NLog.

这篇关于.NET 核心:使用 NLog 设置 Hangfire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆