问题描述
我明白为什么log4net的使用的app.config
文件设置日志记录的 - 这样你就可以很容易地改变了信息的记录,而无需重新编译code。但是,在我的情况下,我不想收拾的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.
有没有办法为我设置登录code,而不是使用的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="Logs\EventLog.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 = @"Logs\EventLog.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;
}
}
}
和那么我要做的就是替换code,我调用了以下调用的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();
这篇关于您可以配置使用配置文件log4net的在code呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!