1. 安装log4net

打开VS 然后 Tools -> NuGet Package Manager -> Manager NuGet Packages for Solution...

image-20200303001309235

浏览 搜索 log4net 然后选择要安装的项目, 点击 install 按钮

image-20200303001520251

安装完毕以后, 在 Solution Explorer 里面可以看到新的文件packages.config, 可以查看安装的 log4net 版本

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="log4net" version="2.0.8" targetFramework="net45" />
</packages>

2. 配置 log4net

2.1 创建 log4net 配置文件 log4net.config

对 solution 右键 -> add -> add new items, 选择 configuration 文件, 文件名改为 log4net.config

image-20200303002220423

log4net.config 文件右键, 打开 Properties, 修改 Copy to Output DirectoryCopy if newer.

image-20200303002349759

2.2 配置 log4net.config 文件

打开 log4net.config 文件, 并修改为以下内容:

<?xml version="1.0" encoding="utf-8"?>
<log4net>
  <!-- 控制台日志配置 -->
  <appender name="Console" type="log4net.Appender.ConsoleAppender">
    <!-- 日志输出格式 -->
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
    </layout>
  </appender>
 
  <!-- 文件存储日志配置 -->
  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <!-- 保存文件的名称 -->
    <file value="D:\log.log" />
    <appendToFile value="true" />
    <!-- 文件的编码方式 -->
    <param name="Encoding" value="UTF-8"/>
    <!-- 每个文件的大小 -->
    <maximumFileSize value="100KB" />
    <!-- 保存文件数量 -->
    <maxSizeRollBackups value="2" />
    <!-- 日志输出格式 -->
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
  </appender>
 
  <root>
    <level value="ALL" />
    <appender-ref ref="Console" />
    <appender-ref ref="RollingFile" />
  </root>
</log4net>

上述配置为 控制台输出 和 文件输出两种日志配置.

2.3 配置 AssemblyInfo.cs

打开Solution 的 ..\Properties\AssemblyInfo.cs, 在最后将 log4net.config 设置给 log4net:

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
 
// 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("Common")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Common")]
[assembly: AssemblyCopyright("Copyright ©  2020")]
[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("b2e1fd9f-3864-4b44-97ee-efa1d225ce53")]
 
// 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")]
 
// 指定log4net 的配置文件
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

3. 测试日志

打开 Program.cs 文件, 修改为以下内容:

namespace Common
{
    public class Program
    {
        private static ILog log = LogManager.GetLogger("Test");
 
        static void Main(string[] args) {
            log.Error("错误", new Exception("发生了一个异常"));//错误
            log.Fatal("严重错误", new Exception("发生了一个致命错误"));//严重错误
            log.Info("信息"); //记录一般信息
            log.Debug("调试信息");//记录调试信息
            log.Warn("警告");//记录警告信息
            Console.ReadKey();
        }
    }
}

运行该文件,得到的结果如下:

image-20200303003042054

同时在在我的电脑 D盘 生成了一个日志文件:

image-20200303003143458

至此, log4net 配置完成.

后记

此文为 log4net 的添加配置 和 简单使用, 更详细的配置等后续使用时在做总结. 也可参考以下文章:

Apache log4net – Apache log4net Manual: Configuration - Apache log4net

Log4Net使用详解 - 技术江湖-小焕 - 博客园

欢迎关注我的公众号获取最新的文章, 或者 移步我的博客: http://blog.devwiki.net

微信公共号

Last modification:March 3rd, 2020 at 12:41 am
If you think my article is useful to you, please feel free to appreciate