Log Events Using Existing Serialogs

Im new to hangfire, and I'm trying to set up an event logging method using my existing serilog logger in 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 boot file I add the code from the hangfire documentation:

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

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

My fage works fine, but how do I include make hangfire in my seriallog?

+4
source share
1 answer

It is possible that Hangfire initializes and caches its own internal logger before the application CreateLog()is called by the application.

, , Log.Logger, , . Global.Application_Start() .

+2

Source: https://habr.com/ru/post/1655161/


All Articles