您可以在代码中配置 log4net 而不是使用配置文件吗? [英] Can you configure log4net in code instead of using a config file?
问题描述
我了解 log4net 为何使用 app.config
文件来设置日志记录 - 这样您就可以轻松更改信息的记录方式,而无需重新编译您的代码.但就我而言,我不想将 app.config
文件与我的可执行文件打包在一起.而且我不想修改我的日志记录设置.
I understand why log4net uses app.config
files for setting up logging - so you can easily change how information is logged without needing to recompile your code. But in my case I do not want to pack a app.config
file with my executable. And I have no desire to modify my logging setup.
有没有办法让我设置登录代码而不是使用 app.config
?
Is there a way for me to set up logging in code rather than using the app.config
?
这是我的简单配置文件:
Here is my simple config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="LogsEventLog.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="5" />
<maximumFileSize value="1GB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="MemoryAppender" type="log4net.Appender.MemoryAppender">
</appender>
<root>
<level value="Info" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="MemoryAppender" />
</root>
</log4net>
</configuration>
完全清楚:我的目标是没有 XML 文件.甚至不是作为我变成流的嵌入式资源.我的目标是完全以编程方式定义记录器.只是好奇是否可能,如果可能的话,我可以在哪里找到语法示例.
To be completely clear: It is my goal to have no XML file. Not even as an embedded resource that I turn into a stream. My goal was to define the logger completely programmatically. Just curious if it's possible and if so where I might find an example of the syntax.
推荐答案
最终解决方案:1
对于将来可能偶然发现此问题的任何人,这就是我所做的.我做了下面的静态类:
For anyone who may stumble upon this in the future, here is what I did. I made the static class below:
using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
namespace Spectrum.Logging
{
public class Logger
{
public static void Setup()
{
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date [%thread] %-5level %logger - %message%newline";
patternLayout.ActivateOptions();
RollingFileAppender roller = new RollingFileAppender();
roller.AppendToFile = false;
roller.File = @"LogsEventLog.txt";
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 5;
roller.MaximumFileSize = "1GB";
roller.RollingStyle = RollingFileAppender.RollingMode.Size;
roller.StaticLogFileName = true;
roller.ActivateOptions();
hierarchy.Root.AddAppender(roller);
MemoryAppender memory = new MemoryAppender();
memory.ActivateOptions();
hierarchy.Root.AddAppender(memory);
hierarchy.Root.Level = Level.Info;
hierarchy.Configured = true;
}
}
}
然后我所要做的就是用以下调用替换我调用 XML 文件的代码:
And then all I had to do was replace the code where I called the XML file with the following call:
//XmlConfigurator.Configure(new FileInfo("app.config")); // Not needed anymore
Logger.Setup();
<小时>
1(此答案已被 OP 编辑为问题,我冒昧地将其作为社区答案,看这里为什么)
这篇关于您可以在代码中配置 log4net 而不是使用配置文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!