C#日志记录设计与实现(BenXHLog)

C#日志记录设计与实现

日志记录:

日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和实现,如有不足,还望见谅!废话不多,直入主题。

笨小孩日志:BenXHLog

类文件设计:

文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,

程序代码:

Ilog接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace BenXH.Log.Log
 7 {
 8     public interface ILog
 9     {
10         bool IsDebugEnabled { get; }
11 
12         bool IsErrorEnabled { get; }
13 
14         bool IsFatalEnabled { get; }
15 
16         bool IsInfoEnabled { get; }
17 
18         bool IsWarnEnabled { get; }
19 
20         void Debug(bool isWriteFile,object message);
21 
22         void Debug(bool isWriteFile, object message, Exception exception);
23 
24         void DebugFormat(bool isWriteFile, string format, object arg0);
25 
26         void DebugFormat(bool isWriteFile, string format, params object[] args);
27 
28         void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
29 
30         void DebugFormat(bool isWriteFile, string format, object arg0, object arg1);
31 
32         void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
33 
34         void Error(bool isWriteFile,object message);
35 
36         void Error(bool isWriteFile, object message, Exception exception);
37 
38         void ErrorFormat(bool isWriteFile, string format, object arg0);
39 
40         void ErrorFormat(bool isWriteFile, string format, params object[] args);
41 
42         void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
43 
44         void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1);
45 
46         void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
47 
48         void Fatal(bool isWriteFile, object message);
49 
50         void Fatal(bool isWriteFile, object message, Exception exception);
51 
52         void FatalFormat(bool isWriteFile, string format, object arg0);
53 
54         void FatalFormat(bool isWriteFile, string format, params object[] args);
55 
56         void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
57 
58         void FatalFormat(bool isWriteFile, string format, object arg0, object arg1);
59 
60         void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
61 
62         void Info(bool isWriteFile, object message);
63 
64         void Info(bool isWriteFile, object message, Exception exception);
65 
66         void InfoFormat(bool isWriteFile, string format, object arg0);
67 
68         void InfoFormat(bool isWriteFile, string format, params object[] args);
69 
70         void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
71 
72         void InfoFormat(bool isWriteFile, string format, object arg0, object arg1);
73 
74         void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
75 
76         void Warn(bool isWriteFile, object message);
77 
78         void Warn(bool isWriteFile, object message, Exception exception);
79 
80         void WarnFormat(bool isWriteFile, string format, object arg0);
81 
82         void WarnFormat(bool isWriteFile, string format, params object[] args);
83 
84         void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
85 
86         void WarnFormat(bool isWriteFile, string format, object arg0, object arg1);
87 
88         void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
89     }
90 }

ILogFactory工厂接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenXH.Log.Log
{
    public interface ILogFactory
    {
        ILog GetLog(string name);
    }
}

日志类Log 这个代码实现多一点,合并了,点开看吧

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace BenXH.Log.Log
  7 {
  8     /// <summary>
  9     /// Console Log
 10     /// </summary>
 11     internal class Log : ILog
 12     {
 13         private string m_Name;
 14 
 15         private const string m_MessageTemplate = "{0}-{1}: {2}";
 16 
 17         private const string m_Debug = "DEBUG";
 18 
 19         private const string m_Error = "ERROR";
 20 
 21         private const string m_Fatal = "FATAL";
 22 
 23         private const string m_Info = "INFO";
 24 
 25         private const string m_Warn = "WARN";
 26 
 27         /// <summary>
 28         /// Initializes a new instance of the <see cref="Log"/> class.
 29         /// </summary>
 30         /// <param name="name">The name.</param>
 31         public Log(string name)
 32         {
 33             m_Name = name;
 34         }
 35 
 36         /// <summary>
 37         /// Gets a value indicating whether this instance is debug enabled.
 38         /// </summary>
 39         /// <value>
 40         ///     <c>true</c> if this instance is debug enabled; otherwise, <c>false</c>.
 41         /// </value>
 42         public bool IsDebugEnabled
 43         {
 44             get { return true; }
 45         }
 46 
 47         /// <summary>
 48         /// Gets a value indicating whether this instance is error enabled.
 49         /// </summary>
 50         /// <value>
 51         ///     <c>true</c> if this instance is error enabled; otherwise, <c>false</c>.
 52         /// </value>
 53         public bool IsErrorEnabled
 54         {
 55             get { return true; }
 56         }
 57 
 58         /// <summary>
 59         /// Gets a value indicating whether this instance is fatal enabled.
 60         /// </summary>
 61         /// <value>
 62         ///     <c>true</c> if this instance is fatal enabled; otherwise, <c>false</c>.
 63         /// </value>
 64         public bool IsFatalEnabled
 65         {
 66             get { return true; }
 67         }
 68 
 69         /// <summary>
 70         /// Gets a value indicating whether this instance is info enabled.
 71         /// </summary>
 72         /// <value>
 73         ///     <c>true</c> if this instance is info enabled; otherwise, <c>false</c>.
 74         /// </value>
 75         public bool IsInfoEnabled
 76         {
 77             get { return true; }
 78         }
 79 
 80         /// <summary>
 81         /// Gets a value indicating whether this instance is warn enabled.
 82         /// </summary>
 83         /// <value>
 84         ///     <c>true</c> if this instance is warn enabled; otherwise, <c>false</c>.
 85         /// </value>
 86         public bool IsWarnEnabled
 87         {
 88             get { return true; }
 89         }
 90 
 91         public string GetDataTimeLog(string log)
 92         {
 93             return String.Format("[{0}]>>{1}", DateTime.Now.ToString("yy-MM-dd HH:mm:ss"), log);
 94         }
 95 
 96         /// <summary>
 97         /// Logs the debug message.
 98         /// </summary>
 99         /// <param name="isWriteFile"></param>
100         /// <param name="message">The message.</param>
101         public void Debug(bool isWriteFile, object message)
102         {
103             string log = GetDataTimeLog(message.ToString());
104             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log);
105             if (isWriteFile)
106             {
107                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
108             }
109         }
110 
111         /// <summary>
112         /// Logs the debug message.
113         /// </summary>
114         /// <param name="isWriteFile"></param>
115         /// <param name="message">The message.</param>
116         /// <param name="exception">The exception.</param>
117         public void Debug(bool isWriteFile, object message, Exception exception)
118         {
119             string log =GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace);
120             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log);
121             if (isWriteFile)
122             {
123                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
124             }
125         }
126 
127         /// <summary>
128         /// Logs the debug message.
129         /// </summary>
130         /// <param name="isWriteFile"></param>
131         /// <param name="format">The format.</param>
132         /// <param name="arg0">The arg0.</param>
133         public void DebugFormat(bool isWriteFile, string format, object arg0)
134         {
135             string log = GetDataTimeLog(string.Format(format, arg0));
136             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log);
137             if (isWriteFile)
138             {
139                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
140             }
141         }
142 
143         /// <summary>
144         /// Logs the debug message.
145         /// </summary>
146         /// <param name="isWriteFile"></param>
147         /// <param name="format">The format.</param>
148         /// <param name="args">The args.</param>
149         public void DebugFormat(bool isWriteFile, string format, params object[] args)
150         {
151             string log = GetDataTimeLog(string.Format(format, args));
152             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(format, args));
153             if (isWriteFile)
154             {
155                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
156             }
157         }
158 
159         /// <summary>
160         /// Logs the debug message.
161         /// </summary>
162         /// <param name="isWriteFile"></param>
163         /// <param name="provider">The provider.</param>
164         /// <param name="format">The format.</param>
165         /// <param name="args">The args.</param>
166         public void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args)
167         {
168             string log = GetDataTimeLog(string.Format(format, args));
169             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(provider, format, args));
170             if (isWriteFile)
171             {
172                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
173             }
174         }
175 
176         /// <summary>
177         /// Logs the debug message.
178         /// </summary>
179         /// <param name="isWriteFile"></param>
180         /// <param name="format">The format.</param>
181         /// <param name="arg0">The arg0.</param>
182         /// <param name="arg1">The arg1.</param>
183         public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1)
184         {
185             string log = GetDataTimeLog(string.Format(format, arg0, arg1));
186             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log);
187             if (isWriteFile)
188             {
189                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
190             }
191         }
192 
193         /// <summary>
194         /// Logs the debug message.
195         /// </summary>
196         /// <param name="isWriteFile"></param>
197         /// <param name="format">The format.</param>
198         /// <param name="arg0">The arg0.</param>
199         /// <param name="arg1">The arg1.</param>
200         /// <param name="arg2">The arg2.</param>
201         public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2)
202         {
203             string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2));
204             Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log);
205             if (isWriteFile)
206             {
207                 LogUtil.WriteLogFile(m_Name, m_Debug, log);
208             }
209         }
210 
211         /// <summary>
212         /// Logs the error message.
213         /// </summary>
214         /// <param name="isWriteFile"></param>
215         /// <param name="message">The message.</param>
216         public void Error(bool isWriteFile, object message)
217         {
218             string log = GetDataTimeLog(message.ToString());
219             Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log);
220             if (isWriteFile)
221             {
222                 LogUtil.WriteLogFile(m_Name, m_Error, log);
223             }
224         }
225 
226         /// <summary>
227         /// Logs the error message.
228         /// </summary>
229         /// <param name="isWriteFile"></param>
230         /// <param name="message">The message.</param>
231         /// <param name="exception">The exception.</param>
232         public void Error(bool isWriteFile, object message, Exception exception)
233         {
234             string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace);
235             Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log);
236             if (isWriteFile)
237             {
238                 LogUtil.WriteLogFile(m_Name, m_Error,log);
239             }
240         }
241 
242         /// <summary>
243         /// Logs the error message.
244         /// </summary>
245         /// <param name="isWriteFile"></param>
246         /// <param name="format">The format.</param>
247         /// <param name="arg0">The arg0.</param>
248         public void ErrorFormat(bool isWriteFile, string format, object arg0)
249         {
250             string log = GetDataTimeLog(string.Format(format, arg0));
251             Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log);
252             if (isWriteFile)
253             {
254                 LogUtil.WriteLogFile(m_Name, m_Error, log);
255             }
256         }
257 
258         /// <summary>
259         /// Logs the error message.
260         /// </summary>
261         /// <param name="isWriteFile"></param>
262         /// <param name="format">The format.</param>
263         /// <param name="args">The args.</param>
264         public void ErrorFormat(bool isWriteFile, string format, params object[] args)
265         {
266             string log = GetDataTimeLog(string.Format(format, args));
267             Console.WriteLine(m_MessageTemplate, m_Name, m_Error,log);
268             if (isWriteFile)
269             {
270                 LogUtil.WriteLogFile(m_Name, m_Error, log);
271             }
272         }
273 
274         /// <summary>
275         /// Logs the error message.
276         /// </summary>
277         /// <param name="isWriteFile"></param>
278         /// <param name="provider">The provider.</param>
279         /// <param name="format">The format.</param>
280         /// <param name="args">The args.</param>
281         public void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args)
282         {
283             string log = GetDataTimeLog(string.Format(provider, format, args));
284             Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log);
285             if (isWriteFile)
286             {
287                 LogUtil.WriteLogFile(m_Name, m_Error, log);
288             }
289         }
290 
291         /// <summary>
292         /// Logs the error message.
293         /// </summary>
294         /// <param name="isWriteFile"></param>
295         /// <param name="format">The format.</param>
296         /// <param name="arg0">The arg0.</param>
297         /// <param name="arg1">The arg1.</param>
298         public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1)
299         {
300             string log = GetDataTimeLog(string.Format(format, arg0, arg1));
301             Console.WriteLine(m_MessageTemplate, m_Name, m_Error,log);
302             if (isWriteFile)
303             {
304                 LogUtil.WriteLogFile(m_Name, m_Error, log);
305             }
306         }
307 
308         /// <summary>
309         /// Logs the error message.
310         /// </summary>
311         /// <param name="isWriteFile"></param>
312         /// <param name="format">The format.</param>
313         /// <param name="arg0">The arg0.</param>
314         /// <param name="arg1">The arg1.</param>
315         /// <param name="arg2">The arg2.</param>
316         public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2)
317         {
318             string log = GetDataTimeLog(string.Format(format, arg0, arg2));
319             Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log);
320             if (isWriteFile)
321             {
322                 LogUtil.WriteLogFile(m_Name, m_Error, log);
323             }
324         }
325 
326         /// <summary>
327         /// Logs the fatal error message.
328         /// </summary>
329         /// <param name="isWriteFile"></param>
330         /// <param name="message">The message.</param>
331         public void Fatal(bool isWriteFile, object message)
332         {
333             string log = GetDataTimeLog(message.ToString());
334             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
335             if (isWriteFile)
336             {
337                 LogUtil.WriteLogFile(m_Name,m_Fatal,log);
338             }
339         }
340 
341         /// <summary>
342         /// Logs the fatal error message.
343         /// </summary>
344         /// <param name="isWriteFile"></param>
345         /// <param name="message">The message.</param>
346         /// <param name="exception">The exception.</param>
347         public void Fatal(bool isWriteFile, object message, Exception exception)
348         {
349             string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace);
350             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
351             if (isWriteFile)
352             {
353                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
354             }
355         }
356 
357         /// <summary>
358         /// Logs the fatal error message.
359         /// </summary>
360         /// <param name="isWriteFile"></param>
361         /// <param name="format">The format.</param>
362         /// <param name="arg0">The arg0.</param>
363         public void FatalFormat(bool isWriteFile, string format, object arg0)
364         {
365             string log = GetDataTimeLog(string.Format(format, arg0));
366             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
367             if (isWriteFile)
368             {
369                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
370             }
371         }
372 
373         /// <summary>
374         /// Logs the fatal error message.
375         /// </summary>
376         /// <param name="isWriteFile"></param>
377         /// <param name="format">The format.</param>
378         /// <param name="args">The args.</param>
379         public void FatalFormat(bool isWriteFile, string format, params object[] args)
380         {
381             string log = GetDataTimeLog(string.Format(format, args));
382             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal,log);
383             if (isWriteFile)
384             {
385                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
386             }
387         }
388 
389         /// <summary>
390         /// Logs the fatal error message.
391         /// </summary>
392         /// <param name="isWriteFile"></param>
393         /// <param name="provider">The provider.</param>
394         /// <param name="format">The format.</param>
395         /// <param name="args">The args.</param>
396         public void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args)
397         {
398             string log = GetDataTimeLog(string.Format(provider, format, args));
399             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
400             if (isWriteFile)
401             {
402                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
403             }
404         }
405 
406         /// <summary>
407         /// Logs the fatal error message.
408         /// </summary>
409         /// <param name="isWriteFile"></param>
410         /// <param name="format">The format.</param>
411         /// <param name="arg0">The arg0.</param>
412         /// <param name="arg1">The arg1.</param>
413         public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1)
414         {
415             string log = GetDataTimeLog(string.Format(format, arg0, arg1));
416             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
417             if (isWriteFile)
418             {
419                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
420             }
421         }
422 
423         /// <summary>
424         /// Logs the fatal error message.
425         /// </summary>
426         /// <param name="isWriteFile"></param>
427         /// <param name="format">The format.</param>
428         /// <param name="arg0">The arg0.</param>
429         /// <param name="arg1">The arg1.</param>
430         /// <param name="arg2">The arg2.</param>
431         public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2)
432         {
433             string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2));
434             Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log);
435             if (isWriteFile)
436             {
437                 LogUtil.WriteLogFile(m_Name, m_Fatal, log);
438             }
439         }
440 
441         /// <summary>
442         /// Logs the info message.
443         /// </summary>
444         /// <param name="isWriteFile"></param>
445         /// <param name="message">The message.</param>
446         public void Info(bool isWriteFile,object message)
447         {
448             string log = GetDataTimeLog(message.ToString());
449             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
450             if (isWriteFile)
451             {
452                 LogUtil.WriteLogFile(m_Name,m_Info,log);
453             }
454         }
455 
456         /// <summary>
457         /// Logs the info message.
458         /// </summary>
459         /// <param name="isWriteFile"></param>
460         /// <param name="message">The message.</param>
461         /// <param name="exception">The exception.</param>
462         public void Info(bool isWriteFile, object message, Exception exception)
463         {
464             string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace);
465             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
466             if (isWriteFile)
467             {
468                 LogUtil.WriteLogFile(m_Name, m_Info, log);
469             }
470         }
471 
472         /// <summary>
473         /// Logs the info message.
474         /// </summary>
475         /// <param name="isWriteFile"></param>
476         /// <param name="format">The format.</param>
477         /// <param name="arg0">The arg0.</param>
478         public void InfoFormat(bool isWriteFile, string format, object arg0)
479         {
480             string log = GetDataTimeLog(string.Format(format, arg0));
481             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
482             if (isWriteFile)
483             {
484                 LogUtil.WriteLogFile(m_Name, m_Info, log);
485             }
486         }
487 
488         /// <summary>
489         /// Logs the info message.
490         /// </summary>
491         /// <param name="isWriteFile"></param>
492         /// <param name="format">The format.</param>
493         /// <param name="args">The args.</param>
494         public void InfoFormat(bool isWriteFile, string format, params object[] args)
495         {
496             string log = GetDataTimeLog(string.Format(format, args));
497             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
498             if (isWriteFile)
499             {
500                 LogUtil.WriteLogFile(m_Name, m_Info, log);
501             }
502         }
503 
504         /// <summary>
505         /// Logs the info message.
506         /// </summary>
507         /// <param name="isWriteFile"></param>
508         /// <param name="provider">The provider.</param>
509         /// <param name="format">The format.</param>
510         /// <param name="args">The args.</param>
511         public void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args)
512         {
513             string log = GetDataTimeLog(string.Format(provider, format, args));
514             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
515             if (isWriteFile)
516             {
517                 LogUtil.WriteLogFile(m_Name, m_Info, log);
518             }
519         }
520 
521         /// <summary>
522         /// Logs the info message.
523         /// </summary>
524         /// <param name="isWriteFile"></param>
525         /// <param name="format">The format.</param>
526         /// <param name="arg0">The arg0.</param>
527         /// <param name="arg1">The arg1.</param>
528         public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1)
529         {
530             string log = GetDataTimeLog(string.Format(format, arg0, arg1));
531             Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log);
532             if (isWriteFile)
533             {
534                 LogUtil.WriteLogFile(m_Name, m_Info, log);
535             }
536         }
537 
538         /// <summary>
539         /// Logs the info message.
540         /// </summary>
541         /// <param name="isWriteFile"></param>
542         /// <param name="format">The format.</param>
543         /// <param name="arg0">The arg0.</param>
544         /// <param name="arg1">The arg1.</param>
545         /// <param name="arg2">The arg2.</param>
546         public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2)
547         {
548             string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2));
549             Console.WriteLine(m_MessageTemplate, m_Name, m_Info,log);
550             if (isWriteFile)
551             {
552                 LogUtil.WriteLogFile(m_Name, m_Info, log);
553             }
554         }
555 
556         /// <summary>
557         /// Logs the warning message.
558         /// </summary>
559         /// <param name="isWriteFile"></param>
560         /// <param name="message">The message.</param>
561         public void Warn(bool isWriteFile,object message)
562         {
563             string log = GetDataTimeLog(message.ToString());
564             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
565             if (isWriteFile)
566             {
567                 LogUtil.WriteLogFile(m_Name,m_Warn,log);
568             }
569         }
570 
571         /// <summary>
572         /// Logs the warning message.
573         /// </summary>
574         /// <param name="isWriteFile"></param>
575         /// <param name="message">The message.</param>
576         /// <param name="exception">The exception.</param>
577         public void Warn(bool isWriteFile, object message, Exception exception)
578         {
579             string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace);
580             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
581             if (isWriteFile)
582             {
583                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
584             }
585         }
586 
587         /// <summary>
588         /// Logs the warning message.
589         /// </summary>
590         /// <param name="isWriteFile"></param>
591         /// <param name="format">The format.</param>
592         /// <param name="arg0">The arg0.</param>
593         public void WarnFormat(bool isWriteFile, string format, object arg0)
594         {
595             string log = GetDataTimeLog(string.Format(format, arg0));
596             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
597             if (isWriteFile)
598             {
599                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
600             }
601         }
602 
603         /// <summary>
604         /// Logs the warning message.
605         /// </summary>
606         /// <param name="isWriteFile"></param>
607         /// <param name="format">The format.</param>
608         /// <param name="args">The args.</param>
609         public void WarnFormat(bool isWriteFile, string format, params object[] args)
610         {
611             string log = GetDataTimeLog(string.Format(format, args));
612             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
613             if (isWriteFile)
614             {
615                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
616             }
617         }
618 
619         /// <summary>
620         /// Logs the warning message.
621         /// </summary>
622         /// <param name="isWriteFile"></param>
623         /// <param name="provider">The provider.</param>
624         /// <param name="format">The format.</param>
625         /// <param name="args">The args.</param>
626         public void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args)
627         {
628             string log = GetDataTimeLog(string.Format(provider, format, args));
629             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
630             if (isWriteFile)
631             {
632                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
633             }
634         }
635 
636         /// <summary>
637         /// Logs the warning message.
638         /// </summary>
639         /// <param name="isWriteFile"></param>
640         /// <param name="format">The format.</param>
641         /// <param name="arg0">The arg0.</param>
642         /// <param name="arg1">The arg1.</param>
643         public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1)
644         {
645             string log = GetDataTimeLog(string.Format(format, arg0, arg1));
646             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
647             if (isWriteFile)
648             {
649                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
650             }
651         }
652 
653         /// <summary>
654         /// Logs the warning message.
655         /// </summary>
656         /// <param name="isWriteFile"></param>
657         /// <param name="format">The format.</param>
658         /// <param name="arg0">The arg0.</param>
659         /// <param name="arg1">The arg1.</param>
660         /// <param name="arg2">The arg2.</param>
661         public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2)
662         {
663             string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2));
664             Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log);
665              if (isWriteFile)
666             {
667                 LogUtil.WriteLogFile(m_Name, m_Warn, log);
668             }
669         }
670     }
671 }
View Code

LogFactory 日志工厂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenXH.Log.Log
{
    public class LogFactory:ILogFactory
    {
        /// <summary>
        /// 创建日志实例
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ILog GetLog(string name)
        {
            return new Log(name);
        }
    }
}

 LogUtil日志格式化

 1 using BenXH.Log.Common;
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace BenXH.Log.Log
 8 {
 9     internal class LogUtil
10     {
11         /// <summary>
12         /// 格式式化Log信息
13         /// </summary>
14         /// <param name="format"></param>
15         /// <param name="name"></param>
16         /// <param name="logType"></param>
17         /// <param name="log"></param>
18         /// <returns></returns>
19         private static string GetLogString(string name, string logType, string log)
20         {
21             return String.Format("[{0}]{1}-{2}: {3}", DateTime.Now.ToString("HH:mm:ss"),name, logType, log);
22         }
23 
24         /// <summary>
25         /// 获得日志要保存的路径
26         /// </summary>
27         /// <param name="name"></param>
28         /// <param name="logType"></param>
29         /// <returns></returns>
30         private static string GetLogPath(string name, string logType)
31         {
32             string path = AppDomain.CurrentDomain.BaseDirectory+"Log";
33             if (!System.IO.Directory.Exists(path))
34             {
35                 System.IO.Directory.CreateDirectory(path);
36             }
37 
38             return System.IO.Path.Combine(path,String.Format("{0}_{1}_{2}.log",DateTime.Now.ToString("yyyy-MM-dd"),name,logType));
39         }
40 
41         public static void WriteLogFile(string name, string logType, string log)
42         {
43             string logPath = GetLogPath(name, logType);
44 
45             FileUtil.WriteAppend(logPath,log);
46         }
47     }
48 }

最后就是一个日志信息的显示和文件的存储   FileUtil

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace BenXH.Log.Common
 8 {
 9     public static class FileUtil
10     {
11         /// <summary>
12         /// 追加内容到指定文件中
13         /// </summary>
14         /// <param name="filePath"></param>
15         /// <param name="content"></param>
16         public static void WriteAppend(string filePath, string content)
17         {
18             WriteAppend(filePath,new string[]{content});
19         }
20 
21         public static void WriteAppend(string filePath, string[] contents)
22         {
23             //System.IO.StreamWriter sr = new System.IO.StreamWriter(filePath, true);
24             //foreach (string c in contents)
25             //{
26             //    sr.WriteLine(c);
27             //}
28             //sr.Flush();
29             //sr.Close();
30 
31             using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
32             {
33                 fs.Seek(fs.Length, SeekOrigin.Current);
34 
35                 string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
36 
37                 byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
38 
39                 fs.Write(data, 0, data.Length);
40 
41                 fs.Close();
42             }
43         }
44     }
45 }

测试代码:

Test.cs

 1  class Test
 2     {
 3         static void Main(string[] args)
 4         {
 5             //日志BenXH的Debug信息
 6             new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "Hello");
 7             new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "World");
 8 
 9             //日志BenXH的info信息
10             new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "Hello");
11             new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "BenXH");
12             Console.Read();
13         }
14     }

运行结果:

文件:程序运行根目录下Log文件夹

Log文件夹下新创建俩个文件DEBUG和INFO

文件:2017-07-29_BenXH_DEBUG.log

文件内容

[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>World

文件:2017-07-29_BenXH_INFO.log

文件内容:

[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>BenXH

工程源文件下载

原文地址:https://www.cnblogs.com/JiYF/p/7256220.html