Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log hangfire events using existing serilog

Im new to hangfire, and im trying to setup a way to log the events using my existing serilog logger in an asp.net web api. Here is my logger class:

public static class LoggerInitializer
{
    private static ILogger CreateLog()
    {
        var settings = Settings.Instance;
        Log.Logger = new LoggerConfiguration().
            MinimumLevel.Debug().
            WriteTo.RollingFile(settings.LoggerDebugDirectory +"Debug-{Date}.txt", restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug, 
                outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}").
            WriteTo.RollingFile(settings.LoggerVerboseDirectory + "Debug-{Date}.txt").
            CreateLogger();
        return Log.Logger;
    }
    static ILogger _logger;
    public static ILogger GetLogger()
    {
        if (_logger != null)
            return _logger;
        return _logger = CreateLog();
    }
}

and in my startup file I add the code from the hangfire documentation:

GlobalConfiguration.Configuration
            .UseSqlServerStorage(Settings.Instance.NLSContextConnectionString);

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

My hangfire works perfectly, but how do i enable make hangfire use my serilog?

like image 343
Jonas Olesen Avatar asked Sep 20 '16 08:09

Jonas Olesen


People also ask

How do you use Serilog logging?

Create a Console Application project in Visual Studio. Install Serilog and its dependencies. Create and configure the Serilog global logger. Integrate the logger into the C# Console Application.

What is Serilog ForContext?

Serilog's ForContext() method returns an ILogger that attaches a specified property to all of the events logged through it. The ILogger can be used multiple times, for example by storing it in a variable or field: var cartLog = Log. ForContext("Source", typeof(CartController).

Does Serilog support .NET core?

Since Serilog supports ASP.NET Cores default logging APIs it can receive log events from ASP.NET Core framework libraries as well. Serilog in ASP.NET Core is very easy to set up and integrate. Serilog provides a structured logging framework and supports a wide variety of sinks to log to console, files, azure, etc.

How do you add Serilog?

Configure Serilog in appsettings. We configured the logger to use the settings from the application configuration. Let's open the appsettings. json file and configure Serilog. I insert a sample configuration.


2 Answers

It's possible that Hangfire is initializing and caching its own internal logger before CreateLog() is being called by the application.

To test this theory, try moving the code that initializes Log.Logger to the very beginning of the app's startup code, e.g. in Global.Application_Start() or similar.

like image 165
Nicholas Blumhardt Avatar answered Oct 15 '22 12:10

Nicholas Blumhardt


In Hangfire 1.6.19 (and maybe before that, I did not check) adding the NuGet Package to your project gives you an extension method on IGlobalConfiguration :

configuration.UseSerilogLogProvider();

like image 26
Richard Avatar answered Oct 15 '22 12:10

Richard