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