自定义日志类

最近开发一个windows服务,需要各项操作和报错写入日志文件以备检查服务运行状态和错误,里面用到的方法其实也是个人在网上找的然后拼接在一起,其他不多说,代码如下:

1.默认日志存放在程序运行的目录,适合于windows服务和CS程序(其中DataAction为程序自定义的一个类,用的时候替换成自己实际程序里面随便一个类就行了)

 1  public class MyLog
 2     {
 3         /// <summary>
 4         /// 日志方法(默认存放程序所在目录)
 5         /// </summary>
 6         /// <param name="readme">日志内容</param>
 7         public static void WriteLog(string info)
 8         {
 9             Assembly SampleAssembly;
10             DataAction da= new DataAction();
11             SampleAssembly = Assembly.GetAssembly(da.GetType());
12             string logDir =Path.GetDirectoryName(SampleAssembly.Location)+"/日志文件/";
13             string logFilePath = logDir + "/" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
14 
15             string infoFull = "事件:" + info + "  " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
16             byte[] text = Encoding.Default.GetBytes(infoFull);
17             if (!Directory.Exists(logDir))
18             {
19                 Directory.CreateDirectory(logDir);
20             }
21             if (!File.Exists(logFilePath))
22             {
23                 using (FileStream fs = File.Create(logFilePath))
24                 {
25                     fs.Write(text, 0, text.Length);
26                 }
27             }
28             else
29             {
30                 bool isUsed = FileIsUsed(logFilePath);
31                 if (!isUsed)
32                 {
33                     StreamWriter dout = new StreamWriter(logFilePath, true, Encoding.Default);
34                     dout.Write(infoFull);
35                     dout.Close();
36                 }
37                 else
38                 {
39                     Thread.Sleep(500);
40                     WriteLog(info);
41                 }
42             }
43             
44         }
45 
46         /// <summary>
47         /// 返回指示文件是否已被其它程序使用的布尔值
48         /// </summary>
49         /// <param name="fileFullName">文件的完全限定名,例如:“C:\MyFile.txt”。</param>
50         /// <returns>如果文件已被其它程序使用,则为 true;否则为 false。</returns>
51         static Boolean FileIsUsed(String fileFullName)
52         {
53             Boolean result = false;
54 
55             //判断文件是否存在,如果不存在,直接返回 false
56             if (System.IO.File.Exists(fileFullName))
57             {
58                 //如果文件存在,则继续判断文件是否已被其它程序使用
59 
60                 //逻辑:尝试执行打开文件的操作,如果文件已经被其它程序使用,则打开失败,抛出异常,根据此类异常可以判断文件是否已被其它程序使用。
61                 System.IO.FileStream fileStream = null;
62                 try
63                 {
64                     fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
65                 }
66                 catch (System.Exception ex)
67                 {
68                     result = true;
69                 }
70                 finally
71                 {
72                     if (fileStream != null)
73                     {
74                         fileStream.Close();
75                     }
76                 }
77             }
78             return result;
79         }
80     }
View Code

 

2.自定义日志存放目录,适合于BS程序

 1 public class MyLog
 2     {
 3         /// <summary>
 4         /// 日志方法
 5         /// </summary>
 6         /// <param name="readme">日志内容</param>
 7         /// <param name="logDir">日志存放目录</param>
 8         public static void WriteLog(string info, string logDir)
 9         {
10             string logFilePath = logDir + "/" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
11             string infoFull = "事件:" + info + "  " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
12             byte[] text = Encoding.Default.GetBytes(infoFull);
13             if (!Directory.Exists(logDir))
14             {
15                 Directory.CreateDirectory(logDir);
16             }
17             if (!File.Exists(logFilePath))
18             {
19                 using (FileStream fs = File.Create(logFilePath))
20                 {
21                     fs.Write(text, 0, text.Length);
22                 }
23             }
24             else
25             {
26                 bool isUsed = FileIsUsed(logFilePath);
27                 if (!isUsed)
28                 {
29                     StreamWriter dout = new StreamWriter(logFilePath, true,Encoding.Default);
30                     dout.Write(infoFull);
31                     dout.Close();
32                 }
33                 else
34                 {
35                     Thread.Sleep(500);
36                     WriteLog(info, logDir);
37                 }
38             }
39         }
40 
41         /// <summary>
42         /// 返回指示文件是否已被其它程序使用的布尔值
43         /// </summary>
44         /// <param name="fileFullName">文件的完全限定名,例如:“C:\MyFile.txt”。</param>
45         /// <returns>如果文件已被其它程序使用,则为 true;否则为 false。</returns>
46         static Boolean FileIsUsed(String fileFullName)
47         {
48             Boolean result = false;
49 
50             //判断文件是否存在,如果不存在,直接返回 false
51             if (System.IO.File.Exists(fileFullName))
52             {
53                 //如果文件存在,则继续判断文件是否已被其它程序使用
54 
55                 //逻辑:尝试执行打开文件的操作,如果文件已经被其它程序使用,则打开失败,抛出异常,根据此类异常可以判断文件是否已被其它程序使用。
56                 System.IO.FileStream fileStream = null;
57                 try
58                 {
59                     fileStream = System.IO.File.Open(fileFullName, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
60                 }
61                 catch (System.Exception ex)
62                 {
63                     result = true;
64                 }
65                 finally
66                 {
67                     if (fileStream != null)
68                     {
69                         fileStream.Close();
70                     }
71                 }
72             }
73             return result;
74         }
75     }
View Code

 

posted on 2014-11-04 19:52  独孤吟风  阅读(286)  评论(0)    收藏  举报