记事本记录操作日志笔记
1 /// <summary>
2 /// 日志操作
3 /// </summary>
4 public class Record
5 {
6 private static Record myRecord = new Record();//实例化自身
7
8 private FileInfo fFile;
9 private FileStream myStream;
10
11 /// <summary>
12 /// 构造函数
13 /// </summary>
14 private Record()
15 {
16 }
17
18 #region 记录日志内容
19 /// <summary>
20 /// 写日志(日志文件)
21 /// </summary>
22 /// <param name="sProcess">操作名</param>
23 /// <param name="sErrMsg">异常信息</param>
24 private void execWriteRecord(string sProcess, string sErrMsg)
25 {
26 try
27 {
28 // 日志内容
29 string sDateTime = DateTime.Now.ToString();
30 string sText = "";
31 sText += "[" + sDateTime + "] ";
32 sText += sProcess + ":";
33 sText += sErrMsg + "\r\n";
34
35 execWrite(sText);
36 }
37 catch { }
38 }
39 #endregion
40
41 #region 记录日志内容
42 /// <summary>
43 /// 写日志(日志文件)
44 /// </summary>
45 /// <param name="sErrMsg">信息</param>
46 private void execWriteRecord(string sMsg)
47 {
48 try
49 {
50 execWrite(execStringRecord(sMsg));
51 }
52 catch { }
53 }
54 #endregion
55
56 #region 组合日志内容
57 /// <summary>
58 /// 组合日志内容
59 /// </summary>
60 /// <param name="sMsg">信息</param>
61 /// <returns></returns>
62 private string execStringRecord(string sMsg)
63 {
64 string sText = "";
65 try
66 {
67 // 日志内容
68 string sDateTime = DateTime.Now.ToString();
69 sText += "[" + sDateTime + "] ";
70 sText += sMsg + "\r\n";
71 }
72 catch
73 { return sMsg; }
74 return sText;
75 }
76 #endregion
77
78 #region 记录日志内容
79 /// <summary>
80 /// 写日志(日志文件)
81 /// </summary>
82 /// <param name="sErrMsg">信息</param>
83 private void execWrite(string sMsg)
84 {
85 try
86 {
87 // 取得日志文件
88 getRecordFile();
89
90 //获得字节数组
91 byte[] data = Encoding.Default.GetBytes(sMsg);
92
93 //开始写入
94 myStream.Write(data, 0, data.Length);
95
96 //清空缓冲区、关闭流
97 myStream.Flush();
98 }
99 finally
100 {
101 if (myStream != null)
102 {
103 myStream.Close();
104 }
105 }
106 }
107 #endregion
108
109 #region 取得日志文件名
110
111 /// <summary>
112 /// 取得日志文件名
113 /// </summary>
114 /// <returns>日志文件名</returns>
115 private void getRecordFile()
116 {
117 // 单个日志文件大小(10M)
118 int iSize = 1024 * 100;//1024 * 1024 * 10;
119 if (myStream != null && myStream.CanWrite)
120 {
121 fFile = new FileInfo(myStream.Name);
122 if (fFile.Length < iSize)
123 {
124 return;
125 }
126 }
127
128 // 日志文件夹
129 string sFolder = Application.StartupPath + "\\Log\\";
130
131 // 文件夹不存在时,创建文件夹
132 DirectoryInfo dirFolder = new DirectoryInfo(sFolder);
133 if (!dirFolder.Exists)
134 {
135 dirFolder.Create();
136 }
137
138 // 日志文件
139 int i;
140 string sDate = DateTime.Now.ToString("yyyyMMdd");
141 string sFileName = "";
142 for (i = 1; ; i++)
143 {
144 sFileName = sDate + "-" + i.ToString() + "-cLog.txt"; //记事本格式
145 fFile = new FileInfo(sFolder + sFileName);
146 if (!fFile.Exists)
147 {
148 // 日志文件不存在
149 myStream = fFile.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
150 break;
151
152 }
153 else if (fFile.Length < iSize)
154 {
155 // 日志文件小于100K
156 try
157 {
158 myStream = fFile.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
159 }
160 catch
161 { }
162
163 break;
164 }
165 else
166 {
167 if (myStream != null)
168 {
169 myStream.Close();
170 }
171 }
172 }
173 }
174 #endregion
175
176 #region 写日志(二个参数)Public Static 方法
177 /// <summary>
178 /// 写日志(日志文件)
179 /// </summary>
180 /// <param name="sProcess">操作名</param>
181 /// <param name="sErrMsg">异常信息</param>
182 public static void execFileRecord(string sProcess, string sErrMsg)
183 {
184 myRecord.execWriteRecord(sProcess, sErrMsg);
185 }
186 #endregion
187
188 #region 写日志(一个参数) Public Static 方法
189 /// <summary>
190 /// 写日志(日志文件)
191 /// </summary>
192 /// <param name="sErrMsg">信息</param>
193 public static void execFileRecord(string sMsg)
194 {
195 myRecord.execWriteRecord(sMsg);
196 }
197 #endregion
198
199 }
200
2 /// 日志操作
3 /// </summary>
4 public class Record
5 {
6 private static Record myRecord = new Record();//实例化自身
7
8 private FileInfo fFile;
9 private FileStream myStream;
10
11 /// <summary>
12 /// 构造函数
13 /// </summary>
14 private Record()
15 {
16 }
17
18 #region 记录日志内容
19 /// <summary>
20 /// 写日志(日志文件)
21 /// </summary>
22 /// <param name="sProcess">操作名</param>
23 /// <param name="sErrMsg">异常信息</param>
24 private void execWriteRecord(string sProcess, string sErrMsg)
25 {
26 try
27 {
28 // 日志内容
29 string sDateTime = DateTime.Now.ToString();
30 string sText = "";
31 sText += "[" + sDateTime + "] ";
32 sText += sProcess + ":";
33 sText += sErrMsg + "\r\n";
34
35 execWrite(sText);
36 }
37 catch { }
38 }
39 #endregion
40
41 #region 记录日志内容
42 /// <summary>
43 /// 写日志(日志文件)
44 /// </summary>
45 /// <param name="sErrMsg">信息</param>
46 private void execWriteRecord(string sMsg)
47 {
48 try
49 {
50 execWrite(execStringRecord(sMsg));
51 }
52 catch { }
53 }
54 #endregion
55
56 #region 组合日志内容
57 /// <summary>
58 /// 组合日志内容
59 /// </summary>
60 /// <param name="sMsg">信息</param>
61 /// <returns></returns>
62 private string execStringRecord(string sMsg)
63 {
64 string sText = "";
65 try
66 {
67 // 日志内容
68 string sDateTime = DateTime.Now.ToString();
69 sText += "[" + sDateTime + "] ";
70 sText += sMsg + "\r\n";
71 }
72 catch
73 { return sMsg; }
74 return sText;
75 }
76 #endregion
77
78 #region 记录日志内容
79 /// <summary>
80 /// 写日志(日志文件)
81 /// </summary>
82 /// <param name="sErrMsg">信息</param>
83 private void execWrite(string sMsg)
84 {
85 try
86 {
87 // 取得日志文件
88 getRecordFile();
89
90 //获得字节数组
91 byte[] data = Encoding.Default.GetBytes(sMsg);
92
93 //开始写入
94 myStream.Write(data, 0, data.Length);
95
96 //清空缓冲区、关闭流
97 myStream.Flush();
98 }
99 finally
100 {
101 if (myStream != null)
102 {
103 myStream.Close();
104 }
105 }
106 }
107 #endregion
108
109 #region 取得日志文件名
110
111 /// <summary>
112 /// 取得日志文件名
113 /// </summary>
114 /// <returns>日志文件名</returns>
115 private void getRecordFile()
116 {
117 // 单个日志文件大小(10M)
118 int iSize = 1024 * 100;//1024 * 1024 * 10;
119 if (myStream != null && myStream.CanWrite)
120 {
121 fFile = new FileInfo(myStream.Name);
122 if (fFile.Length < iSize)
123 {
124 return;
125 }
126 }
127
128 // 日志文件夹
129 string sFolder = Application.StartupPath + "\\Log\\";
130
131 // 文件夹不存在时,创建文件夹
132 DirectoryInfo dirFolder = new DirectoryInfo(sFolder);
133 if (!dirFolder.Exists)
134 {
135 dirFolder.Create();
136 }
137
138 // 日志文件
139 int i;
140 string sDate = DateTime.Now.ToString("yyyyMMdd");
141 string sFileName = "";
142 for (i = 1; ; i++)
143 {
144 sFileName = sDate + "-" + i.ToString() + "-cLog.txt"; //记事本格式
145 fFile = new FileInfo(sFolder + sFileName);
146 if (!fFile.Exists)
147 {
148 // 日志文件不存在
149 myStream = fFile.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
150 break;
151
152 }
153 else if (fFile.Length < iSize)
154 {
155 // 日志文件小于100K
156 try
157 {
158 myStream = fFile.Open(FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
159 }
160 catch
161 { }
162
163 break;
164 }
165 else
166 {
167 if (myStream != null)
168 {
169 myStream.Close();
170 }
171 }
172 }
173 }
174 #endregion
175
176 #region 写日志(二个参数)Public Static 方法
177 /// <summary>
178 /// 写日志(日志文件)
179 /// </summary>
180 /// <param name="sProcess">操作名</param>
181 /// <param name="sErrMsg">异常信息</param>
182 public static void execFileRecord(string sProcess, string sErrMsg)
183 {
184 myRecord.execWriteRecord(sProcess, sErrMsg);
185 }
186 #endregion
187
188 #region 写日志(一个参数) Public Static 方法
189 /// <summary>
190 /// 写日志(日志文件)
191 /// </summary>
192 /// <param name="sErrMsg">信息</param>
193 public static void execFileRecord(string sMsg)
194 {
195 myRecord.execWriteRecord(sMsg);
196 }
197 #endregion
198
199 }
200
作者:chhuic
出处:http://chhuic.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。