How do I filter my log file based on categories with .NET Core logging?

Viewed 653

I've configured my logger factory like so,

loggerFactory
    .AddConsole()
    .AddDebug()
    .AddFile("Logs/trace-{Date}.txt");

and in my constructors I take dependencies on e.g. ILogger<MyServiceClass>. It is my understanding that the framework will then create a logger instance for my service class, with a category name based on the full name of the type.

In my log file, I see messages like this:

2017-07-07T09:10:46.5678506+02:00 0HL650T1MS1RU [WRN] Socket registration failed for participant b943174e-8022-4e33-ef0d-08d4c4bc4879 in negotiation 42d9720f-32a2-46f9-9042-08d4c4bc4881. (799c4f21)

I seem to remember reading somewhere that the category is hashed to save on file size, so I suspect the mouthful between the timestamp and the log level (0HL...1RU) represents the category, but that's not really searchable or discoverable.

How do I configure my logger to not obfuscate the category names in the log file?

2 Answers

You can add the log category by using {SourceContext} in the outputTemplate, like so:

loggerFactory
    .AddConsole()
    .AddDebug()
    .AddFile(
        "Logs/trace-{Date}.txt",
        , outputTemplate: "{Timestamp:o} {RequestId,13} [{Level:u3}] {SourceContext} {Message} ({EventId:x8}){NewLine}{Exception}"
    );
Related