() , .
- Common.Logging(NET), ( 2.0) . , . 2.0 2009 . - , "". . 2009 2010 .
, , , log4net/NLog git. ExtendedLog4netLogger, GlobalContextProperties, ThreadContextProperties, ThreadContextStack ThreadContextStacks, , Castle NDC MDC. Castle for NLog.
Common.Logging NET , , LogManager, :
ILog logger = LogManager.GetCurrentClassLogger();
logger.ThreadContextProperties["EventID"] = 123;
logger.GlobalContextPropeties["USER"] = GetUser();
, , (, , ), log4net / NLog Common.Logging, . , , "", , "". , , , "MyContext".
WriteInternal (, Common.Logging) ( NLog. log4net ):
protected override void WriteInternal(CommonLoggingLogLevel logLevel, object message, Exception exception)
{
LogLevelNLog level = GetLevel(logLevel);
LogEventInfo logEvent = new LogEventInfo(level, _logger.Name, null, "{0}", new object[] { message }, exception);
logEvent.Context["ActivityID"] = MyContext.ActivityID;
logEvent.Context["SessionID"] = MyContext.SessionID;
logEvent.Context["TransactionStartTime"] = MyContext.TransactionStartTime;
NLog.MDC.Set("SoftwareVersion", MyContext.Version.ToString());
_logger.Log(declaringType, logEvent);
}
If you (or someone else) uses Common.Logging (NET), the log4net / NLog context properties are now available, you can do something similar to what Castle did to expose them through the registration paragraph interface. If you want to automatically establish a context with information that you can get from an internal call, you can do something similar to what I suggest above.