登录共享和映射的诊断上下文上下文

由网友(活着便精采)分享简介:有其他人做了什么来绕过这一事实下议院测井项目(用于.NET和Java)不支持映射或嵌套的诊断上下文就我知道的?What have others done to get around the fact that the Commons Logging project (for both .NET and Java) d...

有其他人做了什么来绕过这一事实下议院测井项目(用于.NET和Java)不支持映射或嵌套的诊断上下文就我知道的?

What have others done to get around the fact that the Commons Logging project (for both .NET and Java) do not support Mapped or Nested Diagnostic Contexts as far as I know?

推荐答案

为了完整起见,我结束了写我自己非常简单的通用接口:

For the sake of completeness, I ended up writing my own very simple generic interface:

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

然后实施了log4net的特定版本:

then implemented a Log4Net specific version:

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

然后我用一个IoC容器(Spring.Net)带来的正确实施。如果一个不同的日志记录框架后来要求它会是写一个不同的实现,接口改变IOC配置的一个简单的事情。

I then used an IoC container (Spring.Net) to bring in the correct implementation. If a different logging framework was later required it'd be a simple matter of writing a different implementation of that interface changing the IoC configuration.

阅读全文

相关推荐

最新文章