码迷,mamicode.com
首页 > Windows程序 > 详细

从零开始写C# MVC框架之--- 配置log4日志

时间:2018-05-18 13:47:01      阅读:285      评论:0      收藏:0      [点我收藏+]

标签: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     /// <summary>  
 5     /// 日志处理类  
 6     /// </summary>  
 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 <log4net>  
 2     <!--数据日志-->  
 3     <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender">  
 4       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />  
 5       <param name="File" value="Log\Info\" />  
 6       <param name="AppendToFile" value="true" />  
 7       <param name="rollingStyle" value="Date" />  
 8       <param name="datePattern" value="yyyy-MM-dd.‘Info.log‘" />  
 9       <param name="staticLogFileName" value="false" />  
10       <layout type="log4net.Layout.PatternLayout">  
11         <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />  
12       </layout>  
13       <filter type="log4net.Filter.LevelRangeFilter">  
14         <param name="LevelMin" value="INFO" />  
15         <param name="LevelMax" value="INFO" />  
16       </filter>  
17     </appender>  
18     <!--错误日志-->  
19     <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender">  
20       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />  
21       <param name="File" value="Log\Error\" />  
22       <param name="AppendToFile" value="true" />  
23       <param name="rollingStyle" value="Date" />  
24       <param name="datePattern" value="yyyy-MM-dd.‘error.log‘" />  
25       <param name="staticLogFileName" value="false" />  
26       <layout type="log4net.Layout.PatternLayout">  
27         <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />  
28       </layout>  
29       <filter type="log4net.Filter.LevelRangeFilter">  
30         <param name="LevelMin" value="ERROR" />  
31         <param name="LevelMax" value="ERROR" />  
32       </filter>  
33     </appender>  
34     <!--调试日志-->  
35     <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">  
36       <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />  
37       <param name="File" value="Log\Debug\" />  
38       <param name="AppendToFile" value="true" />  
39       <param name="rollingStyle" value="Date" />  
40       <param name="datePattern" value="yyyy-MM-dd.‘debug.log‘" />  
41       <param name="staticLogFileName" value="false" />  
42       <layout type="log4net.Layout.PatternLayout">  
43         <conversionPattern value="%n%n%n 记录时间:%date %n日志级别:  %-5level %n出错类:%logger  %n错误描述:%message %newline%newline" />  
44       </layout>  
45       <filter type="log4net.Filter.LevelRangeFilter">  
46         <param name="LevelMin" value="DEBUG" />  
47         <param name="LevelMax" value="DEBUG" />  
48       </filter>  
49     </appender>  
50     <root>  
51       <level value="DEBUG" />  
52       <!--文件形式记录日志-->  
53       <appender-ref ref="ErrorRollingFileAppender" />  
54       <appender-ref ref="DebugRollingFileAppender" />  
55       <appender-ref ref="InfoRollingFileAppender" />  
56     </root>  
57   </log4net>  
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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!