Log4net配置错误

时间:2012-08-27 11:44:28

标签: c# logging log4net

我正在使用Log4Net来记录我的Web应用程序中的异常。

我在这里找到了一个配置示例:http://www.csharptocsharp.com/log4net-configuration-for-rockin-loggin

<?xml version="1.0"?>

<configuration>
  <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <!--
  This writes the log information to the console window.  It only logs events
  that are at least at the INFO level (which would mean that DEBUG events are not
  captured.
  -->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{ABSOLUTE} [%thread] %level %logger - %message%newlineExtra Info: %property{testProperty}%newline%exception"/>
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO"/>
        <levelMax value="FATAL"/>
      </filter>
    </appender>
    <!--
  This stores information in the log.txt file.  It only captures log events
  that contain the key word test or error.
  -->
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
           <file value="L:\Name\trunk\Name.Web\log.txt"/>
      <appendToFile value="true"/>
      <rollingStyle value="Size"/>
      <maxSizeRollBackups value="5"/>
      <maximumFileSize value="10MB"/>
      <staticLogFileName value="true"/>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="test"/>
      </filter>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="error"/>
      </filter>
      <filter type="log4net.Filter.DenyAllFilter"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception"/>
      </layout>
    </appender>
    <root>
      <level value="FATAL"/>
      <appender-ref ref="RollingFileAppender"/>
    </root>
    <logger name="Log4NetTest.OtherClass">
      <level value="DEBUG"/>
      <appender-ref ref="ConsoleAppender"/>
    </logger>
  </log4net>


</configuration>

在我的班上,我有:

protected static readonly ILog log = LogManager.GetLogger(typeof(TemplateController));

在我的方法中我有:

log4net.Config.XmlConfigurator.Configure();

                //————————–
                log.Error("sadi the great");
                log.Info("sadi the great");

但我有错误:

enter image description here

3 个答案:

答案 0 :(得分:3)

Rolling File appender的工作配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
         <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
             <file value="logger.log" />
             <appendToFile value="true" />
             <maximumFileSize value="100KB" />
             <maxSizeRollBackups value="2" />
             <layout type="log4net.Layout.PatternLayout">
             <conversionPattern value="%date %logger [%thread] [%ndc] (%file:%line) %level- %message%newline"/>
             </layout>
         </appender>

  <root>
      <level value="DEBUG" />      
  <appender-ref ref="RollingFile" />
  </root>
  </log4net>
</configuration>

你应该将目标框架更改为“.net framework 4”(默认情况下,我的情况不同) 检查这个将是有用的

答案 1 :(得分:1)

在AppConfig文件中使用

<log4net debug="true">
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
  <layout type="log4net.Layout.XMLLayout" />
  <param name="File" value="BackgroundCheckLog/BackgroundCheckLog.log" />
  <param name="AppendToFile" value="true" />
  <param name="maximumFileSize" value="1000KB" />
  <param name="maxSizeRollBaenter code here`ckups" value="-1" />
  <layout type="log4net.Layout.PatternLayout">
    <header type="log4net.Util.PatternString" value="[START LOG] %newline" />
    <footer type="log4net.Util.PatternString" value="[END LOG] %newline" />
    <conversionPattern value="%d [%t] %-5p (%file:%line) - %m%n" />
  </layout>
</appender>
<root>
  <level value="ALL" />
  <appender-ref ref="RollingFile" />
</root>

protected static readonly ILog log = LogManager.GetLogger(typeof(TemplateController));

使用

internal static ILog log = LogManager.GetLogger(typeof(TemplateController));

答案 2 :(得分:0)

确保在AssemblyInfo.cs中列出[assembly: log4net.Config.XmlConfigurator]

我发现没有添加该行,所有内容似乎都适用于Log4Net,但没有任何记录。

-Edit-

这是我的Log4Net配置。我使用彩色控制台appender,以及滚动文件appender。

  <log4net>
        <appender name="Console" type="log4net.Appender.ColoredConsoleAppender">

            <mapping>
                <level value="INFO" />
                <foreColor value="Green,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="DEBUG" />
                <foreColor value="Cyan,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="WARN" />
                <foreColor value="Purple,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="ERROR" />
                <foreColor value="Red,HighIntensity"/>
            </mapping>

            <mapping>
                <level value="FATAL" />
                <foreColor value="Yellow,HighIntensity"/>
            </mapping>

            <layout type="log4net.Layout.PatternLayout">
                <!-- Pattern to output the caller's file name and line number -->
                <conversionPattern value="%date,%5level,(%file:%line),%message%newline" />
            </layout>
        </appender>

        <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
            <file value="LOG.csv" />
            <appendToFile value="true" />
            <maximumFileSize value="1024MB" />
            <rollingStyle value="Date" />
            <datePattern value="yyyyMMdd" />

            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date,%5level,(%file:%line),%message%newline"  />
            </layout>
        </appender>

        <root>
            <level value="DEBUG" />
            <appender-ref ref="Console" />
            <appender-ref ref="RollingFile" />
        </root>
    </log4net>

要特别注意我的根元素是如何列出的 - 我认为您的配置文件问题可能存在。

尝试我的配置,看看它是否适合您。如果它仍然无效,则还有其他问题需要解决。