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

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

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

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

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 class:

中使用它
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();

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

对于点网核心,

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

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