简体   繁体   中英

Serilog Dynamically enrich a context

Is there any possibility to dynamically override or extend the log template in Serilog?

I have a parser class which parses XML line by line and I want to extend the log entry with the line number. I have already written an enricher that does that, but I have to globally override the default logger at the start of the function and reset it at the end, which is difficult to even look at (from a programming perspective):D

What are the other options with Serilog or have I missed something?

After some more digging I finally found a solution that fit my needs.

I used the LogContext.PushProperty mechanism to push the XmlReader (or rather a wrapper object, because the type-matching with the destructuring is exact and XmlReader is a factory which returns many different classes) into the LogContext and added a custom destructuring to LoggerConfiguration to detect objects of that wrapper type and then format it to my liking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM