Reputation: 60634
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?
Upvotes: 3
Views: 1192
Reputation: 1
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}"
);
Upvotes: 0
Reputation: 25039
According to the code, you use Serilog.Extensions.Logging.File (the concrete logger implementation is important). That funny string 0HL...1RU
actually is a unique Request Id. I guess this logger doesn't support log categories for the plain text logs.
Workaround #1: use JSON logs:
loggerFactory.AddFile("filename", isJson: true);
SourceContext
property will appear in the log output having your log category:
"SourceContext":"MyNamespace.MyServiceClass"
Workaround #2: just use another third-party file logger like NLog or full standalone Serilog API.
Upvotes: 2