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

C#日志类

时间:2017-03-22 00:05:59      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:job   file   domain   摘要   sage   datetime   mod   path   art   

 

日志在我们日常程序中是非常容易触及到的东西,当然我们在程序中可以用很多种方式来记录。

在C#中我们可以用log4net.dll,也可以利用IO将记录写入文件。


 

作用:

  1. 记录用户的操作
  2. 记录程序中的异常,我们可以捕获到很多有用的东西
  3. 为数据分析提供依据

这篇文章给了我们写日志非常好的建议:http://blog.jobbole.com/52018/


 

下面是我在项目经常用到的一个log类,

技术分享
 1 /// <summary>
 2 ///Log 的摘要说明
 3 /// </summary>
 4 public class Log
 5 {
 6     private string PathName;
 7     private string FileName;
 8 
 9     /// <summary>
10     /// 构造 Log
11     /// </summary>
12     /// <param name="pathname">相对于网站根目录 App_Log 目录的相对路径,如: System, 就相当于 ~/App_Log/System/</param>
13     public Log(string pathname)
14     {
15         if (String.IsNullOrEmpty(pathname))
16         {
17             throw new Exception("没有初始化 Log 类的 PathName 变量");
18         }
19 
20         PathName = System.AppDomain.CurrentDomain.BaseDirectory + "App_Log/" + pathname;
21 
22         if (!Directory.Exists(PathName))
23         {
24             try
25             {
26                 Directory.CreateDirectory(PathName);
27             }
28             catch { }
29         }
30 
31         if (!Directory.Exists(PathName))
32         {
33             PathName = System.AppDomain.CurrentDomain.BaseDirectory + "App_Log";
34 
35             if (!Directory.Exists(PathName))
36             {
37                 try
38                 {
39                     Directory.CreateDirectory(PathName);
40                 }
41                 catch { }
42             }
43 
44             if (!Directory.Exists(PathName))
45             {
46                 PathName = System.AppDomain.CurrentDomain.BaseDirectory;
47             }
48         }
49 
50         FileName = PathName + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
51     }
52 
53     public void Write(string Message)
54     {
55         if (String.IsNullOrEmpty(FileName))
56         {
57             return;
58         }
59 
60         using (FileStream fs = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.Write))
61         {
62             StreamWriter writer = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GBK"));
63 
64             try
65             {
66                 writer.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + System.DateTime.Now.Millisecond.ToString() + "\t\t" + Message + "\r\n");
67             }
68             catch { }
69 
70             writer.Close();
71         }
72     }
73 
74     public void WriteIni(string Message)
75     {
76         WriteIni("Log", Message);
77     }
78 
79     public void WriteIni(string Section, string Message)
80     {
81         if (String.IsNullOrEmpty(FileName))
82         {
83             return;
84         }
85 
86         Shove._IO.IniFile ini = new Shove._IO.IniFile(FileName);
87 
88         ini.Write(Section, System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":" + System.DateTime.Now.Millisecond.ToString(), Message);
89     }
90 }
View Code

当然我们也可以利用log4net.dll来配置,将一些记录写入到数据库中

demo地址 http://files.cnblogs.com/files/QDF-/log4_demo.rar

C#日志类

标签:job   file   domain   摘要   sage   datetime   mod   path   art   

原文地址:http://www.cnblogs.com/QDF-/p/6597216.html

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