从零开始写C# MVC框架之--- 配置log4日志
标签:pat closed text .net 12c val jsb 从零开始 tps
在框架中配置日志分2步,一个是在帮助项目Zy.Utilities--Zy.Utility.Core中新建log类,封装写入日志方法,还需要在Zy.Utility.Core添加 log4net 的引用
1、log实现类
1 [assembly: log4net.Config.XmlConfigurator(Watch = true)]
2 namespace Zy.Utility.Core
3 {
4 ///
5 /// 日志处理类
6 ///
7 public static class Log
8 {
9 public static void Error(string msg)
10 {
11 log4net.ILog log = log4net.LogManager.GetLogger(msg);
12 log.Error(msg);
13 }
14
15 public static void Info(string msg)
16 {
17 log4net.ILog log = log4net.LogManager.GetLogger(msg);
18 log.Info(msg);
19 }
20
21 public static void Warning(string msg)
22 {
23 log4net.ILog log = log4net.LogManager.GetLogger(msg);
24 log.Warn(msg);
25 }
26
27 public static void Error(string msg, Exception ex)
28 {
29 log4net.ILog log = log4net.LogManager.GetLogger(msg);
30 log.Error("Error", ex);
31 }
32
33 public static void Info(string msg, Exception ex)
34 {
35 log4net.ILog log = log4net.LogManager.GetLogger(msg);
36 log.Info("Info", ex);
37 }
38
39 public static void Warning(string msg, Exception ex)
40 {
41 log4net.ILog log = log4net.LogManager.GetLogger(msg);
42 log.Warn("Warning", ex);
43 }
44 }
45 }
View Code
2、在Web项目的web.config中配置log4,配置完后,启动项目后会在ZyCommon-ZyWeb项目中生成一个Log的文件夹
1 2
3 "InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">
4 "log4net.Appender.FileAppender+MinimalLock" />
5 "File" value="Log\Info\" />
6 "AppendToFile" value="true" />
7 "rollingStyle" value="Date" />
8 "datePattern" value="yyyy-MM-dd.‘Info.log‘" />
9 "staticLogFileName" value="false" />
10 "log4net.Layout.PatternLayout">
11 "%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
12 13 "log4net.Filter.LevelRangeFilter">
14 "LevelMin" value="INFO" />
15 "LevelMax" value="INFO" />
16 17
18
19 "ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">
20 "log4net.Appender.FileAppender+MinimalLock" />
21 "File" value="Log\Error\" />
22 "AppendToFile" value="true" />
23 "rollingStyle" value="Date" />
24 "datePattern" value="yyyy-MM-dd.‘error.log‘" />
25 "staticLogFileName" value="false" />
26 "log4net.Layout.PatternLayout">
27 "%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
28 29 "log4net.Filter.LevelRangeFilter">
30 "LevelMin" value="ERROR" />
31 "LevelMax" value="ERROR" />
32 33
34
35 "DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
36 "log4net.Appender.FileAppender+MinimalLock" />
37 "File" value="Log\Debug\" />
38 "AppendToFile" value="true" />
39 "rollingStyle" value="Date" />
40 "datePattern" value="yyyy-MM-dd.‘debug.log‘" />
41 "staticLogFileName" value="false" />
42 "log4net.Layout.PatternLayout">
43 "%n%n%n 记录时间:%date %n日志级别: %-5level %n出错类:%logger %n错误描述:%message %newline%newline" />
44 45 "log4net.Filter.LevelRangeFilter">
46 "LevelMin" value="DEBUG" />
47 "LevelMax" value="DEBUG" />
48 49
50 51 "DEBUG" />
52
53 ref ref="ErrorRollingFileAppender" />
54 ref ref="DebugRollingFileAppender" />
55 ref ref="InfoRollingFileAppender" />
56 57
View Code
Log文件夹下分为Debug、Error、Info文件夹,分别写入错误级别不同的日志信息
开发中报错后,错误信息会写入Error文件夹中,按配置规则每天生成一个txt文件夹,调试时也可以写入日志中
从零开始写C# MVC框架之--- 配置log4日志
标签:pat closed text .net 12c val jsb 从零开始 tps
原文地址:https://www.cnblogs.com/SUXIAOSHUAI/p/9055208.html
评论