.NET core: Hangfire setup with NLog
Asked Answered
D

1

0

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()'.

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"));

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

and it says:

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.

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

In my csproj I have:

<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" />

In Startup.cs and ConfigureServices I have:

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

and in Configure I have:

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

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

My nlog.config contains:

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

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

Looking at the Nlog documentation at:

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

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.

Dessiedessma answered 20/4, 2018 at 4:23 Comment(0)
D
0

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.

Dessiedessma answered 20/4, 2018 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.