1

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?

Mardo
  • 117
  • 1
  • 9

1 Answers1

2

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("...");
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • This doesn't work because the classes being called are in another namespace, or aren't necessary children of the namepsace name. – Mardo Aug 13 '21 at 22:14
  • Your question says "children classes log from a the calling class" (sic) - classes that are not in the same namespace are _not_ "children classes". I don't think it's clear what your goal is. – C. Augusto Proiete Aug 13 '21 at 22:19