1

我正在使用以下日志配置..

.WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(Matching.FromSource<****.Application.Common.Services.Jobs.JobService>()).WriteTo.File("logs/jobs-.log", rollingInterval: RollingInterval.Day))

我正在努力做到这一点,以便记录作业服务调用的任何类。我不能将这些类直接添加到配置中,因为它们是从其他具有高事务量的类中使用的,而且我不想弄乱日志。

无论如何,有没有从配置的调用类中记录子类?

4

1 回答 1

2

Matching.FromSource<****.Application.Common.Services.Jobs.JobService>()表示SourceContext必须****.Application.Common.Services.Jobs.JobService与过滤器完全匹配。

如果您希望过滤器匹配****.Application.Common.Services.Jobs命名空间下的任何内容,则需要基于命名空间应用过滤器:

.WriteTo.Logger(lc => lc.Filter
    .ByIncludingOnly(
        Matching.FromSource(
            typeof(****.Application.Common.Services.Jobs.JobService).Namespace)
        )
    // ...

此过滤器将匹配任何SourceContext****.Application.Common.Services.Jobs


注意:您还需要创建上下文记录器以包含相应的SourceContext属性:

ILogger log = Log.ForContext<****.Application.Common.Services.Jobs.JobService>();
log.Information("...");
于 2021-08-10T19:51:43.313 回答