欢迎莅临 SUN WU GANG 的园子!!!

世上无难事,只畏有心人。有心之人,即立志之坚午也,志坚则不畏事之不成。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  470 随笔 :: 0 文章 :: 22 评论 :: 30万 阅读
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.IO;
using System.Text;
 
namespace ZB.QueueSys.Common
{
    public class LogHelper
    {
        //private static LogHelper instance;
        //public static LogHelper Instance
        //{
        //    get
        //    {
        //        if (instance == null) instance = new LogHelper();
        //        return LogHelper.instance;
        //    }
        //}
 
        //定义一个用于保存静态变量的实例
        private static LogHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private LogHelper() { }
        public static LogHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null) instance = new LogHelper();
                    }
                }
                return instance;
            }
        }
 
 
        public static LogHelper Default = new LogHelper();
        public string OutputLog = PubVariable.Instance.IsUseLog;
        public const string DIAG = "Diag";
        public const string Enrol = "Enrol";
 
        public StringBuilder sb = new StringBuilder();
        public void WriteCacheLog(string msg)
        {
            sb.Append(DateTime.Now.ToString("HH:mm:ss") + ": " + msg + "\r\n");
        }
 
        public void SaveLogToTxt(string logFile)
        {
            bool isAppend = false;
            if (File.Exists(logFile))
            {
                //File.Delete(logFile);
                isAppend = true;
            }
 
            using (StreamWriter sw = new StreamWriter(logFile, isAppend))
            {
                sw.WriteLine(sb.ToString());
                sw.Flush();
                sw.Close();
            }
        }
        /// <summary>
        /// 保存并输出日志
        /// </summary>
        /// <param name="WriteTxt"></param>
        /// <param name="isOutPut">0、不输出</param>
        public void SaveTextAndOutPut(string WriteTxt, string isOutPut)
        {
            try
            {
                if (isOutPut == "0") return;
                string LogPath = "C:\\ZB.QueueSys\\TempLog\\";
                LogPath = PubVariable.Instance.LogFilePath;
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
 
                LogPath += System.DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                //创建一个文件流,用以写入或者创建一个StreamWriter
                FileStream fs = new FileStream(LogPath + "\\D" + System.DateTime.Now.Hour.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.Flush();
 
                // 使用StreamWriter来往文件中写入内容
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                // 把richTextBox1中的内容写入文件
                m_streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss") + ": \n\r" + WriteTxt);
                //关闭此文件
                m_streamWriter.Flush();
                m_streamWriter.Close();
            }
            catch
            {
            }
        }
 
        public void SaveText(string WriteTxt)
        {
            try
            {
                if (OutputLog == "0") return;
                string LogPath = "C:\\ZB.QueueSys\\TempLog\\";
                LogPath = PubVariable.Instance.LogFilePath;
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
 
                LogPath += System.DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(LogPath))
                {
                    Directory.CreateDirectory(LogPath);
                }
                //创建一个文件流,用以写入或者创建一个StreamWriter
                FileStream fs = new FileStream(LogPath + "\\D" + System.DateTime.Now.Hour.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.Flush();
 
                // 使用StreamWriter来往文件中写入内容
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                // 把richTextBox1中的内容写入文件
                m_streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss") + ": \n\r" + WriteTxt);
                //关闭此文件
                m_streamWriter.Flush();
                m_streamWriter.Close();
            }
            catch
            {
            }
        }
 
        public void SaveAsText(string WriteTxt)
        {
            try
            {
                string LogPath = "C:\\Temp\\Diag\\";
                if (!Directory.Exists(LogPath)) return; //作为开关用
 
                LogPath += System.DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(LogPath)) Directory.CreateDirectory(LogPath);
                //创建一个文件流,用以写入或者创建一个StreamWriter
                FileStream fs = new FileStream(LogPath + "\\D" + System.DateTime.Now.Hour.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.Flush();
 
                // 使用StreamWriter来往文件中写入内容
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                // 把richTextBox1中的内容写入文件
                m_streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss") + ": " + WriteTxt);
                //关闭此文件
                m_streamWriter.Flush();
                m_streamWriter.Close();
            }
            catch
            {
            }
        }
 
        public void SaveAsText(string WriteTxt, string Studyid)
        {
            try
            {
                string LogPath = "C:\\Temp\\DIAG\\";
                if (!Directory.Exists(LogPath)) return; //作为开关用
 
                LogPath = LogPath + System.DateTime.Now.ToString("yyyyMMdd");
                if (!Directory.Exists(LogPath)) Directory.CreateDirectory(LogPath);
 
                if (string.IsNullOrEmpty(Studyid)) Studyid = "1000";
                string filename = LogPath + "\\" + Studyid + ".txt";
 
                //创建一个文件流,用以写入或者创建一个StreamWriter
                FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.Flush();
 
                // 使用StreamWriter来往文件中写入内容
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                // 把richTextBox1中的内容写入文件
                m_streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + ": " + WriteTxt);
                //关闭此文件
                m_streamWriter.Flush();
                m_streamWriter.Close();
            }
            catch
            {
            }
        }
    }
 
    public class MyLogHelper
    {
        /// <summary>
        /// 信息类型
        /// </summary>
        public enum LogLevel
        {
            /// <summary>
            /// 普通信息
            /// </summary>
            Info,
            /// <summary>
            /// 错误
            /// </summary>
            Error
        }
 
        /// <summary>
        /// 普通信息写入日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="logType"></param>
        public static void Info(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message)) return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "", message);
        }
        /// <summary>
        /// 自定义错误信息写入
        /// </summary>
        /// <param name="message">自定义消息</param>
        /// <param name="logType">存储目录类型</param>
        public static void Error(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message)) return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", message);
        }
        /// <summary>
        /// 程序异常信息写入
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="logType">存储目录类型</param>
        public static void Error(Exception e, LogType logType = LogType.Overall)
        {
            if (e == null) return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", e.Message);
        }
        /// <summary>
        /// 写日志的最终执行动作
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="prefix">前缀</param>
        /// <param name="message">内容</param>
        public static void WriteLog(string path, string prefix, string message)
        {
            path = @"C:\RisLog\";
 
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
 
            var fileName = string.Format("{0}{1}.Log", prefix, DateTime.Now.ToString("yyyy-MM-dd"));
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
 
            using (FileStream fs = new FileStream(path + fileName, FileMode.Append, FileAccess.Write, FileShare.Write, 1024, FileOptions.Asynchronous))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("HH:mm:ss") + " " + message + "\r\n");
                IAsyncResult writeResult = fs.BeginWrite(buffer, 0, buffer.Length,
                    (asyncResult) =>
                    {
                        var fStream = (FileStream)asyncResult.AsyncState;
                        fStream.EndWrite(asyncResult);
                    },
 
                    fs);
                fs.Close();
            }
        }
    }
 
    /// <summary>
    /// 日志文件存放文件夹分类枚举
    /// </summary>
    public enum LogType
    {
        /// <summary>
        /// 普通信息
        /// </summary>
        Info,
        /// <summary>
        /// 错误
        /// </summary>
        Error,
        /// <summary>
        /// 其他全部信息
        /// </summary>
        Overall,
    }
 
}

  

posted on   sunwugang  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2016-10-28 Winfrom Wait 实现转圈等待 this.Cursor = Cursors.WaitCursor;
点击右上角即可分享
微信分享提示