关键字:日志操作、类静态实例
今日主旋律是bug。 先是udp的异步接收bug。再是一个每当日期变更NMonitorSystem程序崩溃的bug。不过还好.net的异常系统事件可以捕捉到即使没有代码也可以定位到错误源。顺便说一句很多时候.net异常很难确定,即使进入调试没源码看到的也只是汇编代码很难定位异常。这时候才发现debug程序的意义。
这个异常出现很奇葩,1是因为这个库用了4年了再很多项目中用到,甚至之前的银行程序。2出现的条件是刚好在日期变化是需要日志记录捕捉的程序异常。3log普通的异常没有调用到之前。
Logger类代码(修改日期变更bug)
1 using System; 2 using System.IO; 3 using System.Configuration; 4 5 namespace KS.Global 6 { 7 ///8 /// Logger 的摘要说明。 9 /// 仅可使用DB、Comm和默认log实例 10 /// 11 public class Logger 12 { 13 //定义不同类型日志对象 14 //主业务流程日志 15 private static Logger Steplogger = null; 16 //控件日志 17 private static Logger Controllogger = null; 18 //通信日志 19 private static Logger Commlogger = null; 20 //业务组件日志:记录功能函数实现情况 21 private static Logger Codelogger = null; 22 //后台数据日志 23 private static Logger Datalogger = null; 24 //监控业务日志 25 private static Logger Monitorlogger = null; 26 //流水日志 27 private static Logger Journallogger = null; 28 //脚本日志 29 private static Logger Scriptlogger = null; 30 private static Logger logger = null; 31 private string filePath = ""; //用来记录当前日志文件 32 private StreamWriter sw = null; 33 private bool recordLog = false; 34 private string datetime = null; 35 private string logname = ""; 36 37 private Logger() //禁止直接创建实例 38 { 39 } 40 41 public void close() //关闭日志,释放资源 42 { 43 try 44 { //如果前面的日志还没关闭,则关闭 45 this.sw.Close(); 46 } 47 catch (Exception e) 48 { 49 Console.WriteLine(e); 50 } 51 } 52 private const int DEBUG = 0; 53 private const int WARNING = 1; 54 private const int ERROR = 2; 55 private const int INFO = 3; 56 /** 57 * 记录日志级别 58 * 0 DEBUG 59 * 1 WARNING 60 * 2 ERROR 61 * 3 INFO 62 * 如果需要更改记录日志的级别,只需要修改本字段的取值即可 63 **/ 64 private int level = 0; 65 /** 66 * 获取日志类实例 67 **/ 68 public static Logger getLogger() 69 { 70 if (logger == null) 71 { 72 logger = new Logger(); 73 // string recordlogvalue =ConfigurationSettings.AppSettings["RecordLog"]; 74 // if (recordlogvalue.ToUpper().Equals("YES")) 75 // { 76 // logger.recordLog = true; 77 // } 78 // else 79 // { 80 // logger.recordLog = false; 81 // } 82 83 logger.recordLog = true; 84 logger.datetime = DateTime.Now.ToString("u").Substring(0, 10); 85 string sDir = UtilTool.GetExecutePath(); 86 logger.config(sDir + "LOG\\" + logger.datetime + ".log"); 87 } 88 return logger; 89 } 90 ///91 /// 日志分类型,分别new不同类型对象 92 /// 93 /// 94 ///95 public static Logger getLogger(string LogType) 96 { 97 switch (LogType) 98 { 99 100 case "Control":101 {102 if (Controllogger == null)103 {104 Controllogger = new Logger();105 Controllogger.recordLog = true;106 Controllogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);107 string sDir = UtilTool.GetExecutePath();108 string path = sDir + "LOG\\" + LogType;109 if (!Directory.Exists(path))110 {111 Directory.CreateDirectory(path);112 }113 Controllogger.config(path + "\\" + Controllogger.datetime + ".log");114 }115 return Controllogger;116 }117 case "Step":118 {119 if (Steplogger == null)120 {121 Steplogger = new Logger();122 Steplogger.recordLog = true;123 Steplogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);124 string sDir = UtilTool.GetExecutePath();125 string path = sDir + "LOG\\" + LogType;126 if (!Directory.Exists(path))127 {128 Directory.CreateDirectory(path);129 }130 Steplogger.config(path + "\\" + Steplogger.datetime + ".log");131 }132 return Steplogger;133 }134 135 case "Script":136 {137 if (Scriptlogger == null)138 {139 Scriptlogger = new Logger();140 Scriptlogger.recordLog = true;141 Scriptlogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);142 string sDir = UtilTool.GetExecutePath();143 string path = sDir + "LOG\\" + LogType;144 if (!Directory.Exists(path))145 {146 Directory.CreateDirectory(path);147 }148 Scriptlogger.config(path + "\\" + Scriptlogger.datetime + ".log");149 }150 return Scriptlogger;151 }152 case "Code":153 {154 if (Codelogger == null)155 {156 Codelogger = new Logger();157 Codelogger.recordLog = true;158 Codelogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);159 string sDir = UtilTool.GetExecutePath();160 string path = sDir + "LOG\\" + LogType;161 if (!Directory.Exists(path))162 {163 Directory.CreateDirectory(path);164 }165 Codelogger.config(path + "\\" + Codelogger.datetime + ".log");166 }167 return Codelogger;168 }169 case "DB":170 {171 if (Datalogger == null)172 {173 Datalogger = new Logger();174 Datalogger.recordLog = true;175 Datalogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);176 string sDir = UtilTool.GetExecutePath();177 string path = sDir + "LOG\\" + LogType;178 179 if (!Directory.Exists(path))180 {181 Directory.CreateDirectory(path);182 }183 Datalogger.config(path + "\\" + Datalogger.datetime + ".log");184 }185 return Datalogger;186 }187 case "Monitor":188 {189 if (Monitorlogger == null)190 {191 Monitorlogger = new Logger();192 Monitorlogger.recordLog = true;193 Monitorlogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);194 string sDir = UtilTool.GetExecutePath();195 string path = sDir + "LOG\\" + LogType;196 if (!Directory.Exists(path))197 {198 Directory.CreateDirectory(path);199 }200 Monitorlogger.config(path + "\\" + Monitorlogger.datetime + ".log");201 }202 return Monitorlogger;203 }204 case "Journal":205 {206 if (Journallogger == null)207 {208 Journallogger = new Logger();209 Journallogger.recordLog = true;210 Journallogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);211 string sDir = UtilTool.GetExecutePath();212 string path = sDir + "LOG\\" + LogType;213 if (!Directory.Exists(path))214 {215 Directory.CreateDirectory(path);216 }217 Journallogger.config(path + "\\" + Journallogger.datetime + ".log");218 }219 return Journallogger;220 }221 case "Comm":222 {223 if (Commlogger == null)224 {225 Commlogger = new Logger();226 Commlogger.recordLog = true;227 Commlogger.datetime = DateTime.Now.ToString("u").Substring(0, 10);228 string sDir = UtilTool.GetExecutePath();229 string path = sDir + "LOG\\" + LogType;230 if (!Directory.Exists(path))231 {232 Directory.CreateDirectory(path);233 }234 Commlogger.config(path + "\\" + Commlogger.datetime + ".log");235 }236 return Commlogger;237 }238 default:239 {240 getLogger();241 return logger;242 }243 }244 245 }246 247 /**248 * 配置日志路径及级别249 * 由本方法关闭前面的日志并打开新日志250 **/251 public void config(string filePath)252 {253 try254 { //如果前面的日志还没关闭,则关闭255 if(sw!= null)256 this.sw.Close();257 }258 catch (Exception e)259 {260 }261 262 this.level = level;263 this.filePath = filePath;264 265 try266 {267 this.sw = new StreamWriter(this.filePath, true, System.Text.Encoding.Default);268 this.sw.WriteLine();269 }270 catch (Exception e)271 {272 Console.WriteLine(e);273 //日志文件打开失败,放弃日志274 }275 }276 277 /**278 * 写日志的基础方法279 * 如果要改为写数据库或者其它流,只需更改本方法280 **/281 private void log(string strLog)282 {283 if (this.sw != null)284 {285 string now = DateTime.Now.ToString("u").Substring(0, 10);286 if (!now.Equals(datetime))287 {288 ///日期发生变化,重新打开新文件,记录日志289 ///290 datetime = now;291 string sDir = UtilTool.GetExecutePath();292 if (logger != null)293 logger.config(sDir + "LOG\\" + datetime + ".log");294 if (Datalogger != null)295 Datalogger.config(sDir + "LOG\\DB\\" + datetime + ".log");296 if (Commlogger != null)297 Commlogger.config(sDir + "LOG\\Comm\\" + datetime + ".log");298 }299 this.sw.WriteLine(DateTime.Now.ToString() + " - " + strLog);300 this.sw.Flush();301 }302 }303 304 /**305 * 写日志306 **/307 public void debug(string strLog)308 {309 310 if (this.level <= Logger.DEBUG && this.recordLog)311 {312 this.log("debug: " + strLog);313 }314 }315 public void error(string strLog)316 {317 if (this.level <= Logger.ERROR && this.recordLog)318 {319 this.log("error: " + strLog);320 }321 }322 public void info(string strLog)323 {324 if (this.level <= Logger.INFO && this.recordLog)325 {326 this.log("infor: " + strLog);327 }328 }329 public void warning(string strLog)330 {331 if (this.level <= Logger.WARNING && this.recordLog)332 {333 this.log("warning: " + strLog);334 }335 }336 337 public void debugUploadError(string strLog)338 {339 // System.Windows.Forms.MessageBox.Show(strLog);340 if (this.level <= Logger.DEBUG && this.recordLog)341 {342 this.log("debug: " + strLog);343 }344 }345 }346 }
调用示例:
1 private void btn_db_Click(object sender, EventArgs e) 2 { 3 Logger.getLogger("DB").debug("this is a test click"); 4 } 5 6 private void btn_com_Click(object sender, EventArgs e) 7 { 8 Logger.getLogger("Comm").debug("this is a test click"); 9 }10 11 private void btn_log_Click(object sender, EventArgs e)12 {13 Logger.getLogger().debug("this is a test click");14 }