Serilog child classes not logging when using ByIncludingOnly

Viewed 159

I am using the following log configuration..

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

I am trying to make it so any classes the job service calls get logged. I can't add those classes directly to the configuration because they are used from other classes with a high transaction volume, and I don't want to muddy the log.

Is there anyway to have children classes log from a the calling class that is configured?

1 Answers

Matching.FromSource<****.Application.Common.Services.Jobs.JobService>() means that the SourceContext must be exactly ****.Application.Common.Services.Jobs.JobService to match the filter.

If you want the filter to match anything under the ****.Application.Common.Services.Jobs namespace then you need to apply a filter based on the namespace:

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

This filter will match any SourceContext that starts with ****.Application.Common.Services.Jobs


NB: You also need to create the context logger to include the corresponding SourceContext property:

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