0

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.

Farzad
  • 1,770
  • 4
  • 26
  • 48
  • 1
    why not create your own context that your log event enricher reads? – Daniel A. White Aug 22 '19 at 15:07
  • I'm not sure I follow, can you elaborate a bit? – Farzad Aug 22 '19 at 15:10
  • Can you give an example of some business logic that has internal context you want to log? Also, do your libraries contain data access implementations or are they purely domain/business code? – rob.earwaker Aug 22 '19 at 17:30
  • Wrong language but in https://github.com/jet/equinox I make the libs always take an ILogger and then layer on the values with the `.ForContext()` overloads on it - i.e. not depending on LogContext in the lib. Then a given transient or singleton object can pass in a logger (or `Log.Logger`). – Ruben Bartelink Aug 22 '19 at 20:23

0 Answers0