Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to read Serilog's LogContext later in a downstream class?

I'm pushing values to the Serilog LogContext in a middleware class of a .NET Core web app:-

using LogContext.PushProperty("MyAct", "some clever joke") ...

Is it possible to read the value for MyAct property in a downstream class? I don't see anything promising on the LogContext class - seems like a write only entity, but maybe I am missing something?

like image 804
AngryHacker Avatar asked Sep 07 '25 15:09

AngryHacker


1 Answers

Serilog does not provide for you to Just Read the values from the LogContext.

Normally the PushProperty stashes it, and Enrich.FromLogContext is charged with grabbing the contextual information that will accompany the destructured message and tokens as the logging call is capturing them into the LogEvent that'll then be passed to the chain of sinks.

Really this begs the question: why do you feel you need to do this?


Actually answering the question: It is possible the underlying APIs are exposed in a usable fashion though; I'd go to https://github.com/serilog/serilog - the LogContext impl and tests are all in that core repo.

like image 191
Ruben Bartelink Avatar answered Sep 10 '25 18:09

Ruben Bartelink