繁体   English   中英

有没有办法关闭 Hangfire 对 serilog 所做的日志记录?

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

有没有办法关闭 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();
            }
 }
}

定义一个不记录任何内容的记录器和日志提供者:

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

要开发@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();

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

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

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

对于点网核心,

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

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

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM