MeTitus
MeTitus

Reputation: 3428

How to use Common.Logging.Log4Net to log all messages grouped

So I am using Common.Logging.Log4Net for logging purposes, but since the code is multithreaded, often messages get mixed up, eg:

message from thread 1 message from thread 2 message from thread 1 message from thread 1 message from thread 2

In order to avoid this, we are trying to get all messages together and we print them all when the execution finishes. For that we basically have a list of Action delegates and each contains the log line, eg:

            Log.DebugFormat("Handling...");

But this unfortunately does not solve any issue, because when we write back to the log, messages still get mixed, since all we do is a foreach on the list and execute the delegates. So is there any way to get all the messages written at once?

Upvotes: 2

Views: 513

Answers (1)

Ken
Ken

Reputation: 844

Sounds like you're looking for some sort of transaction logging. By nature, application logging is sequential.

A couple of options I can think of off-hand:

  • Log the thread id. Use a more appropriate log analyzer when reviewing the output. Excel works fine if you're using CSV or other common format. When you're interested in a certain thread's actions, filter on the thread id. I'd try this route first.
  • Log each thread to a separate file. I've seen Microsoft do this on occasion. Might work ok. Would have to uniquely identify each file somehow. Guessing log4net can handle that.

Upvotes: 2

Related Questions