C
C#2mo ago
Kiel

Enabling debug/trace logging only for certain parts of code as needed?

Let's say I have services A, B, C, and D, each of which log at different levels (Error, Warn, Info, Debug). By default my logging is set up so that if DEBUG is set (Debugging in Rider/VS), the minimum logging level is set to Debug, and it's not if it's not. When published, services A, B, C, and D will never log their Debug level messages. Now let's say I determine that service D is producing issues in a way that I could only reasonably diagnose with debug logs. How do I go about enabling Debug-level logging for ONLY service D? I'd like to avoid having to re-compile. Ideally, maybe something I could include in a config file - I am already utilizing a config.json and AddConfiguration pattern for other services. Part of the reason I can only properly diagnose these issues is that some of these chance encounters with bugs only occur as infrequently as once every 21 days, so just running it in Debug mode on my PC for that long would be...very annoying. And if I simply have it logging even in production, I can search the generated logs for debug output and see if I can catch on to anything. I'm using Serilog with MS.Ext.Logging's strongly-typed(?) loggers if it matters.
4 Replies
Sossenbinder
Sossenbinder2mo ago
You can usually configure minimum log levels either globally or for a specific logging context via configuration, e.g. appsettings.json E.g. https://github.com/serilog/serilog-settings-configuration can pull it in for serilog
Kiel
KielOP2mo ago
Looking at the link you set that might be helpful/useful to me. I'm having trouble finding an example on that page about setting it for a specific logging context. I've barely done advanced stuff with logging before, only an enricher once, so I'm a bit OOTL on terminology or what that would even look like

Did you find this page helpful?