使用Log4net实现彩色日志

时间:2013-04-29 06:40:39

标签: c# selenium log4net

现在我收到错误“无法在应用程序的.config文件中找到配置部分'log4net'。这是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();
}

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

2 个答案:

答案 0 :(得分:5)

您必须使用自定义模式布局来实现此目的,然后将其连接到log4net.config文件中。 您的代码如下:

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.config中,连接这个转换器,如下所示。用正确的名称替换“你的命名空间”。

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

答案 1 :(得分:0)

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

公共类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错误ASP.demo_aspx-我的错误