日志很明顯是幫助大家定位到問題的一個很重要的手段,本來是想直接使用的NLog 來做系統的日志工具,哎傷不起,一變態非要說這個有很多不可控制的因素,這里我給大家講一下我是怎么實現日志模塊的,歡迎拍磚
總體架構圖

• 在這里我把日子的等級分為 跟蹤,BUG 和錯誤 3種 定義枚舉如下
復制代碼 代碼如下:
/// <summary>
/// 日志等級
/// </summary>
public enum Loglevel
{
Track=1,
Bug,
Error
}
復制代碼 代碼如下:
public interface ILogTarget
{
/// <summary>
/// 寫入追蹤信息
/// </summary>
/// <param></param>
void WriteTrack(string LogContent);
/// <summary>
/// 寫入BUG信息
/// </summary>
/// <param></param>
void WriteBug(string LogContent);
/// <summary>
/// 寫入錯誤信息
/// </summary>
/// <param></param>
void WriteError(string LogContent);
}
復制代碼 代碼如下:
/// <summary>
/// 文件日志實現類
/// </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);
}
}
復制代碼 代碼如下:
SmartLog log =new SmartLog (new FileLog());
log.WriteTrack("Hello word");
新聞熱點
疑難解答
圖片精選