Log4net xml输出

前端之家收集整理的这篇文章主要介绍了Log4net xml输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想完全控制log4net xml输出.

如何自定义输出模板?

作为 suggested由MrPeregrination,您需要编写一个派生自 XmlLayoutBase的课程,覆盖 FormatXml方法,并指示您的appender将其用作布局:
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中放这个:

<?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>

这将在您的日志文件生成这样的条目:

<LogEntry><Message>Hello world</Message></LogEntry>
原文链接:https://www.f2er.com/xml/292953.html

猜你在找的XML相关文章