使用包装器时,如何保留 Log4Net 记录的类和方法名称?
我需要一个 Log4net 包装器 - 以便暴露于大型应用程序中的许多不同组件。我显然想在记录时保留类和方法名称,但我会避免将类型等传递给我的包装器。
我查看了这个问题,它与我的非常相似,但这没有帮助。
我已经在 这个其他问题中看到它与 smt 类似以下:
MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
this.log.Debug(methodBase.Name + " : " + message);
这并不理想,因为它没有使用开箱即用的 Log4Net 功能。
在我偏离主题并想出一些非常复杂的东西之前,我想先了解人们是如何做到这一点的。任何指示(链接/资源/示例)表示赞赏!
I need a Log4net wrapper - to be exposed to a number of different components in a large app. I obviously want to retain the class and method name when logging but I would keep away of passing down type etc to my wrapper.
I had a look at this question which is very similar to mine, but it didn't help.
I've seen it done in this other question with smt like the following:
MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod();
this.log.Debug(methodBase.Name + " : " + message);
This is not ideal since it's not using the out-of-the-box Log4Net functionality.
I'd like to get an idea of how people are doing this before I go for the tangent and come up with something very complicated. Any pointers (links/resources/samples) appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Log4net 允许您访问方法名称,例如%method。这不是最快的操作,但如果您需要调试某些内容,则可以很好地使用它。我相信您的问题是如果您使用包装器,log4net 将不会输出正确的方法名称。
要解决这个问题,您必须查看 log4net 如何编写
ILog
实现。这基本上是内部 log4net Logger 的包装,因此同样的问题也适用于 ILog 实现。我基本上做了以下事情:显然还有一些事情要做,但重点已经提到了。
Log4net allows you to access method name for instance like this %method. This is not the fastest operation but if you need to debug something you may very well use it. I believe your question is about the fact that log4net will not output the correct method name if you use a wrapper.
To solve that problem you have to look at how log4net wrote the
ILog
implementation. This is basically a wrapper around the internal log4net Logger and therefore the very same problem applies to theILog
implementation, too. I did basically the following:Obviously there are some things to do yet, but the important points are mentioned.
我通常添加一些东西......(这是我需要的所有功能)
您总是可以为 Log4Net 创建一个包装器,它接受您需要的任何参数(如 MethodBase)?
I usually add something this... (it's all the functionality I need)
you could always create a wrapper for Log4Net that takes any params you need (like MethodBase)?