NLog for Linq2Sql

NLog for Linq2Sql

linq自動產生的sql指令, 可能會是沒有效率的, 故需要log 出來, 再進行調整.

應用 DataContext的屬性Log:System.IO.TextWriter 再加上NLog

合成一個好用的 class

 

using System.IO;
using NLog;
using System.ComponentModel;
using System.Text;

namespace Common.NMail.Core {
    /// <summary>
    /// 包裝NLog.Logger物件為TextWriter型別
    /// 以利LinqToSql的Log用.
    /// </summary>
    public class NLogTextWriter : TextWriter {
        /// <summary>
        /// 日誌物件
        /// proxy pattern::有需要時再建立
        /// </summary>
        private Logger _logger = null;
        /// <summary>
        /// 日誌層級::預設為Info
        /// <seealso cref="NLog.LogLevel"/>
        /// </summary>
        private LogLevel _level = NLog.LogLevel.Info;
        /// <summary>
        /// 預設建構子, 自動產生日誌物件
        /// </summary>
        public NLogTextWriter() {            
        }

        /// <summary>
        /// 預設建構子, 自動產生日誌物件, 並修改預設的日誌層級
        /// </summary>
        public NLogTextWriter(NLog.LogLevel level) {
            if (level != null) {
                _level = level;
            }
        }

        /// <summary>
        /// 使用已建立的日誌物件
        /// </summary>
        /// <param name="logger"></param>
        public NLogTextWriter(NLog.Logger logger) {
            _logger = logger;            
        }
        /// <summary>
        /// 使用已建立的日誌物件, 並修改預設的日誌層級
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="level"></param>
        public NLogTextWriter(NLog.Logger logger,NLog.LogLevel level) {
            _logger = logger;
            _level = level;
        }
        /// <see cref="System.IO.TextWriter"/>
        public override void Write(char[] buffer) {
            string value = new string(buffer);
            Write(value);
        }
        /// <see cref="System.IO.TextWriter"/>
        public override void Write(char[] buffer, int index, int count) {
            Write(new string(buffer, index, count));
        }
        /// <see cref="System.IO.TextWriter"/>
        public override void WriteLine(char[] buffer) {
            Write(new StringBuilder(new string(buffer)).Append("\r\n").ToString());
        }
        /// <see cref="System.IO.TextWriter"/>
        public override void WriteLine(char[] buffer, int index, int count) {
            Write(new StringBuilder(new string(buffer, index, count)).Append("\r\n").ToString());
        }
        /// <see cref="System.IO.TextWriter"/>
        public override void WriteLine(string value) {
            Write(new StringBuilder(value).Append("\r\n").ToString());
        }

        /// <see cref="System.IO.TextWriter"/>
        public override void Write(string value) {     
            if(_logger==null){
                _logger = LogManager.GetCurrentClassLogger();
            }
            _logger.Log(_level, value);                        
        }

        /// <summary>
        /// (唯讀)系統輸出文字的編碼方式
        /// </summary>
        public override System.Text.Encoding Encoding {            
            get {
                return System.Text.Encoding.Default;
            }
        }
    }
}