国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.NET 日志系統設計思路及實現代碼

2024-07-10 13:18:27
字體:
來源:轉載
供稿:網友

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

總體架構圖

.NET 日志系統設計思路及實現代碼

•    在這里我把日子的等級分為 跟蹤,BUG 和錯誤 3種  定義枚舉如下

復制代碼 代碼如下:


/// <summary>
    /// 日志等級
    /// </summary>
    public enum Loglevel
    {
        Track=1,
        Bug,
        Error
    }


•    這里考慮日志的模塊的可擴展性 (這里支持 數據庫 和文件 2種方式)  這里使用適配器模式來完成本模塊。 這里給大家來年終福利。貼點代碼
定義一個接口ILogTarget

復制代碼 代碼如下:


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);

    }



•     FileLog ,和DBLog 2個類實現上面的接口 這里不貼上具體的現實

復制代碼 代碼如下:


/// <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");

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻江县| 石城县| 额济纳旗| 新化县| 柏乡县| 耒阳市| 会泽县| 阳东县| 扎兰屯市| 甘泉县| 黔东| 三台县| 平谷区| 黑河市| 平舆县| 扶绥县| 观塘区| 杭锦旗| 道真| 小金县| 娱乐| 财经| 雷州市| 南川市| 利川市| 泰宁县| 东乌| 沙坪坝区| 桂平市| 北票市| 长沙市| 思茅市| 康乐县| 石狮市| 青岛市| 兴仁县| 沅江市| 青冈县| 宜良县| 双鸭山市| 陇川县|