添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
log4net的配置和简单使用

log4net的配置和简单使用

3 年前 · 来自专栏 一起学.NET

1. 安装log4net

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

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

安装完毕以后, 在 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

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


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>
    <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: 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("调试信息");//记录调试信息