5

现在我收到错误“无法在应用程序.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();
}

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

4

2 回答 2

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>

于 2013-04-29T06:51:43.857 回答
1

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

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);
    }
}

像这样记录它:

2019 年 11 月 15 日下午 1:57:16 错误 ASP.demo_aspx - 我的错误

于 2019-11-15T08:33:16.630 回答