What is the best way to configure logging for multiple threads in c#?

Viewed 299

I have a .net core app that reads data from a Kafka topic and processes that data. The processor is implemented as a BackgroundService and works perfectly.

Recently, I've changed that BackgroundService to start up multiple threads to allow each thread to process a couple of kafka partitions. This also works great.

The application logs to the eventlog. I've created a new EventSource like this:

 EventLog.CreateEventSource(eventlogSettings.SourceName, eventlogSettings.LogName);

My logging configuration is:

  "EventLog": {
      "LogLevel": {
        "Default": "Information"
      },
      "LogName": "MyService",
      "SourceName": "MyService"
    }

My question is:

How can I change my logging so that I can easily trace what a single thread under that BackgroundService did?

For example, I'd like to be able to follow the logic that a specific thread followed by looking at the series of events that occurred in that thread.

It would be ok if I could just filter on the threadId, but ideally I'd like something more intuitive, since there are other threads in the system too (not just the processing threads) - so it would be tricky to establish which thread ids were processing threads and which belonged to some other background service or task.....

I guess there are 2 questions baked into this one:

  1. How should I set up my eventsource to handle multiple threads/contibutors? (or is my current config good enough)

  2. How do I tell the ILogger to indicate a specific thread/contibutor? (I'm using dependency injection (ILogger), so by default I should be getting a standard ILogger, but there will be nothing on that logger indicating which thread it belongs to?

0 Answers
Related