winform 集成 log4net

1.引入庫log4net.dllapp

2.展開項目文件下的Properties文件夾,打開AssemblyInfo.cs並在AssemblyInfo.cs中添加一行:在AssemblyInfo.cs中添加一行:(其中log4net.config對應配置文件名)spa

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

3.添加log4net.config配置文件: 插件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
  </configSections>
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log\logfile.log"/>
      <appendToFile value="true"/>
      <rollingStyle value="Composite"/>
      <datePattern value="yyyyMMdd"/>
      <maxSizeRollBackups value="10"/>
      <maximumFileSize value="1MB"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
      </layout>
    </appender>
    <root>
      <level value="All"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
</configuration>

4.添加一個公共的日誌管理類AppLog.csdebug

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using log4net.Config;
using System.IO;
 
 
namespace log4net
{
    /// <summary>
    /// 使用Log4net插件的log日誌對象
    /// </summary>
    public static class AppLog
    {
        private static ILog log;
 
        static AppLog()
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
            log = LogManager.GetLogger(typeof(AppLog));
        }
 
        public static void Debug(object message)
        {
            log.Debug(message);
        }
 
        public static void DebugFormatted(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }
 
        public static void Info(object message)
        {
            log.Info(message);
        }
 
        public static void InfoFormatted(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }
 
        public static void Warn(object message)
        {
            log.Warn(message);
        }
 
        public static void Warn(object message, Exception exception)
        {
            log.Warn(message, exception);
        }
 
        public static void WarnFormatted(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }
 
        public static void Error(object message)
        {
            log.Error(message);
        }
 
        public static void Error(object message, Exception exception)
        {
            log.Error(message, exception);
        }
 
        public static void ErrorFormatted(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }
 
        public static void Fatal(object message)
        {
            log.Fatal(message);
        }
 
        public static void Fatal(object message, Exception exception)
        {
            log.Fatal(message, exception);
        }
 
        public static void FatalFormatted(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }
    }
}

在任何你想寫日誌的地方使用,例如:日誌

AppLog.Info("Info log");
AppLog.Error("Error log");

注意:當運行正常沒有建立日誌文件或者log = LogManager.GetLogger(typeof(AppLog))中log對象字段值爲false時,右擊log4net.config選擇屬性-->高級把複製到項目的值改成始終複製code

這樣方式在debug模式下能夠輸出,可是可能在release模式下不輸出,官網提示:orm

using Com.Foo;

// Import log4net classes.
using log4net;
using log4net.Config;

public class MyApp 
{
    // Define a static logger variable so that it references the
    // Logger instance named "MyApp".
    private static readonly ILog log = LogManager.GetLogger(typeof(MyApp));

    static void Main(string[] args) 
    {
        // Set up a simple configuration that logs on the console.
        BasicConfigurator.Configure();

        log.Info("Entering application.");
        Bar bar = new Bar();
        bar.DoIt();
        log.Info("Exiting application.");
    }
}

如上,就能夠在release模式下輸出了。xml

相關文章
相關標籤/搜索