一个简单的日志类

继前段时间更新的2篇以后,又有日子没更新了,时间一长怕又不想更了。框架

放一个之前作的,一直用着的日志类。代码比较短,也不用码不少字去说明,代码以下:ide

 1 /*
 2  * 日志类 log.cs
 3  * 做者:宁涛
 4  * 时间:2012-09-20
 5  * 用法:
 6  * NingTao.Log myLog = new NingTao.Log("日志名称");
 7  * myLog.addLog(@"日志信息");
 8  * 添加一条日志时:
 9  * 目录结构:日志名称\年月\日.log
10  * 日志内容:[时间] 日志信息
11 */
12 
13 using System;
14 using System.Text;
15 using System.IO;
16 
17 namespace NingTao
18 {
19   public class Log
20   {
21     // 日志分类名称
22     private string logCategory = "logs";
23 
24     public string LogCategory
25     {
26       get { return logCategory; }
27       set { logCategory = value; }
28     }
29 
30     public Log()
31     {
32     }
33     public Log(string categoryname)
34     {
35       LogCategory = categoryname;
36     }
37 
38     // 添加一行日志
39     public bool addLog(string logContent)
40     {
41       try
42       {
43         DateTime currentTime = DateTime.Now;
44         // 目录不存在则建立
45         string strPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\" + logCategory + currentTime.ToString(@"\\yyyyMM");
46         if (!Directory.Exists(strPath))
47         {
48           Directory.CreateDirectory(strPath);
49         }
50 
51         string strFile = currentTime.ToString(@"\\dd.lo\g");
52         string strContent = currentTime.ToString(@"[HH:mm:ss] ");
53         strContent += logContent + "\r\n";
54         using (var writer = new StreamWriter(strPath + strFile, true, Encoding.UTF8))
55         {
56           writer.Write(strContent);
57         }
58         return true;
59       }
60       catch
61       {
62         return false;
63       }
64     }
65   }
66 }

用法:spa

先建立实例,指定存放文件夹的名称。好比我建立一个eventLOg,一个errorLog。日志

须要保存事件或错误信息的时候,只要相应的实例add一条就能够了。code

以后软件运行一段时间,就能够查一下相应的事件及错误日志了。orm

 

周末若是有时间,准备继续玩一下leapmotion。
毕竟仍是想让它能有点用处,能拿出来显摆显摆。
好比控制个幻灯片播放什么的。
最近试了一下,网页版幻灯片框架reveal.js居然能够支持leapmotion控制。
想通用的话,仍是模拟下鼠标动做比较方便。
查了一下,codeproject上面有个MouseKeyboardLibrary鼠标键盘钩子库,比较好用。
准备抽时间把它挂到leapmotion上,先试试用leapmotion代替鼠标。
下篇预告:(关于leapmotion)
相关文章
相关标签/搜索