How do I filter my log file based on categories with .NET Core logging?
I have configured my factory log like so,
loggerFactory
.AddConsole()
.AddDebug()
.AddFile("Logs/trace-{Date}.txt");
and in my constructors I take dependencies on, for example ILogger<MyServiceClass>
. I understand that the framework will instantiate the logger for my service class with the category name based on the fully qualified type name.
I see messages like this in my log file:
2017-07-07T09: 10: 46.5678506 + 02: 00 0HL650T1MS1RU [WRN] Error registering socket for participant b943174e-8022-4e33-ef0d-08d4c4bc4879 in negotiations 42d9720f-32a2-46f9-9042-08d4c4bc4881. (799c4f21)
I seem to remember reading somewhere that the category is hashed to preserve the file size, so I suspect the gap between the timestamp and the log level ( 0HL...1RU
) represents the category, but that may not be searchable or discoverable.
How can I configure the logger to not obfuscate the category names in the log file?
source to share
As per the code, you are using Serilog.Extensions.Logging.File (specific logger implementation is important). This funny string is 0HL...1RU
actually a unique identifier for the request. My guess is that this logger does not support log categories for plain text logs.
Workaround # 1: Using JSON Logs:
loggerFactory.AddFile("filename", isJson: true);
SourceContext
the property will appear in the log that has your log category:
"SourceContext":"MyNamespace.MyServiceClass"
Workaround # 2: Just use another third party file logger like NLog or the full standalone Serilog API .
source to share