Logging 在使用包装器时,如何保留Log4Net的类和方法名以进行日志记录?
我需要一个Log4net包装器-在一个大型应用程序中暴露于许多不同的组件。显然,我希望在记录时保留类和方法名,但我会避免将类型等传递给我的包装器 我看了一眼,它和我的非常相似,但没用 我在smt中看到了如下所示:Logging 在使用包装器时,如何保留Log4Net的类和方法名以进行日志记录?,logging,log4net,wrapper,Logging,Log4net,Wrapper,我需要一个Log4net包装器-在一个大型应用程序中暴露于许多不同的组件。显然,我希望在记录时保留类和方法名,但我会避免将类型等传递给我的包装器 我看了一眼,它和我的非常相似,但没用 我在smt中看到了如下所示: MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); this.log.Debug(methodBase.Name + " : " + message); // define a field such as
MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
this.log.Debug(methodBase.Name + " : " + message);
// define a field such as this
private static readonly Type ThisDeclaringType = typeof(MyLogWrapper);
// in constructor. Note: I use the internal Logger!
this.Logger = LogManager.GetLogger(name).Logger;
// I created a WriteLog method that calls the internal logger like this
// you will need to translate to the internal log levels
// message and ex are the items I want to log (ex can be null)
this.Logger.Log(MyLogWrapper.ThisDeclaringType, log4netLevel, message, ex);
这并不理想,因为它没有使用现成的Log4Net功能
我想先了解一下人们是怎么做的,然后再去做一些非常复杂的事情。感谢您的指点(链接/资源/示例) 我通常会在这个。。。(这是我所需要的全部功能)
您可以始终为Log4Net创建一个包装器,该包装器接受您需要的任何参数(如MethodBase)?Log4Net允许您访问方法名称,例如%method。这不是最快的操作,但是如果您需要调试某些东西,您可以很好地使用它。我相信您的问题是,如果使用包装器,log4net将不会输出正确的方法名 要解决这个问题,您必须了解log4net是如何编写
ILog
实现的。这基本上是内部log4net记录器的包装,因此同样的问题也适用于ILog
实现。我基本上做到了以下几点:
MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
this.log.Debug(methodBase.Name + " : " + message);
// define a field such as this
private static readonly Type ThisDeclaringType = typeof(MyLogWrapper);
// in constructor. Note: I use the internal Logger!
this.Logger = LogManager.GetLogger(name).Logger;
// I created a WriteLog method that calls the internal logger like this
// you will need to translate to the internal log levels
// message and ex are the items I want to log (ex can be null)
this.Logger.Log(MyLogWrapper.ThisDeclaringType, log4netLevel, message, ex);
显然还有一些事情要做,但重要的一点已经提到了
像这样创建您的包装器类
按以下方式配置log4net.config文件
谢谢-我想避免传递类型和方法名。嗨,这在发布模式下失败。它只是在类名和方法名中保存“?”。你面对过这样的问题吗?如果是的话,有什么想法吗?从来没有这个问题。我可以想象编译器正在内联您的方法?是的,Log4Net文档也提出了相同的建议,即它使用System.Diagnostics.StackTrace,在发布模式下工作方式不同:-(我喜欢这个解决方案,但它不适用于静态方法。有什么建议吗?
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="logfile.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
<root>
<!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL -->
<level value="ALL" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
// need to pass this(current obj) as we want to log full class name
LogFourNet.Debug(this, "started something from wrapper");
output:
-------
2017-02-04 15:38:37,549 [1] DEBUG [WebAPI_DI.Startup.Configuration:25] - started something from wrapper