【问题标题】:Serilog child classes not logging when using ByIncludingOnlySerilog 子类在使用 ByIncludingOnly 时不记录
【发布时间】:2021-08-10 17:21:44
【问题描述】:

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

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

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

是否有从配置的调用类记录子类?

【问题讨论】:

    标签: serilog serilog-sinks-file


    【解决方案1】:

    Matching.FromSource&lt;****.Application.Common.Services.Jobs.JobService&gt;() 表示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)
            )
        // ...
    

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


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

    ILogger log = Log.ForContext<****.Application.Common.Services.Jobs.JobService>();
    log.Information("...");
    

    【讨论】:

    • 这不起作用,因为被调用的类在另一个命名空间中,或者不是命名空间名称的必要子类。
    • 您的问题是“子类从调用类记录”(原文如此) - 不在同一命名空间中的类是 not“子类”。我认为你的目标不是很清楚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多