在 C# 中获取 log4net 日志文件
这是我的 log4net 配置:
<log4net>
<appender name="MyLogger" type="log4net.Appender.RollingFileAppender">
<file value="MyLog.log" />
<appendToFile value="true" />
<rollingStyle value="Size"/>
<maxSizeRollBackups value="20"/>
<maximumFileSize value="1000KB"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss},%p,%m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="MyLogger" />
</root>
</log4net>
在 C# 中,我试图获取日志文件的名称(MyLog.log)。我用谷歌搜索并尝试了很多东西,但都失败了。有什么帮助吗?
谢谢!
This is my configuration for log4net:
<log4net>
<appender name="MyLogger" type="log4net.Appender.RollingFileAppender">
<file value="MyLog.log" />
<appendToFile value="true" />
<rollingStyle value="Size"/>
<maxSizeRollBackups value="20"/>
<maximumFileSize value="1000KB"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss},%p,%m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="MyLogger" />
</root>
</log4net>
In C# I'm trying to get the name of the log file (which is MyLog.log). I googled and tried many things but failed to do so. Any help?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据您的情况,解决方案非常简单;只需使用这段代码:
Solution is quite easy in your situation; just use this code:
当有多个文件追加器时,您可能希望通过名称获取它们。另外,为了确保即使根节点没有引用它,也能获取附加程序,以下代码会有所帮助:
When having multiple file appenders, you might want to get them by name. Also to make sure to get the appender even if it is not referenced by the root node, the following code helps:
由于我已经在类中定义了一个记录器,所以我只是使用了它。需要注意的一件事是,可能有多个附加程序,并且通常第一个附加程序是控制台(它没有文件)。这是我的解决方案的价值。
}
Since I already had a logger defined in the class I just used it. One thing to be aware of is that there may be more than one appender and often the first one is the console (which doesn't have a file). Here is my solution for what its worth.
}
如果您的配置没有;节点,那么上述解决方案将不适合你。请继续阅读。
这将检索日志文件:(
希望)防崩溃版本:
最后,如果您遇到 log4net 问题,请将其添加到您的中。部分:
If your config does not have a <root> node then the above solution will not work for you. Read on.
This retrieves the log file:
The (hopefully) crash-proof version:
Finally, if you get stuck with log4net add this to your <appSettings> section:
我没有发现上面的代码有效。
这对我有用
I didn't find the above code working.
This worked for me