如何让 log4net 根据实例化记录器的类写入文件名?

发布于 2024-09-07 07:40:19 字数 1531 浏览 6 评论 0原文

这是我的困境。我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2024-09-14 07:40:19

也许这个小花絮可能会帮助您:您需要重写 RollingFileAppender 并在修改 File 属性后,调用 ActivateOptions 方法,如下所示:

var myPath = "C:\\";
var log = LogManager.GetLogger(typeof(MySpecificAssembly).Name);
XmlConfigurator.Configure();

var rfa = (RollingFileAppender)LogManager.GetRepository().GetAppenders()
    .First(c => c.Name == "RollingFileAppender");

rfa.File = myPath + typeof(MySpecificAssembly).Name + ".log";
rfa.ActivateOptions();

Maybe this small tidbit might help you out: you need to override the RollingFileAppender and after modifying the File property, invoke the ActivateOptions method, like below:

var myPath = "C:\\";
var log = LogManager.GetLogger(typeof(MySpecificAssembly).Name);
XmlConfigurator.Configure();

var rfa = (RollingFileAppender)LogManager.GetRepository().GetAppenders()
    .First(c => c.Name == "RollingFileAppender");

rfa.File = myPath + typeof(MySpecificAssembly).Name + ".log";
rfa.ActivateOptions();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文