1

我正在使用 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 配置:

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

1 回答 1

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>
于 2012-08-13T09:42:14.163 回答