1
duan
2024-08-21 f71a02229c1ba00fbecaead19256593ffb052753
提交 | 用户 | age
90c6eb 1 using System;
J 2 using System.IO;
3 using System.Threading.Tasks;
4
5 namespace DIXWeb.Util
6 {
7     /// <summary>
8     /// 日志帮助类
9     /// </summary>
10     public static class LogHelper
11     {
12
13         /// <summary>
14         /// 写入日志到本地TXT文件 
15         /// </summary>
16         /// <param name="log">日志内容</param>
17         public static void WriteLog(string content, string folder = @"c:\logs\")
18         {
19             try
20             {
21                 if (string.IsNullOrEmpty(folder)) folder = AppDomain.CurrentDomain.BaseDirectory;
22                 WriteLogFile(content, folder);
23             }
24             catch { }
25         }
26         public static string CurrentTimeString
27         {
28             get { return " 【" + System.DateTime.Now.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss fff") + "】"; }
29         }
30         public static void WriteLogFile(string content, string logFolder = @"c:\logs\", bool needtime = false)
31         {
32             if (needtime) content += "  " + CurrentTimeString;
33             string filename = System.DateTime.Now.ToLocalTime().ToString("yyMMddHHmm") + ".txt";
34
35             string directory = Path.GetDirectoryName(logFolder);
36             string path = logFolder + filename;
37             if (!Directory.Exists(directory))
38             {
39                 try
40                 {
41                     Directory.CreateDirectory(directory);
42                 }
8eea5c 43                 catch (Exception)
90c6eb 44                 {
J 45                     //盘符不存在情况
46                     logFolder = "c:\\logs\\";
47                     directory = Path.GetDirectoryName(logFolder);
48                     Directory.CreateDirectory(directory);
49                 }
50
51             }
52
53             FileStream fs = null;
54             StreamWriter sw = null;
55             try
56             {
57                 if (!File.Exists(path))
58                     fs = new FileStream(path, FileMode.Create, FileAccess.Write);
59                 else
60                     fs = new FileStream(path, FileMode.Append, FileAccess.Write);
61
62
63                 sw = new StreamWriter(fs);
64                 //开始写入
65                 sw.WriteLine(content);
66                 //清空缓冲区
67                 sw.Flush();
68             }
69             catch (Exception er)
70             {
71             }
72             finally
73             {
74                 //关闭流
75                 if (sw != null)
76                 {
77                     sw.Close();
78                     sw.Dispose();
79                 }
80                 if (fs != null)
81                 {
82                     fs.Close();
83                     fs.Dispose();
84                 }
85
86             }
87         }
88         /// <summary>
89         /// 写入日志到本地TXT文件
90         /// 注:日志文件名为"A_log.txt",目录为根目录
91         /// </summary>
92         /// <param name="log">日志内容</param>
93         public static void WriteLog_LocalTxt(string log)
94         {
95             Task.Run(() =>
96             {
97                 string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A_log.txt");
98                 string logContent = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:{log}\r\n";
99                 File.AppendAllText(filePath, logContent);
100             });
101         }
102     }
103 }