.NET 日志系統(tǒng)設(shè)計(jì)思路及實(shí)現(xiàn)代碼
日志很明顯是幫助大家定位到問(wèn)題的一個(gè)很重要的手段,本來(lái)是想直接使用的NLog 來(lái)做系統(tǒng)的日志工具,哎傷不起,一變態(tài)非要說(shuō)這個(gè)有很多不可控制的因素,這里我給大家講一下我是怎么實(shí)現(xiàn)日志模塊的,歡迎拍磚
總體架構(gòu)圖

• 在這里我把日子的等級(jí)分為 跟蹤,BUG 和錯(cuò)誤 3種 定義枚舉如下
/// <summary>
/// 日志等級(jí)
/// </summary>
public enum Loglevel
{
Track=1,
Bug,
Error
}
• 這里考慮日志的模塊的可擴(kuò)展性 (這里支持 數(shù)據(jù)庫(kù) 和文件 2種方式) 這里使用適配器模式來(lái)完成本模塊。 這里給大家來(lái)年終福利。貼點(diǎn)代碼
定義一個(gè)接口ILogTarget
public interface ILogTarget
{
/// <summary>
/// 寫入追蹤信息
/// </summary>
/// <param name="LogContent"></param>
void WriteTrack(string LogContent);
/// <summary>
/// 寫入BUG信息
/// </summary>
/// <param name="LogContent"></param>
void WriteBug(string LogContent);
/// <summary>
/// 寫入錯(cuò)誤信息
/// </summary>
/// <param name="LogContent"></param>
void WriteError(string LogContent);
}
• FileLog ,和DBLog 2個(gè)類實(shí)現(xiàn)上面的接口 這里不貼上具體的現(xiàn)實(shí)
/// <summary>
/// 文件日志實(shí)現(xiàn)類
/// </summary>
public class FileLog : ILogTarget
{
public void WriteTrack(string LogContent)
{
throw new NotImplementedException();
}
public void WriteBug(string LogContent)
{
throw new NotImplementedException();
}
public void WriteError(string LogContent)
{
throw new NotImplementedException();
}
}
public class DBLog : ILogTarget
{
public void WriteTrack(string LogContent)
{
throw new NotImplementedException();
}
public void WriteBug(string LogContent)
{
throw new NotImplementedException();
}
public void WriteError(string LogContent)
{
throw new NotImplementedException();
}
}
public class SmartLog
{
private ILogTarget _adaptee;
public SmartLog(ILogTarget tragent)
{
this._adaptee = tragent;
}
public void WriteTrack(string LogContent)
{
_adaptee.WriteTrack(LogContent);
}
public void WriteBug(string LogContent)
{
_adaptee.WriteBug(LogContent);
}
public void WriteError(string LogContent)
{
_adaptee.WriteError(LogContent);
}
}
• 調(diào)用方式
SmartLog log =new SmartLog (new FileLog());
log.WriteTrack("Hello word");
相關(guān)文章
asp.net中利用ashx實(shí)現(xiàn)圖片防盜鏈代碼
直接分析盜鏈原理:看下面用httpwatch截獲的http發(fā)送的數(shù)據(jù)2008-11-11
用C#中的params關(guān)鍵字實(shí)現(xiàn)方法形參個(gè)數(shù)可變
個(gè)人認(rèn)為,提供params關(guān)鍵字以實(shí)現(xiàn)方法形參個(gè)數(shù)可變是C#語(yǔ)法的一大優(yōu)點(diǎn)。在方法形參列表中,數(shù)組類型的參數(shù)前加params關(guān)鍵字,通??梢栽谡{(diào)用方法時(shí)代碼更加精練2012-01-01
用WPF實(shí)現(xiàn)屏幕文字提示的實(shí)現(xiàn)方法
本文介紹WPF應(yīng)用程序?qū)崿F(xiàn)在屏幕上顯示一行或多行文字通知。它沒(méi)有標(biāo)題欄和最大化最小化等按鈕,可以有半透明背景以使文字的顯示更清晰,鼠標(biāo)點(diǎn)擊后提示消失。2013-07-07
asp.net core中Cookie和Session的實(shí)現(xiàn)示例
ASP.NET Core用戶會(huì)話管理主要通過(guò)Cookie和Session實(shí)現(xiàn),本文主要介紹了asp.net core中Cookie和Session的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-01
在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟
這篇文章主要介紹了在ASP.NET Core5.0中訪問(wèn)HttpContext的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
這篇文章主要介紹了asp.net使用npoi讀取excel模板并導(dǎo)出下載的示例,大家參考使用吧2014-01-01
asp.net 模擬提交有文件上傳的表單(通過(guò)http模擬上傳文件)
通過(guò)HTTP模擬GET或POST請(qǐng)求,提交數(shù)據(jù)到服務(wù)端獲取響應(yīng),比較常見(jiàn)些;但如上傳文件到服務(wù)端,使用html form當(dāng)然簡(jiǎn)單了,而因環(huán)境所限有時(shí)需要使用模擬方法去提交有附件(文件上傳)的表單。2010-02-02
asp.net TemplateField模板中的Bind方法和Eval方法
在TemplateField模板中為了能夠有限制的或者取出數(shù)據(jù)庫(kù)中某列的值時(shí),可以用Bind和Eval方法來(lái)實(shí)現(xiàn)。以下是Bind方法的格式,Eval的格式也是和Bind一樣的。 Bind("列的名稱","顯示的格式文")2009-06-06

