使用Log4net实现彩色日志

本文关键字:日志 彩色 实现 Log4net 使用 | 更新日期: 2023-09-27 18:11:22

现在我得到错误"未能找到配置节'log4net'在应用程序的.config文件"。这是我在log4net.config文件中的代码:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<root>
<level Value="Info"/>
<level Value="Error"/>
<appender-ref ref="ColoredFileAppender" />
</root>
<appender name="ColoredFileAppender" type="log4net.Appender.RollingFileAppender" >
<file type="log4net.Util.PatternString"/>
<file value="C:'CL2.html" />
<encoding value="utf-8" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%CC" />
<converter>
<name value="CC" />
<type value="Cluster2.ColoredMessageConverter" />
</converter>
<mapping>
<level value="Info" />
<foreColor value="Green" />
</mapping>
<mapping>
<level value="ERROR" />
<backColor value="Red, HighIntensity" />
</mapping>
</layout>
</appender>
</configuration>
</log4net>

我在程序集中包含了这条语句:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

我在程序中包含了这条语句:

protected void Application_start(Object Sender, EventArgs e)
{
    log4net.Config.XmlConfigurator.Configure();
}

我不知道是什么问题。请帮助。

使用Log4net实现彩色日志

您必须使用自定义模式布局来实现这一点,然后将其连接到log4net中。配置文件。您的代码看起来像:

public class ColoredMessageConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        string color = "";
        switch (loggingEvent.Level.Name)
        {
            case "DEBUG":
                color = "green";
                break;
            case "WARN":
            case "INFO":
                color = "white";
                break;
            case "ERROR":
                color = "pink";
                break;
            case "FATAL":
                color = "red";
                break;
        }
        string logToRender = string.Format(" <p style='color:{0}'>{1}</p>", color, loggingEvent.RenderedMessage);
        //Add logToRender to file
        writer.Write(logToRender);
    }
}

在您的log4net。按如下所示配置、连接此转换器。将"Your namespace"替换为正确的名称空间。

<log4net>
<root>
  <level value="DEBUG" />
  <appender-ref ref="ColoredFileAppender" />
</root>
<appender name="ColoredFileAppender" type="log4net.Appender.RollingFileAppender" >
  <file type="log4net.Util.PatternString" value="c:'test.html" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%CC" />
    <converter>
      <name value="CC" />
      <type value="<<Your namespace>>.ColoredMessageConverter" />
    </converter>
  </layout>
</appender>

添加上面建议的内容,下面是添加日期/级别/方法以及该信息的代码

public class LoggerColoredMessageConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        string color = "";
        switch (loggingEvent.Level.Name)
        {
            case "DEBUG":
                color = "#383d41";
                break;
            case "WARN":
                color = "#856404";
                break;
            case "INFO":
                color = "#0c5460;";
                break;
            case "ERROR":
            case "FATAL":
                color = "#721c24";
                break;
        }
        string logToRender = string.Format(" <p style='color:{0}'>{1} {2} {3}" +
                                           " - {4}</p>", color, loggingEvent.TimeStamp, loggingEvent.Level.Name, 
            loggingEvent.LoggerName,
            loggingEvent.RenderedMessage);
        //Add logToRender to file
        writer.Write(logToRender);
    }
}

这样记录:

11/15/2019 1:57:16 PM ERROR ASP.demo_aspx - My ERROR