Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to turn off logging that Hangfire does with serilog?

Is there a way to turn off logging that Hangfire does with serilog? We are using our own abstraction and I don’t want all this extra noise coming from the Hangfire logger while using serilog.

// INIT call under web project 
namespace MYApp.Web.App_Start
{
    public static class Bootstrapper
    {
        public static void Run()
        {
            Core.Logging.Serilog.SetConfiguration();
        }
    }
}

// project where config method is setup

namespace MYApp.Core.Logging
{
 public static class Serilog
    {
      public static void SetConfiguration()
            {

                Log.Logger = new LoggerConfiguration()
                    //.WriteTo.File(@"c:\log-.txt", rollingInterval: RollingInterval.Minute, shared: true)
                    .WriteTo.Email(new EmailConnectionInfo()
                    {
                        FromEmail = "[email protected]",
                        MailServer = "smtp.gmail.com",
                        ToEmail = "[email protected]",
                        NetworkCredentials = new NetworkCredential("[email protected]", "xxxxxxxxx"),
                        Port = 587,
                        EnableSsl = true,
                        EmailSubject = "YYYYYY: Error Log"
                    }, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {NewLine} {Message:lj}{NewLine}{Exception}")
                    .Filter.ByExcluding(Matching.FromSource("Hangfire"))
                    .CreateLogger();
            }
 }
}
like image 465
Kaushik Thanki Avatar asked Sep 10 '18 05:09

Kaushik Thanki


People also ask

Can you stop logging with Serilog?

according to Microsoft documentation for logging if you set the log level to None, all logs will be suppressed. To suppress all logs, specify LogLevel. None. LogLevel.

What is Serilog logging?

Serilog is an easy-to-set-up logging library for . NET with a clear API. In the long list of the Serilog's features you can find: Support of structured logging, which allows logs to be treated as data sets rather than text. Compatibility with asynchronous applications and systems.

Can Serilog log database?

Serilog is a highly efficient logging framework that helps us enable logging to almost all logging source formats including Console, File, Database logging, etc.

What is Serilog sink?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.


4 Answers

Define a logger and log provider that does not log anything:

using Hangfire;
using Hangfire.Logging;

public class NoLoggingProvider : ILogProvider {
    public ILog GetLogger(string name) {
        return new NoLoggingLogger();
    }
}

public class NoLoggingLogger : ILog {
    public bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception = null) {
        return false;
    }
}

and configure Hangfire to use it in your Startup class:

using Hangfire;

public class Startup {
    public void Configuration(IAppBuilder app) {
        GlobalConfiguration.Configuration.UseLogProvider(new NoLoggingProvider());
        // the rest of your configuration...        
    }
}
like image 63
Richard Avatar answered Oct 16 '22 01:10

Richard


For Dot Net Core,

In appsettings.json, just add "Hangfire" under "MinimumLevel => Override" and value as "Warning". This will only log Warning and Error.

If you set as "Override" then it will only log error from hangfire.

enter image description here

like image 42
prisan Avatar answered Oct 16 '22 00:10

prisan


You could exclude the entire Hangfire namespace (assuming it's all under one namespace -- I've never used it before) using a Filter expression:

.Filter.ByExcluding(Matching.FromSource("Hangfire"))

like image 32
PatrickSteele Avatar answered Oct 16 '22 01:10

PatrickSteele


To develop @PatrickSteele's answer, you seemingly need to use the SourceContent of SeriLog. Hangfire has a number of these. This code implements this:

        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.ExpirationManager"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.SqlServerObjectsInstaller"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.BackgroundServerProcess"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.ServerWatchdog"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.ServerHeartbeatProcess"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.Processing.BackgroundExecution"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.CountersAggregator"))
            .Filter.ByExcluding(Matching.FromSource("Hangfire.BackgroundJobServer"))
            .CreateLogger();

Of course, this is my configuration, which uses SQL Server as a target for logging. It may prove to be useful to collect the various sources from Hangfire in this one place for inclusion in your own code (though this is not necessarily exhaustive).

like image 44
Program.X Avatar answered Oct 16 '22 02:10

Program.X