C#日志记录设计与实现.docx
《C#日志记录设计与实现.docx》由会员分享,可在线阅读,更多相关《C#日志记录设计与实现.docx(13页珍藏版)》请在冰点文库上搜索。
C#日志记录设计与实现
C#日志记录设计与实现
日志记录:
日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和实现,如有不足,还望见谅!
废话不多,直入主题。
笨小孩日志:
BenXHLog
类文件设计:
文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,
程序代码:
Ilog接口
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Text;
5
6namespaceBenXH.Log.Log
7{
8publicinterfaceILog
9{
10boolIsDebugEnabled{get;}
11
12boolIsErrorEnabled{get;}
13
14boolIsFatalEnabled{get;}
15
16boolIsInfoEnabled{get;}
17
18boolIsWarnEnabled{get;}
19
20voidDebug(boolisWriteFile,objectmessage);
21
22voidDebug(boolisWriteFile,objectmessage,Exceptionexception);
23
24voidDebugFormat(boolisWriteFile,stringformat,objectarg0);
25
26voidDebugFormat(boolisWriteFile,stringformat,paramsobject[]args);
27
28voidDebugFormat(boolisWriteFile,IFormatProviderprovider,stringformat,paramsobject[]args);
29
30voidDebugFormat(boolisWriteFile,stringformat,objectarg0,objectarg1);
31
32voidDebugFormat(boolisWriteFile,stringformat,objectarg0,objectarg1,objectarg2);
33
34voidError(boolisWriteFile,objectmessage);
35
36voidError(boolisWriteFile,objectmessage,Exceptionexception);
37
38voidErrorFormat(boolisWriteFile,stringformat,objectarg0);
39
40voidErrorFormat(boolisWriteFile,stringformat,paramsobject[]args);
41
42voidErrorFormat(boolisWriteFile,IFormatProviderprovider,stringformat,paramsobject[]args);
43
44voidErrorFormat(boolisWriteFile,stringformat,objectarg0,objectarg1);
45
46voidErrorFormat(boolisWriteFile,stringformat,objectarg0,objectarg1,objectarg2);
47
48voidFatal(boolisWriteFile,objectmessage);
49
50voidFatal(boolisWriteFile,objectmessage,Exceptionexception);
51
52voidFatalFormat(boolisWriteFile,stringformat,objectarg0);
53
54voidFatalFormat(boolisWriteFile,stringformat,paramsobject[]args);
55
56voidFatalFormat(boolisWriteFile,IFormatProviderprovider,stringformat,paramsobject[]args);
57
58voidFatalFormat(boolisWriteFile,stringformat,objectarg0,objectarg1);
59
60voidFatalFormat(boolisWriteFile,stringformat,objectarg0,objectarg1,objectarg2);
61
62voidInfo(boolisWriteFile,objectmessage);
63
64voidInfo(boolisWriteFile,objectmessage,Exceptionexception);
65
66voidInfoFormat(boolisWriteFile,stringformat,objectarg0);
67
68voidInfoFormat(boolisWriteFile,stringformat,paramsobject[]args);
69
70voidInfoFormat(boolisWriteFile,IFormatProviderprovider,stringformat,paramsobject[]args);
71
72voidInfoFormat(boolisWriteFile,stringformat,objectarg0,objectarg1);
73
74voidInfoFormat(boolisWriteFile,stringformat,objectarg0,objectarg1,objectarg2);
75
76voidWarn(boolisWriteFile,objectmessage);
77
78voidWarn(boolisWriteFile,objectmessage,Exceptionexception);
79
80voidWarnFormat(boolisWriteFile,stringformat,objectarg0);
81
82voidWarnFormat(boolisWriteFile,stringformat,paramsobject[]args);
83
84voidWarnFormat(boolisWriteFile,IFormatProviderprovider,stringformat,paramsobject[]args);
85
86voidWarnFormat(boolisWriteFile,stringformat,objectarg0,objectarg1);
87
88voidWarnFormat(boolisWriteFile,stringformat,objectarg0,objectarg1,objectarg2);
89}
90}
ILogFactory工厂接口
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceBenXH.Log.Log
{
publicinterfaceILogFactory
{
ILogGetLog(stringname);
}
}
日志类Log这个代码实现多一点,合并了,点开看吧
ViewCode
LogFactory日志工厂
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceBenXH.Log.Log
{
publicclassLogFactory:
ILogFactory
{
///
///创建日志实例
///
///
///
publicILogGetLog(stringname)
{
returnnewLog(name);
}
}
}
LogUtil日志格式化
1usingBenXH.Log.Common;
2usingSystem;
3usingSystem.Collections.Generic;
4usingSystem.Linq;
5usingSystem.Text;
6
7namespaceBenXH.Log.Log
8{
9internalclassLogUtil
10{
11///
12///格式式化Log信息
13///
14///
15///
16///
17///
18///
19privatestaticstringGetLogString(stringname,stringlogType,stringlog)
20{
21returnString.Format("[{0}]{1}-{2}:
{3}",DateTime.Now.ToString("HH:
mm:
ss"),name,logType,log);
22}
23
24///
25///获得日志要保存的路径
26///
27///
28///
29///
30privatestaticstringGetLogPath(stringname,stringlogType)
31{
32stringpath=AppDomain.CurrentDomain.BaseDirectory+"Log";
33if(!
System.IO.Directory.Exists(path))
34{
35System.IO.Directory.CreateDirectory(path);
36}
37
38returnSystem.IO.Path.Combine(path,String.Format("{0}_{1}_{2}.log",DateTime.Now.ToString("yyyy-MM-dd"),name,logType));
39}
40
41publicstaticvoidWriteLogFile(stringname,stringlogType,stringlog)
42{
43stringlogPath=GetLogPath(name,logType);
44
45FileUtil.WriteAppend(logPath,log);
46}
47}
48}
最后就是一个日志信息的显示和文件的存储 FileUtil
1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.IO;
4usingSystem.Linq;
5usingSystem.Text;
6
7namespaceBenXH.Log.Common
8{
9publicstaticclassFileUtil
10{
11///
12///追加内容到指定文件中
13///
14///
15///
16publicstaticvoidWriteAppend(stringfilePath,stringcontent)
17{
18WriteAppend(filePath,newstring[]{content});
19}
20
21publicstaticvoidWriteAppend(stringfilePath,string[]contents)
22{
23//System.IO.StreamWritersr=newSystem.IO.StreamWriter(filePath,true);
24//foreach(stringcincontents)
25//{
26//sr.WriteLine(c);
27//}
28//sr.Flush();
29//sr.Close();
30
31using(FileStreamfs=newFileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write,FileShare.ReadWrite))
32{
33fs.Seek(fs.Length,SeekOrigin.Current);
34
35stringcontent=String.Join(Environment.NewLine,contents)+Environment.NewLine;
36
37byte[]data=System.Text.Encoding.UTF8.GetBytes(content);
38
39fs.Write(data,0,data.Length);
40
41fs.Close();
42}
43}
44}
45}
测试代码:
Test.cs
1classTest
2{
3staticvoidMain(string[]args)
4{
5//日志BenXH的Debug信息
6newBenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true,"Hello");
7newBenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true,"World");
8
9//日志BenXH的info信息
10newBenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true,"Hello");
11newBenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true,"BenXH");
12Console.Read();
13}
14}
运行结果:
文件:
程序运行根目录下Log文件夹
Log文件夹下新创建俩个文件DEBUG和INFO
文件:
2017-07-29_BenXH_DEBUG.log
文件内容
[17-07-2916:
01:
26]>>Hello
[17-07-2916:
01:
26]>>World
文件:
2017-07-29_BenXH_INFO.log
文件内容:
[17-07-2916:
01:
26]>>Hello
[17-07-2916:
01:
26]>>BenXH