码迷,mamicode.com
首页 > 其他好文 > 详细

微软企业库研究之日志模块

时间:2015-06-25 19:47:08      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

很久之前研究微软的企业库时候写的,要注意需要引用Microsoft.Practices.EnterpriseLibrary.Logging组件库,因为ASP.NET5出来,微软在.netframework中自带了Logger,所以应该以后也不会用上。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;

namespace Enuo.Core.Log
{
    /// <summary>
    /// 标题:微软企业库日志模块调用类
    /// 描述:目前适用Enterprise6.0
    /// 作者:sunjipeng
    /// 日期:2013-12-24
    /// </summary>
    public class EnterpriseLibraryLogger
    {
        #region Variables
        static LogWriter logwriter;
        static LoggingConfiguration config;
        //static LogWriterFactory factory;
        static string headerstyle = "------------Log--------------";
        #endregion

        #region Ctors
        static EnterpriseLibraryLogger()
        {
            TextFormatter brie = new TextFormatter();
            var flatFileTraceListener = new FlatFileTraceListener(DefaultDirectory, headerstyle, string.Empty, brie);
            config = new LoggingConfiguration();
            config.AddLogSource("mylog", SourceLevels.All, true).AddTraceListener(flatFileTraceListener);
            logwriter = new LogWriter(config);

        }
        #endregion

        #region Property Getter Setter
        /// <summary>
        /// 默认的日志路径
        /// </summary>
        public static string DefaultDirectory
        {
            get
            {
                string dirname = "AppLog";
                string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,dirname);
                if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); }
                return dir;
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// 写入调试跟踪日志
        /// </summary>
        public static void WriteDebug(string msg)
        {
            if(logwriter.IsLoggingEnabled())
            {
                LogEntry entry = new LogEntry();
                //事件号
                entry.EventId = 9007;
                entry.Message =  "日志信息";
                //严重等级
                entry.Priority = 9;
                entry.Severity = TraceEventType.Verbose;
                entry.Categories.Add("种类");
                logwriter.Write(entry);
            } 
        }
        /// <summary>
        /// 写入普通消息日志
        /// </summary>
        public void WriteInfo()
        {
            LogEntry entry = new LogEntry();
            entry.Severity = TraceEventType.Information;
            logwriter.Write(entry);
        }
        /// <summary>
        /// 写入可恢复错误日志
        /// </summary>
        public void WriteError()
        {
            LogEntry entry = new LogEntry();
            entry.Severity = TraceEventType.Error;
            logwriter.Write(entry);
        }
        /// <summary>
        /// 写入错误或应用程序崩溃日志
        /// </summary>
        public void WriteCritical()
        {
            LogEntry entry = new LogEntry();
            entry.Severity = TraceEventType.Critical;
            logwriter.Write(entry);
        }
        #endregion
    }
}


微软企业库研究之日志模块

标签:

原文地址:http://my.oschina.net/violetmoon/blog/470837

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!