schangxiang@126.com
2025-09-04 5df8a085b6c9334a04ed8b90be110d1fbcd3bc89
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using Newtonsoft.Json;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
 
namespace yunneiWCS
{
    public class LogTextHelper
    {
        /// <summary>
        /// 批次删除日志文件
        /// </summary>
        public static void BatchDeleteLog()
        {
            try
            {
                List<string> deleteFolder = new System.Collections.Generic.List<string>();
                var aa = System.Environment.CurrentDirectory;
                string folderPath = aa+"\\Log";
                FolderHelper.ListDirectory(folderPath, 0, ref deleteFolder);
 
                foreach (var item in deleteFolder)
                {
                    FileHelper.DeleteOldFiles(item, 30);//删除一个月的数据
                }
            }
            catch (Exception)
            {
              
            }
        }
 
        /// <summary>
        /// 批次删除日志文件
        /// </summary>
        public static void BatchDeleteLog(string folderPath)
        {
            try
            {
                List<string> deleteFolder = new System.Collections.Generic.List<string>();
                FolderHelper.ListDirectory(folderPath, 0, ref deleteFolder);
 
                foreach (var item in deleteFolder)
                {
                    FileHelper.DeleteOldFiles(item, 30);//删除一个月的数据
                }
            }
            catch (Exception)
            {
 
            }
        }
 
        /// <summary>
        /// 往日志里追加内容
        /// </summary>
        /// <param name="folder">日志目录</param>
        /// <param name="logContent">内容</param>
        private static void DoWriteLogContent(string folder, string logContent, Exception ex)
        {
            try
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                string message = string.Format("【{0}】{1}\r\n", DateTime.Now.ToString("HH:mm:ss"), logContent);
                if (ex != null)
                {
                    message += ",异常堆栈:" + JsonConvert.SerializeObject(ex);
                }
                File.AppendAllText(Path.Combine(folder, DateTime.Now.ToString("yyyyMMdd") + ".txt"), message);
            }
            catch (Exception)
            {
            }
        }
 
        /// <summary>
        /// 往日志里追加内容
        /// </summary>
        /// <param name="folder">日志目录</param>
        /// <param name="format">内容格式</param>
        /// <param name="args">内容参数</param>
        public static void WriteLine(string folder, string format, params object[] args)
        {
            try
            {
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                string message = string.Format("【{0}】{1}\r\n", DateTime.Now.ToString("HH:mm:ss"), string.Format(format, args));
                File.AppendAllText(Path.Combine(folder, DateTime.Now.ToString("yyyyMMdd") + ".txt"), message);
            }
            catch (Exception)
            {
            }
        }
 
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="folder">日志目录</param>
        /// <param name="className">类名</param>
        /// <param name="methodName">方法名</param>
        /// <param name="format">内容格式</param>
        /// <param name="args">内容参数</param>
        public static void WriteLog(string folder, string className, string methodName, string format, params object[] args)
        {
            WriteLine(folder, "在类{0}的{1}方法中:{2}", className, methodName, string.Format(format, args));
 
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="folder">日志目录</param>
        /// <param name="className">类名</param>
        /// <param name="methodName">方法名</param>
        /// <param name="format">内容格式</param>
        /// <param name="args">内容参数</param>
        public static void WriteLogContent(string folder, string className, string methodName, string logContent, Exception ex)
        {
            DoWriteLogContent(folder, string.Format("在类{0}的{1}方法中:{2}", className, methodName, logContent), ex);
        }
    }
}