SkryptX
SkryptX

Reputation: 1001

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?

Upvotes: 0

Views: 1649

Answers (1)

SkryptX
SkryptX

Reputation: 1001

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.

Upvotes: 1

Related Questions