Yogev
Yogev

Reputation: 141

.Net logger that support hierarchy and thread safety

I have two dlls. both are using log4Net and log.

The first Dll 'A.dll' has an async function that log parallel and has a unique id for each call. This function is using the second dll 'B.dll' which log too.

logger.Log("Is about to enter B function");
B.somefucntion();

The problem is that I have multiple logs from 'A.dll' and 'B.dll' and I can't connect between them.

I can't send the unique id of the function to 'B.dll' (this is an extenral dll that I can't modify).

I though of a solution in which in 'A.dll' opens a scope, and every log in between this scope will be added under this scope + it's message.

logger.Log("Is about to enter B function");
using (logger.Scope("Bla bla " + id))
{
   B.somefucntion();
}

Then each log occur in 'B.dll' will start with "Bla bla + {id}". I already implemented it and it's working.

The problem with this solution is that it's not thread safe, so multiple threads can change the current scope together. Thus the scope as no meaning.

Do you know another solution, that is already implemented/ I need to implement?

Thank you very much.

Upvotes: 1

Views: 392

Answers (2)

code5
code5

Reputation: 4794

If you really want great traceability and hierarchy, try ReflectInsight. https://insightextensions.codeplex.com

Upvotes: 0

Manuel Spezzani
Manuel Spezzani

Reputation: 1041

Have a look at log4net Contexts. They allow to add and remove programmatically properties to your logs: http://logging.apache.org/log4net/release/manual/contexts.html

In particular, have a look at ThreadContext.

Context properties can be accessed by layouts to render the message.

Upvotes: 1

Related Questions