如何配置log4Net

之前曾经用过几次,但是每次都是用完就忘了,下次再用的时候要baidu半天,这次弄通之后直接记下来。

步骤如下。

1. 安装log4Net,直接用NuGet, Install-Package log4Net

2. 把Log4Net.config这个配置文件加到工程里面,切记要把属性改成"Copy Always"。文件内容如下。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>

  <log4net>

    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Log" />
      <appendToFile value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="2MB" />
      <staticLogFileName value="false" />
      <param name="DatePattern" value="yyyy-MM-dd&quot;.log&quot;"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="[%date]  %thread  %-5level  %message%newline" />
      </layout>
    </appender>

  </log4net>
</configuration>
View Code

3. 在工程里面加一个类,内容如下。

using System;

namespace AutoFlashingTool
{
    class Logger
    {

        log4net.ILog _log = null;

        public Logger()
        {

            _log= log4net.LogManager.GetLogger("default");

        }
        public  void WriteDebug(string msg)
        {

            _log.Debug(msg);

        }

        public  void WriteInfo(string msg)
        {

            _log.Info(msg);

        }

        public  void WriteWarning(string msg)
        {

            _log.Warn(msg);

        }

        public  void WriteError(string msg, Exception ex)
        {

            _log.Error(msg, ex);

        }

    }
}
View Code

4. 在AssemblyInfo.cs文件里面加上一行,告诉应用程序,log4Net的相关配置在log4Net.config这个文件里面

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Media;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("AutoFlashingTool")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("BOSCH")]
[assembly: AssemblyProduct("AutoFlashingTool")]
[assembly: AssemblyCopyright("Copyright © BOSCH 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d9ea3b80-f51a-4b54-8173-a8757d35429e")]

// required to support per-monitor DPI awareness in Windows 8.1+
// see also https://mui.codeplex.com/wikipage?title=Per-monitor%20DPI%20awareness
[assembly: DisableDpiAwareness]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]


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

5. 在代码里实例化Logger这个类,就可以调用方法记录Log了。

原文地址:https://www.cnblogs.com/QiuTianBaBa/p/7220187.html