博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LogHelper

Posted on 2012-01-04 11:24  ☆Keep★Moving☆  阅读(606)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Logging;

 

/*================================================================
 
 ==> 使用 <<Enterprise Library 4.1 Logging>> 记录系统Log和异常信息
 
 ==> 使用方法:
        LogHelper.WriteExceptionLog(ex);              
        LogHelper.WriteInformationLog("Test Susscessful!", "Test Function");
 
 ==> Web Config 裡,
        <<===默認為:{Event Log Trace Listener},此會將Log進系統 Window日誌中===>>
        <<===如果要寫txt文件,   請配置為 {Rolling Flat File Trace Listener }==>>

 
 ==> Create By JinHui Ren  2012-01-01
================================================================*/

namespace Utility.Logging
{
    /// <summary>
    ///定义Log优先级常量
    /// </summary>
    public struct Priority
    {
        public const int Lowest = 0;
        public const int Low = 1;
        public const int Normal = 2;
        public const int High = 3;
        public const int Highest = 4;
    }

    /// <summary>
    /// 定义Log种类
    /// </summary>
    public struct Category
    {
        public const string General = "General";
        public const string Trace = "Trace";
    }

    /// <summary>
    ///  Enterprise Library 4.1记录系统Log和异常信息
    /// </summary>
    public static class LogHelper
    {

        static LogHelper() { }
        //public LogHelper(){}

        /// <summary>
        /// 记录log信息
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="msg"></param>
        /// <param name="functionName"></param>
        private static void WriteLog(Exception ex, string category, int priority, string msg, string functionName)
        {
            string logMessage = string.Empty;

            LogEntry log = new LogEntry();
            //log.Categories.Add(Category.Trace);
            //log.Priority = Priority.Normal;

            log.Categories.Add(category);
            log.Priority = priority;

            if (!string.IsNullOrEmpty(functionName))
            {
                logMessage = string.Concat(new object[] { logMessage, "方法名称:", functionName, '\r', '\n' });
            }
            if (!string.IsNullOrEmpty(msg))
            {
                logMessage = string.Concat(new object[] { logMessage, "输出信息:", msg, '\r', '\n' });
            }
            if (ex != null)
            {
                logMessage = string.Concat(new object[] { logMessage, "Source:", ex.Source, '\r', '\n' });
                logMessage = string.Concat(new object[] { logMessage, "错误信息:", ex.Message, '\r', '\n' });
                logMessage = string.Concat(new object[] { logMessage, "详细信息:", '\r', '\n', ex.ToString(), '\r', '\n' });
            }

            log.Message = logMessage;
            Logger.Write(log);
        }


        /// <summary>
        /// 记录异常信息
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteExceptionLog(Exception ex)
        {
            //ExceptionLog按照Category.Trace类别/Priority.High记录
            WriteLog(ex, Category.Trace, Priority.High, "", "");
        }

        /// <summary>
        /// 记录普通Message信息
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="functionName"></param>
        public static void WriteInformationLog(string msg, string functionName)
        {
            //InformationLog按照Category.General类别/Priority.Normal记录
            WriteLog(null, Category.General, Priority.Normal, msg, functionName);
        }

    }

}