C#保存日誌文件到txt中,可追加保存,定時刪除最後一次操做半年前日誌文件

 1         /// <summary>
 2         /// 輸出指定信息到文本文件
 3         /// </summary>
 4         /// <param name="msg">輸出信息</param>
 5         public void WriteMessage(string msg, string userName)
 6         {
 7             try
 8             {
 9 
10                 string mainPath = "F:\\log\\";//日誌文件路徑&配置到Config文件中直接獲取
11                 string path = mainPath;
12                 string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt";//文件名
13                 string year = DateTime.Now.ToString("yyyy");//
14                 string month = DateTime.Now.ToString("MM");//15 
16                 //判斷log文件路徑是否存在,不存在則建立文件夾 
17                 if (!System.IO.Directory.Exists(path))
18                 {
19                     System.IO.Directory.CreateDirectory(path);//不存在就建立目錄 
20                 }
21 
22                 path += year + "\\";
23                 //判斷年度文件夾是否存在,不存在則建立文件夾 
24                 if (!System.IO.Directory.Exists(path))
25                 {
26                     System.IO.Directory.CreateDirectory(path);//不存在就建立目錄 
27                 }
28 
29                 path += month + "\\";
30                 //判斷月度文件夾是否存在,不存在則建立文件夾 
31                 if (!System.IO.Directory.Exists(path))
32                 {
33                     System.IO.Directory.CreateDirectory(path);//不存在就建立目錄 
34                 }
35 
36                 //拼接完整文件路徑
37                 path += filename;
38                 if (!File.Exists(path))
39                 {
40                     //文件不存在,新建文件
41                     FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
42                     StreamWriter sw = new StreamWriter(fs);
43                     sw.Close();
44                 }
45 
46                 using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
47                 {
48                     using (StreamWriter sw = new StreamWriter(fs))
49                     {
50                         sw.BaseStream.Seek(0, SeekOrigin.End);
51                         //sw.WriteLine("------------------------------------------------------------------------ Info Start ");
52                         sw.WriteLine("操做時間:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
53                         sw.WriteLine("操做人:" + userName);
54                         sw.WriteLine("Message:{0}\n", msg, DateTime.Now);
55                         sw.WriteLine("------------------------------------------------------------------------ ");
56                         Console.WriteLine("\n");
57                         sw.Flush();
58                     }
59                 }
60 
61                 //當前月份
62                 int totalmonth = int.Parse(month);
63                 int totalyear = int.Parse(year);
64                 int tmonth = 6;//保留日誌時長,月度單位
65                 DateTime date;
66                 string datestring = "";
67 
68                 DirectoryInfo dyInfo = new DirectoryInfo(mainPath);
69                 //刪除歷史數據,保留6個月日誌
70                 if (totalmonth < tmonth)
71                 {
72                     //刪除前一年totalmonth+6月份以前的數據
73                     datestring = (totalyear - 1).ToString() + "-" + (totalmonth + tmonth).ToString().PadLeft(2, '0') + "-" + "01 00:00:00";
74 
75                 }
76                 else
77                 {
78                     //刪除當年6個月前的數據
79                     datestring = (totalyear).ToString() + "-" + (totalmonth - tmonth).ToString().PadLeft(2, '0') + "-" + "01 00:00:00";
80                 }
81                 date = Convert.ToDateTime(datestring);
82                 //獲取文件夾下全部的文件
83                 foreach (FileInfo feInfo in dyInfo.GetFiles())
84                 {
85                     //判斷文件日期是否小於今天,是則刪除
86                     if (feInfo.CreationTime < date)
87                         feInfo.Delete();
88                 }
89             }
90             catch (Exception)
91             {
92             }
93         }

 

相關文章
相關標籤/搜索