Using Serilog contextual without dependency

224 Views Asked by At

I think the title could be a bit vague, but I couldn't come up with a better title.

My use case is this: I have a Web API project, which is using some "library" projects, taking care of business logic, and I want to use Serilog and its LogContext to put some properties so that I can track actions.

The issue that I have is that in my entry-point (Web API project), I don't have those properties to be able to do something like this:

using (LogContext.PushProperty("MyProperty", "my-value"))
{
  DoStuff();
}

It's inside my "library" projects that I have the values for those properties, but I feel that my business logic shouldn't necessarily depend on Serilog.Context to be able to add the property.

What I'm trying to say is that dependency on Serilog should ideally be in my entry-point project, and my business logic should depend on an interface of some kind.

from here, I know that ILogEventEnricher exists, but firstly it would imply the dependency on Serilog in my business logic. Furthermore, it would need to know about the property it's trying to set, but given that I need to add it in the beginning and in my entry-point project like this:

Log.Logger = new LoggerConfiguration()
    .Enrich.With(new MyLogEnricher(/* I need to pass the "not-yet-known" object here*/);

which means I'm back to my original problem.

So, it seems that I'm facing a dilemma here that I don't know how to solve. Maybe my thought process is moving in the wrong direction, but anyway, I would appreciate any help.

0

There are 0 best solutions below