Log4net 封装用法

      在这之前也使用过log4net,但使用起来总感觉不是很舒服,一不留神配置就写错了,配置一个新的日志花很多时间调试,win版和web版还有一些区别,经过公司大神(凯哥)帮助,直接使用封装好的类,简单粗暴,美!

      上代码,一切都安静了。。。。

using System;
using System.Xml;

namespace WinLog4net
{
    public class Log4netHelper
    {       
        static Log4netHelper()
        {
            Init();
        }
        public static void Init()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + @"\log4netConfig.xml";
            XmlDocument xmlDoc = new XmlDocument();

            if (System.IO.File.Exists(path))
            {
                xmlDoc.Load(path);
            }
            else
            {
                #region create XML
                string[] arr = new string[] { "",
                "",
                "  ",
                "    ",
                "    ",
                "    ",
                "    ",
                "    ",
                "    ",
                "    ",
                "    ",
                "      ",
                "    ",
                "    ",
                "      ",
                "      ",
                "    ",
                "  ",
                "  ",
                "    ",
                "    ",
                "  ",
                ""};
                #endregion
                xmlDoc.LoadXml(string.Join<string>(string.Empty, arr));
                xmlDoc.Save(path);
            }

            log4net.Config.XmlConfigurator.Configure(xmlDoc.DocumentElement);

        }
        public static void Info(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("InfoLogger");
            if (log.IsInfoEnabled) log.Info(message);
        }
        
    }
}

 2017.12.5 修改

原来封装在一个类的方法有个小问题,不能得到正确的方法名、函数及正解的代码行数,所以分开使用.效果会更好.
 1 using System;
 2 using System.Xml;
 3 
 4 namespace WebLog4net
 5 {
 6     /// 
 7     /// 原来封装在一个类的方法有个小问题,不能得到正确的方法名、函数及正解的代码行数,所以分开使用.
 8     /// 使用方法:
 9     /// 1.在系统初始化时调用一次:     Log4netHelper.Init();
10     /// 2.在需要使用的声明:           log4net.ILog log = log4net.LogManager.GetLogger("InfoLogger");
11     /// 3.需要写日志的地方调用,4种级别按需调用:
12     ///       log.Info("This is Info messages");
13     ///       log.Error("This is Error messages");
14     ///       log.Warn("This is Warn messages");
15     ///       log.Debug("This is Debug messages");
16     /// 
17     public class Log4netHelper
18     {
19         //static Log4netHelper()
20         //{
21         //    //Init();
22         //}
23         /// 
24         /// 系统初始化时调用一次
25         /// 
26         public static void Init()
27         {
28             string path = AppDomain.CurrentDomain.BaseDirectory + @"\log4netConfig.xml";
29             XmlDocument xmlDoc = new XmlDocument();
30 
31             if (System.IO.File.Exists(path))
32             {
33                 xmlDoc.Load(path);
34             }
35             else
36             {
37                 #region create XML
38                 string[] arr = new string[] { "",
39                 "",
40                 "  ",
41                 "    ",
42                 "    ",
43                 "    ",
44                 "    ",
45                 "    ",
46                 "    ",
47                 "    ",
48                 "    ",
49                 "      ",
50                 "    ",
51                 "  ",
52                 "  ",
53                 "    ",
54                 "    ",
55                 "  ",
56                 ""};
57                 #endregion
58                 xmlDoc.LoadXml(string.Join<string>(string.Empty, arr));
59                 xmlDoc.Save(path);
60             }
61             log4net.Config.XmlConfigurator.Configure(xmlDoc.DocumentElement);
62         }
63         //public static void Info(string message)
64         //{
65         //    log4net.ILog log = log4net.LogManager.GetLogger("InfoLogger");
66         //    log.Info(message);
67         //}
68     }
69 }
 1  protected void Page_Load(object sender, EventArgs e)
 2         {
 3             if (!IsPostBack)
 4             {
 5                 Log4netHelper.Init();
 6             }
 7         }
 8         log4net.ILog log = log4net.LogManager.GetLogger("InfoLogger");
 9         protected void btInfo_Click(object sender, EventArgs e)
10         {
11             log.Info("This is Info messages");
12             log.Error("This is Error messages");
13             log.Warn("This is Warn messages");
14             log.Debug("This is Debug messages");
15         }

此配置会在程序目录生成一个log4netConfig.xml 文件

使用时会生成一个log4net文件夹,日期名称为日志名称.同时可以写出正确的方法名,类,执行的代码行数. webfrom winfrom 通用.

Log4net 封装用法_第1张图片

 

转载于:https://www.cnblogs.com/cxd1008/p/6862356.html

你可能感兴趣的:(Log4net 封装用法)