log4net没有创建要记录的日志文件

时间:2015-11-04 15:36:33

标签: c# logging log4net

log4net没有创建任何要记录的文本文件。我试过这个

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="EpinovaADSyncJob.log" />
  <appendToFile value="false" />
  <rollingStyle value="Composite" />
  <staticLogFileName value="false" />
  <datePattern value="yyyy-MM-dd" />
  <preserveLogFileNameExtension value="true"/>
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
  </layout>
</appender>
<root>
  <level value="INFO"/>
  <appender-ref ref="RollingFileAppender"/>
  <appender-ref ref="ConsoleAppender" />
</root>
</log4net>
</configuration>

在汇编文件中:

[assembly: log4net.Config.XmlConfigurator(
ConfigFile = "Log4Net.config", Watch = true)]

声明为类变量

 private static readonly ILog Log = LogManager.GetLogger(
 MethodBase.GetCurrentMethod().DeclaringType);

在一个名为Log的方法

BasicConfigurator.Configure();
        Log.Debug("Myjob");
        Log.Error("newlkjasdf");
        Log.Info("lkjlkjasdfasdf");

1 个答案:

答案 0 :(得分:0)

试试这个appender:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="log.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>