如何让 log4net 根据实例化记录器的类写入文件名?
这是我的困境。我有一个 RollingFileAppender。我有一个 BufferingForwardingAppender。转发追加器写入文件追加器。我的文件附加器需要一个要写入的文件。我希望该文件被命名为 %Called Assembly Type%log.txt
,其中 %Called Assembly Type% 是被调用的程序集类型(哎呀!)
我打算按如下方式使用它:
从名为“OkilyDokily
”的服务调用:
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
从名为“Neighborinos
”的服务调用:
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
我希望有 2 个日志文件,其中一个名为 OkilyDokilylog.txt 和一个名为
Neighborinoslog.txt
有没有办法在 log4net 中做到这一点?我的 BufferingForwardingAppender 会成为问题吗?我已经尝试过此配置,但 %logger
只是转换为字符串 logger
:(
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="C:\testlogs\%logger log.txt" />
</file>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="5KB" />
<staticLogFileName value="true" />
<countDirection value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-8level %-21date{M/d/yyyy H:m:ss} %message%newline" />
</layout>
</appender>
This is my dilemma. I have a RollingFileAppender
. I have a BufferingForwardingAppender
. The forwarding appender writes to the file appender. My file appender needs a file to write to. I want the file to be named as such %Called Assembly Type%log.txt
, where %Called Assembly Type% is the called assembly type (gasp!)
I'm intending to use this as follows:
Calling from a service called "OkilyDokily
":
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
Calling from a service called "Neighborinos
":
var log4NetLogger = LogManager.GetLogger(GetMyAssemblyName());
log4NetLogger.LogInfo("Toodleoo");
I expect to have 2 log files, one named OkilyDokilylog.txt
and one named Neighborinoslog.txt
Is there a way to do this in log4net? Is my BufferingForwardingAppender
going to be an issue? I have tried this configuration but %logger
is simply translating to the string logger
:(
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString">
<conversionPattern value="C:\testlogs\%logger log.txt" />
</file>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="5KB" />
<staticLogFileName value="true" />
<countDirection value="1"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-8level %-21date{M/d/yyyy H:m:ss} %message%newline" />
</layout>
</appender>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这个小花絮可能会帮助您:您需要重写 RollingFileAppender 并在修改
File
属性后,调用ActivateOptions
方法,如下所示:Maybe this small tidbit might help you out: you need to override the RollingFileAppender and after modifying the
File
property, invoke theActivateOptions
method, like below: