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

            }

 }

}


人到中年有点甜
浏览 649回答 3
3回答

斯蒂芬大帝

定义一个不记录任何内容的记录器和日志提供程序:using Hangfire;using Hangfire.Logging;public class NoLoggingProvider : ILogProvider {&nbsp; &nbsp; public ILog GetLogger(string name) {&nbsp; &nbsp; &nbsp; &nbsp; return new NoLoggingLogger();&nbsp; &nbsp; }}public class NoLoggingLogger : ILog {&nbsp; &nbsp; public bool Log(LogLevel logLevel, Func<string> messageFunc, Exception exception = null) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}并配置 Hangfire 以在您的Startup课程中使用它:using Hangfire;public class Startup {&nbsp; &nbsp; public void Configuration(IAppBuilder app) {&nbsp; &nbsp; &nbsp; &nbsp; GlobalConfiguration.Configuration.UseLogProvider(new NoLoggingProvider());&nbsp; &nbsp; &nbsp; &nbsp; // the rest of your configuration...&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}

慕标琳琳

要开发@PatrickSteele 的答案,您似乎需要使用 SeriLog 的 SourceContent。Hangfire有很多这样的。此代码实现了这一点:&nbsp; &nbsp; &nbsp; &nbsp; Log.Logger = new LoggerConfiguration()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ReadFrom.Configuration(Configuration)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.ExpirationManager"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.SqlServerObjectsInstaller"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.BackgroundServerProcess"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.ServerWatchdog"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.Server.ServerHeartbeatProcess"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.Processing.BackgroundExecution"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.SqlServer.CountersAggregator"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Filter.ByExcluding(Matching.FromSource("Hangfire.BackgroundJobServer"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .CreateLogger();当然,这是我的配置,它使用 SQL Server 作为日志记录的目标。在这个地方收集来自 Hangfire 的各种资源以包含在您自己的代码中可能会被证明是有用的(尽管这不一定是详尽的)。

慕森卡

对于点网核心,在 appsettings.json 中,只需在“MinimumLevel => Override”下添加“Hangfire”,并将值设置为“&nbsp;Warning&nbsp;”。这只会记录警告和错误。如果您设置为“覆盖”,那么它只会记录来自hangfire的错误。
打开App,查看更多内容
随时随地看视频慕课网APP