日志很明顯是幫助大家定位到問題的一個(gè)很重要的手段,本來是想直接使用的NLog 來做系統(tǒng)的日志工具,哎傷不起,一變態(tài)非要說這個(gè)有很多不可控制的因素,這里我給大家講一下我是怎么實(shí)現(xiàn)日志模塊的,歡迎拍磚
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);
}
/// 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);
}
}
SmartLog log =new SmartLog (new FileLog());
log.WriteTrack("Hello word");