码迷,mamicode.com
首页 > 其他好文 > 详细

小的系统采用写文本作为日志记录

时间:2015-10-21 12:13:53      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

平日工作中会写一些小的工具,但是使用log4net又过于大了些,感觉没有必要,所以就用记事本去记录一些系统日志

代码如下:

 1 private static object objLock = new object(); // 读写文件锁
 2         /// <summary>
 3         /// 记录错误日志
 4         /// </summary>
 5         /// <param name="lcontent">错误日志内容</param>
 6         /// <param name="filePath">错误日志保存文件路径</param>
 7         public static void WriteErrorLog(string filePath, string lcontent)
 8         {
 9             string directoty = DateTime.Now.ToString("yyyyMMdd") + "\\" + filePath + "\\";
10             string fileName = string.Empty;
11 
12             if (string.IsNullOrEmpty(fileName))
13             {
14                 fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
15             }
16             else
17             {
18                 fileName = fileName + ".txt";
19             }
20             string content = DateTime.Now.ToString() + "\r\n" + lcontent + "\r\n\r\n";
21             WriteInfoToFile(directoty, fileName, content);
22         }
23 
24         /// <summary>
25         /// 记录错误到到日志文件
26         /// </summary>
27         /// <param name="directory">目录</param>
28         /// <param name="fileName">文件名</param>
29         /// <param name="content">错误内容</param>
30         private static void WriteInfoToFile(string directory, string fileName, string content)
31         {
32             System.IO.FileStream fs = null;
33             System.IO.TextWriter tw = null;
34             try
35             {
36                 lock (objLock)
37                 {
38                     string logPath = Assembly.GetExecutingAssembly().Location;
39                     //string logPath =HttpContext.Current.Server.MapPath("~");
40                     logPath = logPath.Substring(0, logPath.LastIndexOf(\\));//删除文件名;
41                     logPath = logPath + @"\log\" + directory;
42                     if (!System.IO.Directory.Exists(logPath))
43                         System.IO.Directory.CreateDirectory(logPath);
44 
45                     fs = new System.IO.FileStream(logPath + fileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
46                     tw = new System.IO.StreamWriter(fs);
47 
48                     fs.Seek(0, System.IO.SeekOrigin.End);
49                     tw = System.IO.TextWriter.Synchronized(tw);
50                     tw.Write(content);
51                     tw.Flush();
52                     tw.Close();
53                     fs.Close();
54                 }
55             }
56             catch (IOException ex )
57             {
58                 throw ex;
59             }
60             finally
61             {
62                 if (tw != null)
63                 {
64                     tw.Close();
65                     tw.Dispose();
66                 }
67                 if (fs != null)
68                 {
69                     fs.Close();
70                     fs.Dispose();
71                 }
72             }
73         }

 

小的系统采用写文本作为日志记录

标签:

原文地址:http://www.cnblogs.com/airven/p/4897299.html

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