add object instance id to log4net log
I am trying to use log4net for distributed logging on a cluster.
My software stack is composed of isolated agents which are spreed around the cluster with unique instance id. I would like to add the instance id of each object who is logging to the log being generated but I want all the inherited classes to use the same value for object instance id where I am following the best practice of adding a static logger to every class of mine and even if a class inherit from a base class, it will have its own logger like below
private static ILog _Log =LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
so I can not use the logger name. Also, I do not want to have to add the object instance id to every log that I make and I would it to be automatically done with logn4net log.
I c开发者_开发技巧an use the logger context and but the instance id is not global and does not belong to an espcific thread. How should I go about this?
You can add Nested Diagnostic Contexts to you business classes which provide context information on what is going on. You log messages will be enriched with the NDC information, for example you can add the current id to NDC context and when you log, the log message will be enriched.
using(NDC.Push("my context message, and my id is:"+object.id))
{
... all log calls will have 'my context message, and my id is:...' included ...
}
精彩评论