zoukankan      html  css  js  c++  java
  • LogHelper

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

        }

    }

  • 相关阅读:
    ubuntu配置服务器环境
    discuz安装与学习资料
    前端面试题总结(一)
    css公共样式,初始化
    js的解析--预处理(三)
    sass的安装与基础
    移动开发学习笔记(一) 移动开发的注意事项
    移动前端一些常用的框架
    JavaScript的构造器与对象(二)
    JavaScript 中的Object的使用详解笔记(一)
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/2311781.html
Copyright © 2011-2022 走看看