Log4net xml输出 [英] Log4net xml output
本文介绍了Log4net xml输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想完全控制log4net xml输出.
I want full control over log4net xml output.
如何定制输出模板?
推荐答案
如建议 MrPeregrination,您需要编写一个源自 XmlLayoutBase 的类a>,覆盖 FormatXml 方法和指示您的附加程序将其用作布局:
As suggested by MrPeregrination you need to write a class deriving from XmlLayoutBase, override the FormatXml method and instruct your appender to use it as layout:
class Program
{
static void Main(string[] args)
{
XmlConfigurator.Configure();
ILog log = log4net.LogManager.GetLogger(typeof(Program));
log.Debug("Hello world");
}
}
public class MyXmlLayout : XmlLayoutBase
{
protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
{
writer.WriteStartElement("LogEntry");
writer.WriteStartElement("Message");
writer.WriteString(loggingEvent.RenderedMessage);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
然后在app.config中输入以下内容:
And in app.config put this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="MyNamespace.MyXmlLayout" />
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
这将在您的日志文件中产生类似这样的条目:
This will produce entries like this in your log file:
<LogEntry><Message>Hello world</Message></LogEntry>
这篇关于Log4net xml输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文