如何在 log4net 中记录没有路径的类文件
我希望能够在日志文件中记录类文件和行号,因此我使用以下配置...
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--etc-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level (%file:%line) %logger => %message%newline" />
</layout>
</appender>
但是 %file 属性使我的日志文件条目太长而无法舒适地阅读...
2009-08-07 16:41:55,271 [7] INFO (O:\mystream\aevpallsrv\DotNet\com.mycompany.au\myapp\Myappp\Controller.cs:75) MyApp.Controller => Controller.EnqueueWorkerThreads() - START
有吗一种只显示类文件('Controller.cs')而不是文件的完整路径的方法???
迈克尔
I want to be able to log the class file and line number in my log file so I am using the following config...
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--etc-->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level (%file:%line) %logger => %message%newline" />
</layout>
</appender>
However the %file attribute is making my log file entries waaaay too long to read comfortably....
2009-08-07 16:41:55,271 [7] INFO (O:\mystream\aevpallsrv\DotNet\com.mycompany.au\myapp\Myappp\Controller.cs:75) MyApp.Controller => Controller.EnqueueWorkerThreads() - START
Is there a way to show just the class file ('Controller.cs') instead of the full path to the file also???
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管您询问的是文件名,但我相信您可以使用 %type 来获取完全限定的类型名称 (abclassName)。 如果您只需要类名,请使用 %type{1}
请注意,任何生成调用者信息(%file 和 %type)的方法都会产生与之相关的性能成本。
顺便说一句,您可以通过使用类型名称命名记录器来绕过性能影响。
您的转换模式将如下所示:
您的记录器将是“MyNameSpace.Foo”。 同样,如果您只需要类名,请使用“%logger{1}”,它将解析为“Foo”。
这种方法的最大优点之一是您可以利用 log4net 的分层存储库系统来调整每种类型的日志记录级别:
Although you're asking about the file name, I believe you can use %type to get the fully qualified type name (a.b.className). If you just want the class name, use %type{1}
Note that any method that generates caller information (%file and %type) have a performance cost associated with them.
As an aside, you can bypass the performance hit by naming the Logger using the Type name.
Your conversion pattern would look like:
Where your logger would be "MyNameSpace.Foo". Likewise, if you only want the class name, use "%logger{1}, which will resolve as "Foo".
One of the best advantages to this approach is that you can leverage log4net's hierarchical repository system to adjust logging levels per type:
PatternLayout 开箱即用,仅支持 %file 标记。 您可以做的是子类 PatternLayout 并添加您自己的模式,例如 %filename,并且对于此标记仅输出文件名。
Out of the box, PatternLayout supports only the %file token. What you can do is subclass PatternLayout and add your own pattern, say %filename, and for this token only output the name of the file.