C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)_第1頁(yè)
C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)_第2頁(yè)
C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)_第3頁(yè)
C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)_第4頁(yè)
C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、C#日志記錄設(shè)計(jì)與實(shí)現(xiàn)日志記錄:日志記錄在程序設(shè)計(jì)開發(fā)過(guò)程中,是非常重要的,可以供調(diào)試和記錄數(shù)據(jù),雖然說(shuō) 有開源的強(qiáng)大日志管理系統(tǒng),比如apache的Log4Net,功能可謂強(qiáng)悍,但是有時(shí) 候,不需要這么大的日志,只需要顯示和文件記錄就可以了,沒(méi)必要用這么重的日 志系統(tǒng),那么就需要自己來(lái)寫,如下就是一個(gè)簡(jiǎn)單的日志記錄和顯示模塊的設(shè)計(jì)和 實(shí)現(xiàn),如有不足,還望見(jiàn)諒!廢話不多,直入主題。笨小孩日志:BenXHLog類文件設(shè)計(jì):文件結(jié)構(gòu)簡(jiǎn)單,類圖就不畫了,細(xì)心的已經(jīng)發(fā)現(xiàn)了,這就是一個(gè)簡(jiǎn)單工廠模式,程序代碼: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這個(gè)代碼實(shí)現(xiàn)多一點(diǎn),合并了,點(diǎn)開看吧J View CodeLogFactory日志工廠using System;using System.Collections.Generic;using System丄inq;using System.Text;n amespaceBe nXH丄 og Logpublicclass LogFactory:ILogFactoryIIIIII創(chuàng)建日志實(shí)例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 最后就是一個(gè)日志信息的顯示和文件的存儲(chǔ)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追加內(nèi)容到指定文件中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);測(cè)試代碼: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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論