




版權(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ì)開(kāi)發(fā)過(guò)程中,是非常重要的,可以供調(diào)試和記錄數(shù)據(jù),雖然說(shuō) 有開(kāi)源的強(qiáng)大日志管理系統(tǒng),比如apache的Log4Net,功能可謂強(qiáng)悍,但是有時(shí) 候,不需要這么大的日志,只需要顯示和文件記錄就可以了,沒(méi)必要用這么重的日 志系統(tǒng),那么就需要自己來(lái)寫(xiě),如下就是一個(gè)簡(jiǎn)單的日志記錄和顯示模塊的設(shè)計(jì)和 實(shí)現(xiàn),如有不足,還望見(jiàn)諒!廢話不多,直入主題。笨小孩日志:BenXHLog類文件設(shè)計(jì):文件結(jié)構(gòu)簡(jiǎn)單,類圖就不畫(huà)了,細(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)開(kāi)看吧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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 計(jì)算機(jī)三級(jí)信息管理考試實(shí)戰(zhàn)經(jīng)驗(yàn)及試題及答案
- 系統(tǒng)分析師考試的案例分析方法及試題及答案
- 西班牙電商面試題及答案
- 健康管理考研試題及答案
- 新疆消防考試題庫(kù)及答案
- 深視智能視覺(jué)面試題及答案
- 現(xiàn)代企業(yè)信息網(wǎng)絡(luò)試題及答案
- 一二三智力測(cè)試題及答案
- 貢獻(xiàn)力量的軟件評(píng)測(cè)師試題及答案
- c1科目四試題及答案900
- 國(guó)家開(kāi)放大學(xué)本科《管理英語(yǔ)3》一平臺(tái)機(jī)考真題及答案總題庫(kù)珍藏版
- 20萬(wàn)噸高塔造粒顆粒硝酸銨工藝安全操作規(guī)程
- CJJ82-2012 園林綠化工程施工及驗(yàn)收規(guī)范
- 江蘇省南京市2022-2023學(xué)年四年級(jí)下學(xué)期數(shù)學(xué)期末試卷(含答案)
- 江蘇省南京市建鄴區(qū)2022-2023學(xué)年五年級(jí)下學(xué)期期末數(shù)學(xué)試卷
- 提高感染性休克集束化治療完成率工作方案
- 肝硬化病人健康宣教課件
- 心力衰竭病人的護(hù)理課件
- 0-3歲兒童適應(yīng)性行為的發(fā)展與教育
- 【多功能自動(dòng)跑步機(jī)機(jī)械結(jié)構(gòu)設(shè)計(jì)4800字(論文)】
- 動(dòng)物生理學(xué)血細(xì)胞計(jì)數(shù)實(shí)驗(yàn)報(bào)告
評(píng)論
0/150
提交評(píng)論