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

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

问题描述

我有一个要添加Hangfire的.NET Core 2驱动的API.该项目已经在使用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/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中,我有:

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中而不是Startup.cs中添加了NLog,因此我也尝试了这种方法,但是仍然遇到相同的错误.

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天全站免登陆