在这之前也使用过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[] { "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", "<log4net>", " <appender name=\"TastInfo\" type=\"log4net.Appender.RollingFileAppender\">", " <file value=\"log4net\\\" />", " <appendToFile value=\"true\" />", " <rollingStyle value=\"Composite\" />", " <maxSizeRollBackups value=\"-1\" />", " <maximumFileSize value=\"1MB\" />", " <staticLogFileName value=\"false\" />", " <DatePattern value=\"yyyy-MM-dd".txt"\"/>", " <layout type=\"log4net.Layout.PatternLayout\">", " <conversionPattern value=\"%n==========%n【日志级别】%-5level%n【记录时间】%date%n【线程编号】[%thread]%n【执行时间】[%r]毫秒%n【执行行号】%l%n【描述】%message%newline\" />", " </layout>", " <filter type=\"log4net.Filter.LevelRangeFilter\">", " <param name=\"LevelMin\" value=\"INFO\" />", " <param name=\"LevelMax\" value=\"INFO\" />", " </filter>", " </appender>", " <logger name=\"InfoLogger\">", " <level value=\"ALL\"/>", " <appender-ref ref=\"TastInfo\" />", " </logger>", "</log4net>"}; #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 /// <summary> 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 /// </summary> 17 public class Log4netHelper 18 { 19 //static Log4netHelper() 20 //{ 21 // //Init(); 22 //} 23 /// <summary> 24 /// 系统初始化时调用一次 25 /// </summary> 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[] { "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", 39 "<log4net>", 40 " <appender name=\"TastInfo\" type=\"log4net.Appender.RollingFileAppender\">", 41 " <file value=\"log4net\\\" />", 42 " <appendToFile value=\"true\" />", 43 " <rollingStyle value=\"Composite\" />", 44 " <maxSizeRollBackups value=\"-1\" />", 45 " <maximumFileSize value=\"1MB\" />", 46 " <staticLogFileName value=\"false\" />", 47 " <DatePattern value=\"yyyy-MM-dd".txt"\"/>", 48 " <layout type=\"log4net.Layout.PatternLayout\">", 49 " <conversionPattern value=\"%n==========%n[Level]%-5level%n[Data]%date%n[Thread][%thread]%n[Run time][%r]ms%n[Line]%l%n[Message]%message%newline\" />", 50 " </layout>", 51 " </appender>", 52 " <logger name=\"InfoLogger\">", 53 " <level value=\"ALL\"/>", 54 " <appender-ref ref=\"TastInfo\" />", 55 " </logger>", 56 "</log4net>"}; 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 通用.