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

        }

    }

  • 相关阅读:
    MCU软件最佳实践——独立按键
    MCU软件最佳实践——矩阵键盘驱动
    MCU软件最佳实践——使用printf打印数据
    CAP定理图解证明
    类型和变量
    数字ID过长 精度丢失 (已解决:后端方案)
    Springboot 异步线程池配置(小型应用)
    Java 数字 字符串 简单操作
    Java 网络请求
    Java 时间 日 周 月 季 年
  • 原文地址:https://www.cnblogs.com/guyuehuanhuan/p/2311781.html
Copyright © 2011-2022 走看看