为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
人到中年有点甜 2022-01-09 16:54:29
有没有办法关闭 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();            } }}
查看完整描述

3 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

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


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

    }

}


查看完整回答
反对 回复 2022-01-09
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

要开发@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 的各种资源以包含在您自己的代码中可能会被证明是有用的(尽管这不一定是详尽的)。


查看完整回答
反对 回复 2022-01-09
?
慕森卡

TA贡献1806条经验 获得超8个赞

对于点网核心,

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

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

//img1.sycdn.imooc.com//61daa2f800012b8610910784.jpg

查看完整回答
反对 回复 2022-01-09
  • 3 回答
  • 0 关注
  • 648 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号