c# - Serilog:如何排除多个来源

标签 c# logging asp.net-core asp.net-core-2.0 serilog

我想要多个日志文件,我正试图找到一种方法来排除多个源。这是我的代码:

Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.Logger(l => l.Filter.ByIncludingOnly(Matching.FromSource("Hangfire"))
        .WriteTo.Async(a => a.RollingFile("Logs/hangfire-{Date}.txt"))
    )
    .WriteTo.Logger(l => l.Filter.ByExcluding(Matching.FromSource("Hangfire"))
        .WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt"))
    )
    .CreateLogger();

如您所见,我有子记录器:第一个仅收集 hangfire 日志,第二个收集除 hangfire 之外的所有日志。但是,我想添加另一个排除过滤器,例如,我想排除 hangfire 和 IMyClass 日志。我该怎么做?

最佳答案

Matching.FromSource() 返回一个从 LogEventbool 的函数。您可以利用它来创建两个过滤器函数:

var hangfire = Matching.FromSource("Hangfire");
var myClass = Matching.FromSource("MyClass");

然后内联调用它们:

.WriteTo.Logger(l => l.Filter.ByExcluding(le => myClass(le) || hangfire(le))
    .WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt"))
)

关于c# - Serilog:如何排除多个来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46706933/

相关文章:

c# - 跨 .cake 文件共享全局变量

docker - 我的结构化日志消息中的时间字段来自哪里?

asp.net-core - 无法请求 token ,请求字段为空

c# - 如何在 Ubuntu 上为 Asp.Net 5.0 安装 MySql 包?

android - 在android应用程序上以编程方式获取cpu信息

asp.net - 在 .NET Framework 中引用库,而不是在 .NET Standard 中

c# - 如何将 BigInteger 与小数相乘?

c# - 通过 Console/WinForms 中的 ServiceHost 托管 WCF 服务

c# - ServiceStack:访问InsertFilter和UpdateFilter中的 session

c# - 将日志记录代码与 C# 对象分离