The ‘classic’ way I used to attach extra properties to a log message in Serilog was through the LogContext.
From the documentation:
Properties can be added and removed from the context using LogContext.PushProperty()
:
Disadvantage of using the LogContext is that the additional information is only available inside the scope of the specific logcontext(or deeper nested contexts). This typically leads to a larger number of logs which doesn’t always help to find out what is going on.
Today I try to follow a different approach where I only log a single message at the end of an operation. Idea is that the log message is enriched during the lifetime of an operation and that we end up with a single log entry.
This is easy to achieve in Serilog thanks to the IDiagnosticContext interface. The diagnostic context is provides an execution context (similar to LogContext) with the advantage that it can be enriched throughout its lifetime. The request logging middleware then uses this to enrich the final “log completion event”.
More info: https://nblumhardt.com/2019/10/serilog-mvc-logging/