Logging Commons and Mapped Diagnostic Context

What did others do to circumvent the fact that the Commons Logging project (for both .NET and Java) does not support Mapped or nested diagnostic contexts, as far as I know?

+2
source share
4 answers

For completeness, I ended up writing my very simple common interface:

public interface IDiagnosticContextHandler
{
    void Set(string name, string value);
}

then a specific version of Log4Net is implemented:

public class Log4NetDiagnosticContextHandler : IDiagnosticContextHandler
{
    private readonly Assembly assembly;

    private readonly Type mdcType;

    public Log4NetDiagnosticContextHandler()
    {
        this.assembly = Assembly.Load("log4net");
        this.mdcType = this.assembly.GetType("log4net.MDC", true);
    }

    public void Set(string name, string value)
    {
        this.mdcType.InvokeMember("Set", BindingFlags.InvokeMethod, null, null, new object[] { name, value });
    }
}

Then I used the IoC container (Spring.Net) for the correct implementation. If you later needed a different logging structure, it would be easy to write a different implementation of this interface that changes the configuration of IoC.

0
source

Executive Summary:

( log4j).

:

? , , , , (.. .)

( log4j). Commons-Logging classpath, , . , , , log4j, , , /.

  • NDC ?
  • , , , ?

:

  • ?

, - , , org.apache.log4j, , 1. log4e Eclipes ; NDC , ​​. NDC.enter() NDC.leave() , .

</" > 1)

+2

, - , Commons Logging? , SLF4J (Simple Logging Facade - http://www.slf4j.org/) - API , , , .

, , , , , Commons Logging. , (Hibernate, Spring Modules, Apache).

+2

() , .

- 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);

  //Access these context values for output formatting using the event-context:item token
  logEvent.Context["ActivityID"] = MyContext.ActivityID;
  logEvent.Context["SessionID"] = MyContext.SessionID;
  logEvent.Context["TransactionStartTime"] = MyContext.TransactionStartTime;

  //Note that you can also set NDC and/or MDC here:
  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.

0
source

Source: https://habr.com/ru/post/1729588/


All Articles