本地化记录日志(写文件)

平日工作中会写一些小的工具,但是使用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         }

考虑到日志记录会越来越多,所以产生了删除日志的方法,代码如下:

 1  /// <summary>
 2         /// 删除早期产生的老的日志
 3         /// </summary>
 4         private void DeleteLogFile()
 5         {
 6             //删除早期日志
 7             var time = DateTime.Now.AddDays(-10);
 8             var year = time.Year;
 9             var month = time.ToString("yyyyMM");
10             var date = time.ToString("yyyyMMdd");
11             var filePath = AppDomain.CurrentDomain.BaseDirectory + @"log\" + year + @"\" + month + @"\" + date + ".txt";
12             if (File.Exists(filePath))
13             {
14                 File.Delete(filePath);
15             }
16             if (DateTime.Now.Day >= 10)
17             {
18                 //删除上个月产生的文件夹
19                 var lastMothTime = DateTime.Now.AddMonths(-1);
20                 var lastMothYear = lastMothTime.Year;
21                 var lastMonth = lastMothTime.ToString("yyyyMM");
22                 var lastMonthLogFilePath = AppDomain.CurrentDomain.BaseDirectory + @"log\" + lastMothYear + @"\" + lastMonth;
23                 if (Directory.Exists(lastMonthLogFilePath))
24                 {
25                     Directory.Delete(lastMonthLogFilePath, true);
26                 }
27                 //删除上一年产生的日志文件夹
28                 var lastYear = DateTime.Now.AddYears(-1).Year;
29                 var lastYearLogFilePath = AppDomain.CurrentDomain.BaseDirectory + @"log\" + lastYear;
30                 if (Directory.Exists(lastYearLogFilePath))
31                 {
32                     Directory.Delete(lastYearLogFilePath, true);
33                 }
34             }
35         }
原文地址:https://www.cnblogs.com/airven/p/4897299.html