use*_*173 4 c# logging serilog asp.net-core asp.net-core-2.0
我想要几个日志文件,我正在尝试找到一种排除多个来源的方法。这是我的代码:
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();
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我有子记录器:首先只收集 hangfire 日志,第二个收集除 hangfire 之外的所有日志。但是,我想添加另一个排除过滤器,例如,我想排除 hangfire 和IMyClass日志。我该怎么做?
Matching.FromSource()从LogEventto返回一个函数bool。您可以利用它来创建两个过滤器函数:
var hangfire = Matching.FromSource("Hangfire");
var myClass = Matching.FromSource("MyClass");
Run Code Online (Sandbox Code Playgroud)
然后内联调用它们:
.WriteTo.Logger(l => l.Filter.ByExcluding(le => myClass(le) || hangfire(le))
.WriteTo.Async(a => a.RollingFile("Logs/main-{Date}.txt"))
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1634 次 |
| 最近记录: |