记录共享和映射的诊断上下文 [英] Logging Commons and Mapped Diagnostic Context
问题描述
据我所知,Commons Logging 项目(适用于 .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.
这篇关于记录共享和映射的诊断上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!