.NET 日志系統(tǒng)設(shè)計(jì)思路及實(shí)現(xiàn)代碼
日志很明顯是幫助大家定位到問題的一個很重要的手段,本來是想直接使用的NLog 來做系統(tǒng)的日志工具,哎傷不起,一變態(tài)非要說這個有很多不可控制的因素,這里我給大家講一下我是怎么實(shí)現(xiàn)日志模塊的,歡迎拍磚
總體架構(gòu)圖
• 在這里我把日子的等級分為 跟蹤,BUG 和錯誤 3種 定義枚舉如下
/// <summary>
/// 日志等級
/// </summary>
public enum Loglevel
{
Track=1,
Bug,
Error
}
• 這里考慮日志的模塊的可擴(kuò)展性 (這里支持 數(shù)據(jù)庫 和文件 2種方式) 這里使用適配器模式來完成本模塊。 這里給大家來年終福利。貼點(diǎn)代碼
定義一個接口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>
/// 寫入錯誤信息
/// </summary>
/// <param name="LogContent"></param>
void WriteError(string LogContent);
}
• FileLog ,和DBLog 2個類實(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)文章
.NET?6新特性試用之DateOnly和TimeOnly類型
這篇文章主要介紹了.NET?6新特性試用之DateOnly和TimeOnly類型,主要介紹DateOnly和TimeOnly類型使用過程及存在的一些過程,需要的小伙伴可以參考一下2022-03-03ASP.NET數(shù)據(jù)綁定GridView控件使用技巧
這篇文章主要為大家詳細(xì)介紹了ASP.NET數(shù)據(jù)綁定GridView控件使用技巧,感興趣的小伙伴們可以參考一下2016-03-03.NET讀寫Excel工具Spire.Xls使用 對數(shù)據(jù)操作與控制(4)
這篇文章主要為大家詳細(xì)介紹了.NET讀寫Excel工具Spire.Xls使用,對數(shù)據(jù)操作與控制的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11使用最小?WEB?API?實(shí)現(xiàn)文件上傳的Swagger支持
這篇文章主要介紹了使用最小?WEB?API?實(shí)現(xiàn)文件上傳Swagger支持,我們使用最小?WEB?API?實(shí)現(xiàn)文件上傳功能,雖然客戶端訪問是正常的,但是當(dāng)打開?Swagger?頁面時,沒法使用?Swagger?頁面測試,下面就來一篇支持Swagger的,需要的小伙伴可以參考一下2022-02-02WPF中button按鈕同時點(diǎn)擊多次觸發(fā)click解決方法
這篇文章主要為大家詳細(xì)介紹了WPF中button按鈕同時點(diǎn)擊多次觸發(fā)click的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04配置Visual Studio 以調(diào)試.net framework源代碼
看到.net框架代碼發(fā)布了,興奮了一下,把在Visual Studio 2008上配置的內(nèi)容翻譯了一下,只翻譯了原文的基本步驟,高級用戶篇和QA沒有翻譯。2009-04-04