Log4net无法写入文件

时间:2012-08-13 09:20:09

标签: c# asp.net log4net

我正在使用log4net来记录我的项目。我需要在我的应用程序中创建多个日志文件。我能够创建日志文件,但似乎log4net无法写入。

请参阅下面的编码:

public void Execute(IJobExecutionContext context)
{
    strLoadFilePath = @"E:\lewre\excel\stock\";
    strLoadFileName = @"iCenterProductConcept_*.xls";

    strCopyFilePath = @"E:\lewre\excel\stock\Processed\";
    strCopyFileName = @"iCenterProductConceptProcess_" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";

    //--Assign log4net path --
    log4net.GlobalContext.Properties["fileName"] = strCopyFilePath + "GeneralLog_" + DateTime.Now.ToString("yyyyMMddHHmm");
    //log4net.GlobalContext.Properties["errorFileName"] = strCopyFilePath +"ErrorLog_"+ DateTime.Now.ToString("yyyyMMddHHmm");
    log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo("App.config"));
    ////**Assign log4net path **

    logger.Debug("test");
}

Log4net config:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="log4net.Internal.Debug" value="false" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <!--<common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,&#xD;&#xA;                        Common.Logging.Log4Net">
        <arg key="configType" value="INLINE" />
        <arg key="configFile" value="E:\\lewre\LewrerpLog\LewreQuartzLog.log" />
        <arg key="level" value="ALL" />
      </factoryAdapter>
    </logging>
  </common>-->
  <log4net>


<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="%property{fileName}.txt"/>
  <appendToFile value="false"/>
  <rollingStyle value="Size"/>
  <maxSizeRollBackups value="-1"/>
  <maximumFileSize value="1024KB"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %logger - %message%newline"/>
  </layout>

  <filter type="log4net.Filter.PropertyFilter">
    <Key value="Version" />
    <StringToMatch value="1" />
  </filter>
</appender>

  <root>
    <level value="All" />
    <appender-ref ref="FileAppender" />      
  </root>
</log4net>

1 个答案:

答案 0 :(得分:1)

要配置net的日志,您只需要添加

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
[assembly: log4net.Config.Repository()]
namespace MyApp
{
    public class Main
    {
       private static readonly ILog log = LogManager.GetLogger(typeof(Main));
    }
}

这只需要为您的应用程序执行一次。

您可以使用DatePattern创建要编写日志文件的日期模式。

 <appender name="A1" type="log4net.Appender.RollingFileAppender">
    <file value="E:\lewre\excel\stock\Processed\GeneralLog_" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <staticLogFileName value="false" />
    <param name="DatePattern" value="yyyyMMddHHmm'.txt'" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
  </appender>