C#中使用Log4记录日志

Posted ZuQing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中使用Log4记录日志相关的知识,希望对你有一定的参考价值。

Log4Net配置还是挺简单的,具体:

1、新建Web或者Webform项目,使用Nuget下载log4net

技术分享

2、新建Config文件夹,将log4net配置文件放入此文件夹中(当修改web.config的时候会重启网站,所以把log4net单独出来比较好,也好管理),log4net配置:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <log4net>
 3   <appender name="fatalAppender" type="log4net.Appender.RollingFileAppender">
 4     <filter type="log4net.Filter.LevelMatchFilter">
 5       <levelToMatch value="FATAL" />
 6     </filter>
 7     <filter type="log4net.Filter.DenyAllFilter" />
 8     <File value="Logs\fatal.log" />
 9     <appendToFile value="true" />
10     <rollingStyle value="Date" />
11     <datePattern value="yyyyMMdd" />
12     <layout type="log4net.Layout.PatternLayout">
13       <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
14     </layout>
15   </appender>
16   <appender name="errorAppender" type="log4net.Appender.RollingFileAppender">
17     <filter type="log4net.Filter.LevelMatchFilter">
18       <levelToMatch value="ERROR" />
19     </filter>
20     <filter type="log4net.Filter.DenyAllFilter" />
21     <File value="Logs\err.log" />
22     <appendToFile value="true" />
23     <rollingStyle value="Date" />
24     <datePattern value="yyyyMMdd" />
25     <layout type="log4net.Layout.PatternLayout">
26       <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
27     </layout>
28   </appender>
29   <appender name="warnAppender" type="log4net.Appender.RollingFileAppender">
30     <filter type="log4net.Filter.LevelMatchFilter">
31       <levelToMatch value="WARN" />
32     </filter>
33     <filter type="log4net.Filter.DenyAllFilter" />
34     <File value="Logs\warn.log" />
35     <appendToFile value="true" />
36     <rollingStyle value="Date" />
37     <datePattern value="yyyyMMdd" />
38     <layout type="log4net.Layout.PatternLayout">
39       <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
40     </layout>
41   </appender>
42   <appender name="infoAppender" type="log4net.Appender.RollingFileAppender">
43     <filter type="log4net.Filter.LevelMatchFilter">
44       <levelToMatch value="INFO" />
45     </filter>
46     <filter type="log4net.Filter.DenyAllFilter" />
47     <File value="Logs\info.log" />
48     <appendToFile value="true" />
49     <rollingStyle value="Date" />
50     <datePattern value="yyyyMMdd" />
51     <layout type="log4net.Layout.PatternLayout">
52       <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
53     </layout>
54   </appender>
55   <appender name="debugAppender" type="log4net.Appender.RollingFileAppender">
56     <filter type="log4net.Filter.LevelMatchFilter">
57       <levelToMatch value="DEBUG" />
58     </filter>
59     <filter type="log4net.Filter.DenyAllFilter" />
60     <File value="Logs\debug.log" />
61     <appendToFile value="true" />
62     <rollingStyle value="Date" />
63     <datePattern value="yyyyMMdd" />
64     <layout type="log4net.Layout.PatternLayout">
65       <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
66     </layout>
67   </appender>
68   <appender name="perfAppender" type="log4net.Appender.RollingFileAppender">
69     <filter type="log4net.Filter.LevelMatchFilter">
70       <levelToMatch value="INFO" />
71     </filter>
72     <filter type="log4net.Filter.DenyAllFilter" />
73     <File value="Logs\perf.log" />
74     <appendToFile value="true" />
75     <rollingStyle value="Date" />
76     <datePattern value="yyyyMMdd" />
77     <layout type="log4net.Layout.PatternLayout">
78       <conversionPattern value="%date %logger - %message%newline" />
79     </layout>
80   </appender>
81   <root>
82     <level value="ALL" />
83     <appender-ref ref="fatalAppender" />
84     <appender-ref ref="errorAppender" />
85     <appender-ref ref="warnAppender" />
86     <appender-ref ref="infoAppender" />
87     <appender-ref ref="debugAppender" />
88   </root>
89 </log4net>

3、右键点击第2步骤的log4net文件属性,改成如图所示

技术分享

在app.config或者web.config中的configuration节点下加入:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

4、在Properties下的AssemblyInfo.cs文件中加入

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

我写了一个log4帮助类方便使用,代码:

 1     public static class Log4Helper
 2     {
 3         public static void Fatal(Type type, object message, Exception exception = null)
 4         {
 5             ILog log = LogManager.GetLogger(type);
 6             if (exception == null)
 7                 log.Fatal(message);
 8             else
 9                 log.Fatal(message, exception);
10         }
11 
12         public static void Error(Type type, object message, Exception exception = null)
13         {
14             ILog log = LogManager.GetLogger(type);
15             if (exception == null)
16                 log.Error(message);
17             else
18                 log.Error(message, exception);
19         }
20 
21         public static void Warn(Type type, object message, Exception exception = null)
22         {
23             ILog log = LogManager.GetLogger(type);
24             if (exception == null)
25                 log.Warn(message);
26             else
27                 log.Warn(message, exception);
28         }
29 
30         public static void Info(Type type, object message, Exception exception = null)
31         {
32             ILog log = LogManager.GetLogger(type);
33             if (exception == null)
34                 log.Info(message);
35             else
36                 log.Info(message, exception);
37         }
38 
39         public static void Debug(Type type, object message, Exception exception = null)
40         {
41             ILog log = LogManager.GetLogger(type);
42             if (exception == null)
43                 log.Debug(message);
44             else
45                 log.Debug(message, exception);
46         }
47     }

5、调用:

1             Log4Helper.Fatal(this.GetType(), "Fatal");
2             Log4Helper.Error(this.GetType(), "Error", new Exception("error"));
3             Log4Helper.Warn(this.GetType(), "Warn");
4             Log4Helper.Info(this.GetType(), "Info");
5             Log4Helper.Debug(this.GetType(), "Debug");

在根目录会有一个Log文件夹,里面:技术分享

 

以上是关于C#中使用Log4记录日志的主要内容,如果未能解决你的问题,请参考以下文章

C# 使用Log4Net记录程序日志

C#中使用Log4记录日志

c#中使用log4net工具记录日志

如何使用 C# 中的 log4net 记录日志

C#上位机开发(十三)—— 使用Log4net添加日志记录功能

C#上位机开发(十三)—— 使用Log4net添加日志记录功能