C#日志记录设计与实现_第1页
C#日志记录设计与实现_第2页
C#日志记录设计与实现_第3页
C#日志记录设计与实现_第4页
C#日志记录设计与实现_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#日志记录设计与实现日志记录:日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说 有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时 候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日 志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和 实现,如有不足,还望见谅!废话不多,直入主题。笨小孩日志:BenXHLog类文件设计:文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,程序代码:Ilog 接口using using using using123456789101112131

2、41516171819202122 exceptio n);2324arg0);2526 object2728stri ng2927 object3128 object33343536System;System.Collectio ns.Ge neric; Syste m丄 i nq;System.Text;n amespaceBe nXH丄 og Log public in terfaceILogboolboolboolboolboolvoidvoidvoidvoidargs);void format,IsDebugE nabled IsErrorE nabled IsFatalEnable

3、d IsInfoEn abled IsWarnE nabled get;get; get; get; get; Debug( bool isWriteFile, object message);Debug( bool isWriteFile, object message, ExceptionDebugFormat( bool isWriteFile,DebugFormat( bool isWriteFile,DebugFormat(bool isWriteFile, params object args);void DebugFormat(bool isWriteFile, arg1);vo

4、id DebugFormat(bool isWriteFile, arg1, object arg2);void Error( bool isWriteFile,void Error( bool isWriteFile,stri ng format,stri ng format,IFormatProviderobjectparamsprovider,string format, object arg0string format, object arg0object message);object message, Exception3738void ErrorFormat( bool isWr

5、iteFile,stri ngformat,objectarg0);3940void ErrorFormat( bool isWriteFile,stri ngformat,paramsobjectargs);4142void ErrorFormat( bool isWriteFile,IFormatProviderprovider,stri ngformat, params object args);4344void ErrorFormat( bool isWriteFile,stri ngformat,object arg0,objectarg1);4546void ErrorFormat

6、( bool isWriteFile,stri ngformat,object arg0,object argl, object arg2);4748 void Fatal( bool isWriteFile,object message);4949 void Fatal( bool isWriteFile,object message, Exceptionexceptio n);5152void FatalFormat( bool isWriteFile,stri ngformat,objectarg0);5354void FatalFormat( bool isWriteFile,stri

7、 ngformat,paramsobjectargs);5556void FatalFormat( bool isWriteFile,IFormatProviderprovider,stri ngformat, params object args);5758void FatalFormat( bool isWriteFile,stri ngformat,object arg0,objectarg1);5960void FatalFormat( bool isWriteFile,stri ngformat,object arg0,object arg1, object arg2);6162 v

8、oid Info( bool isWriteFile,object message);6363 void Info( bool isWriteFile,object message, Exceptionexceptio n);6564 void In foFormat( bool isWriteFile,stri ng format, objectargO);6768void In foFormat( bool isWriteFile,stri ng format, paramsobjectargs);6970void InfoFormat( bool isWriteFile, IFormat

9、Provider provider,stri ngformat, params object args);7172void InfoFormat( bool isWriteFile,string format, object arg0,objectarg1);7374void InfoFormat( bool isWriteFile,string format, object arg0,objectarg1, object arg2);7576void Warn (bool isWriteFile,object message);7778void Warn( bool isWriteFile,

10、object message, Exceptionexceptio n);7980void WarnFormat( bool isWriteFile,stri ng format, objectargO);8182void WarnFormat( bool isWriteFile,stri ng format, paramsobjectargs);8384void WarnFormat(bool isWriteFile, IFormatProvider provider,stri ngformat, params object args);8586void WarnFormat(bool is

11、WriteFile,string format, object arg0,objectarg1);8788void WarnFormat(bool isWriteFile,string format, object arg0,objectarg1, object arg2);89 90 副ILogFactory工厂接口电usingSystem;usingSystem.Collectio ns.Ge neric;usingSyste m丄inq;usingSystem.Text;n amespaceBe nXH丄 og Logpublic in terfaceILogFactoryILog Ge

12、tLog( stri ng n ame);日志类Log这个代码实现多一点,合并了,点开看吧J View CodeLogFactory日志工厂using System;using System.Collections.Generic;using System丄inq;using System.Text;n amespaceBe nXH丄 og Logpublicclass LogFactory:ILogFactoryIIIIII创建日志实例IIIIIIvparam n ame=n ameIIIpublic ILog GetLog( string name) return n ewLog( nam

13、e);LogUtil日志格式化1 using BenXH丄og.Common;2 using System;3 using System.Collections.Generic;4 using System丄inq;5678using System.Text;9internalclass LogUtil10 11III12III格式式化Log信息13III14III15III16III17III18III19private staticstring GetLogString( stringlogType,stri nglog)20 21return String.Format( 01-2: 3

14、DateTime.Now.ToString( HH:mm:ss),name, logType, log);22 2324III25III获得日志要保存的路径26III27III28III29IIIprivate static string GetLogPath(string name, string logType)name, stringn amespaceBe nXH丄 og Log 303132stri ng path =AppDomain.CurrentDomain.BaseDirectory+ Log;333435363738if (!System.lO.Directory.Exis

15、ts(path)System.IO.Directory.CreateDirectory(path);returnSystem.lO.Path.Combi ne(path,Stri ng.Format(_1_2ogQateTime.Now.ToString( yyyy-MM-dd),name,logType);394041stri ngpublic static void WriteLogFile( string name, string logType, log)4243 stri ng logPath = GetLogPath( name, logType);4444 FileUtil.Wr

16、iteAppe nd(logPath,log);45 46 47 最后就是一个日志信息的显示和文件的存储FileUtil1 using System;2 using System.Collections.Generic;3 using System.10;4 using System丄inq;5 using System.Text;68 9publicstatic class FileUtil10 11III12III追加内容到指定文件中13III14III15III16public static void WriteAppend( string7 namespaceBenXULog.Comm

17、onfilePath,stri ngstring 24252627282930content)17 18 WriteAppend(filePath,new string content);19 2020 public static void WriteAppend( string filePath, conten ts)21 22 /Syste m.I O.StreamWriter sr = newSystem.IO.StreamWriter(filePath, true);/foreach (stri ng c in conten ts) / sr.WriteLi ne(c);/sr.Flu

18、sh();sr.Close();31 using (FileStream fs =n ewFileStream(filePath,FileMode.Ope nOrCreate, FileAccess.Write, FileShare.ReadWrite)32 33 fs.Seek(fs.Le ngth, SeekOrigi n.Curre nt);3434 stri ng content = Stri ng.Joi n(E nviro nmen t.NewLi ne, conten ts) + En viro nmen t.NewLi ne;3635 byte data =System.Tex

19、t.E ncodi ng.UTF8.GetBytes(co nte nt);3836 fs.Write(data,0, data.Le ngth);4037 fs.Close();38 39 40 41 BenXH).Debug( true , BenXH).Debug( true , 信息BenXHJ.Info( true ,BenXHJ.Info( true ,Hello);World);Hello);BenXH);测试代码:Test.cs1classTest23static void Main( string args)45/ 日志 BenXH的 Debug信息6newBen XH.Log.Log.LogFactory().GetLog(6 newBen XH.Log.Log.LogFactory().GetLog(87 / 日志BenXH的info8 newBen XH.Log.Log.LogFactory().GetLog(9 newBen XH.Log.Log.LogFactor

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论