0
votes

I have created a standalone reproduction of issue here: https://github.com/GuerrillaCoder/HangfireLoggingTest

Versions used:

  <ItemGroup>
    <PackageReference Include="Hangfire.AspNetCore" Version="1.7.12" />
    <PackageReference Include="Hangfire.Console" Version="1.4.2" />
    <PackageReference Include="Hangfire.Core" Version="1.7.12" />
    <PackageReference Include="Hangfire.MemoryStorage.Core" Version="1.4.0" />
    <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
  </ItemGroup>

Issue:

Hangfire never writes anything to logs which makes it very difficult to debug issues with tasks stalling or failing.

I believe I have followed the instructions correctly and set LogLevel to Trace but nothing is written to logs apart from when I directly send message to the Nlog logger.

Step to reproduce

  1. git clone https://github.com/GuerrillaCoder/HangfireLoggingTest
  2. run project (observe that logs are created in LogOutput folder and message is written showing NLog is working)
  3. Navigate to /hangfire and manually trigger task
  4. Observe hangfire has not written any log messages at all
1
Remember to configure NLog.config in project so Copy to Output Directory equals Copy if newer. See also github.com/NLog/NLog/wiki/Logging-troubleshootingRolf Kristensen
That has been set. Nlog is working correctly, hangfire is not logging to it.Guerrilla
But your example project doesn't have this configured. Care to update it on github?Rolf Kristensen
Maybe you can try declaring Nlog in your program.cs, like this : github.com/NLog/NLog/wiki/…jbl

1 Answers

0
votes

Try updating the ConfigureServices like this:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Register NLog as LoggingProvider for Microsoft ILogger
    services.AddLogging(builder => {
       loggingBuilder.ClearProviders();
       loggingBuilder.AddNLog();
    });

    services.AddControllersWithViews();

    // Only needed for NetFramework with LibLog. NetCore uses Microsoft ILogger
    // GlobalConfiguration.Configuration.UseNLogLogProvider();

    services.AddHangfire((isp, config) =>
    {
        config.UseMemoryStorage();
        config.UseConsole(); // https://www.nuget.org/packages/Hangfire.Console/
    });
}

See also: https://docs.hangfire.io/en/latest/configuration/configuring-logging.html