'log4net'用于简单的控制台应用程序

时间:2014-02-14 16:20:14

标签: c# log4net

log4net.Config.BasicConfigurator.Configure();
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));

使用这两行,我的记录器被配置为写入控制台。

如何仅输出文件?

3 个答案:

答案 0 :(得分:0)

使用XmlConfigurator

log4net.Config.XmlConfigurator.Configure(new Uri("path to your file"));

答案 1 :(得分:0)

您可以使用这样的流畅配置:

Dim fileappender = New log4net.Appender.RollingFileAppender()
fileappender.AppendToFile = True
fileappender.Threshold = log4net.Core.Level.Debug
fileappender.File = "MyLogFile_"
fileappender.DatePattern = "yyyyMMddhhmm"
fileappender.StaticLogFileName = False
fileappender.Layout = New log4net.Layout.SimpleLayout()
fileappender.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Date
fileappender.ActivateOptions()
DirectCast(log4net.LogManager.GetRepository(), log4net.Repository.Hierarchy.Hierarchy).Root.AddAppender(fileappender)
log4net.Config.BasicConfigurator.Configure(fileappender)
log.InfoFormat("Hurray. I have configured a rolling file appender via code.")

在此处找到此代码段:http://rajeshsv.blogspot.it/2013/01/configuring-log4net-rolling-file.html

答案 2 :(得分:0)

  <log4net>
    <!--<Appender for Info and Error logging>-->
    <appender name="LogInfoFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="D:\\InfoMode.txt" /> <!--<Please specify the file with path here for log file>-->
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="2" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern"
             value="%-5p – %20C %20M %m%n%n" />
      </layout>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogInfoFileAppender" />
    </root>
  </log4net>

private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()。DeclaringType);

log.Error(“连接失败”);