【问题标题】:Is there a way to turn off logging that Hangfire does with serilog?有没有办法关闭 Hangfire 对 serilog 所做的日志记录?
【发布时间】:2018-09-10 05:19:02
【问题描述】:

有没有办法关闭 Hangfire 对 serilog 所做的日志记录?我们正在使用自己的抽象,我不希望在使用 serilog 时来自 Hangfire 记录器的所有这些额外噪音。

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

// 配置方法设置的项目

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 = "xxxxxx@gmail.com",
                        MailServer = "smtp.gmail.com",
                        ToEmail = "xxxx@xxxx.xx.xx",
                        NetworkCredentials = new NetworkCredential("xxxx@gmail.com", "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();
            }
 }
}

【问题讨论】:

    标签: c# asp.net-mvc hangfire serilog


    【解决方案1】:

    定义不记录任何内容的记录器和日志提供程序:

    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;
        }
    }
    

    并配置 Hangfire 以在您的 Startup 类中使用它:

    using Hangfire;
    
    public class Startup {
        public void Configuration(IAppBuilder app) {
            GlobalConfiguration.Configuration.UseLogProvider(new NoLoggingProvider());
            // the rest of your configuration...        
        }
    }
    

    【讨论】:

      【解决方案2】:

      对于点网核心,

      在 appsettings.json 中,只需在“MinimumLevel => Override”下添加“Hangfire”,并将值设为“Warning”。这只会记录警告和错误。

      如果您设置为“覆盖”,那么它只会记录挂火的错误。

      【讨论】:

        【解决方案3】:

        您可以使用过滤器表达式排除整个 Hangfire 命名空间(假设它都在一个命名空间下——我以前从未使用过它):

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

        【讨论】:

        • 请将您的日志记录初始化代码添加到您的问题中。
        【解决方案4】:

        要开发@PatrickSteele 的答案,您似乎需要使用 SeriLog 的 SourceContent。 Hangfire有很多这样的。这段代码实现了这个:

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

        当然,这是 my 配置,它使用 SQL Server 作为日志记录的目标。在这个地方收集来自 Hangfire 的各种资源以包含在您自己的代码中可能会被证明是有用的(尽管这不一定是详尽的)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-27
          • 2014-09-23
          • 2012-04-13
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多