asp.Netのlog 4 netのhelperクラス

69320 ワード

using log4net;
using System;
using System.Diagnostics;

namespace MxWeiXinPF.Common.log
{
    public static class LogHelper
    {
        // private static LogMessage message = null;

        #region error   、        

        /// 
        ////// 
        /// 
        public static void Error(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message);
        }

        /// 
        ////// 
        /// 
        /// 
        public static void Error(object message, Exception exception)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception);
        }

        /// 
        ////// 
        ///     
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Error(string message, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();

            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(lm);
        }

        /// 
        ////// 
        ///     
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Error(string message, Exception exception, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();

            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;
            LogManager.GetLogger(GetCurrentMethodFullName()).Error(lm, exception);
        }

        #endregion

        #region info   ,          

        /// 
        ////// 
        /// 
        public static void Info(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message);
        }

        /// 
        ////// 
        /// 
        /// 
        public static void Info(object message, Exception ex)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex);
        }

        /// 
        ////// 
        ///     
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Info(string message, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();

            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;

            LogManager.GetLogger(GetCurrentMethodFullName()).Info(lm);
        }

        /// 
        ////// 
        ///     
        ///   
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Info(string message, Exception ex, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();

            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;
            LogManager.GetLogger(GetCurrentMethodFullName()).Info(lm, ex);
        }

        #endregion

        #region debug       

        /// 
        ///       
        /// 
        /// 
        public static void Debug(object message)
        {
           
                LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message);
            
        }

        /// 
        ///        
        /// 
        /// 
        /// 
        public static void Debug(object message, Exception ex)
        {
            
                LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex);
             
        }

        /// 
        ///       
        /// 
        ///     
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Debug(string message, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
           
                LogMessage lm = new LogMessage();

                lm.userid = userid;
                lm.platName = platName;
                lm.module = module;
                lm.operating = operating;
                lm.flgValue = flgValue;
                lm.addip = Utils.getIPAddress();
                lm.remark = message;
                LogManager.GetLogger(GetCurrentMethodFullName()).Debug(lm);
             
        }

        #endregion

        #region warin   

        public static void Warn(object message)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message);
        }

        public static void Warn(object message, Exception ex)
        {
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(message, ex);
        }

        /// 
        ////// 
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        ///     
        public static void Warn(string message, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();

            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(lm);
        }

        /// 
        ////// 
        ///      
        /// userid
        ///   
        ///   
        ///     
        ///  
        /// ip
        public static void Warn(string message, Exception exception, int userid = 0, string platName = "", string module = "", string operating = "", string flgValue = "")
        {
            LogMessage lm = new LogMessage();
            lm.userid = userid;
            lm.platName = platName;
            lm.module = module;
            lm.operating = operating;
            lm.flgValue = flgValue;
            lm.addip = Utils.getIPAddress();
            lm.remark = message;
            LogManager.GetLogger(GetCurrentMethodFullName()).Warn(lm, exception);
        }

        #endregion

        private static string GetCurrentMethodFullName()
        {
            StackFrame frame;
            string MethodFunStr = "";
            string MethodFullNameStr = "";
            // bool flag;
            try
            {
                int num = 2;
                StackTrace stackTrace = new StackTrace();
                int length = stackTrace.GetFrames().Length;
                //do
                //{
                int num1 = num;
                // num = num1 + 1;
                frame = stackTrace.GetFrame(num1);
                MethodFunStr = frame.GetMethod().DeclaringType.ToString();
                // flag = (!MethodFunStr.EndsWith("Exception") ? false : num < length);
                //}
                //while (flag);
                string name = frame.GetMethod().Name;
                MethodFullNameStr = string.Concat(MethodFunStr, ".", name);
            }
            catch (Exception ex)
            {
                string exMessage = ex.Message;
                MethodFullNameStr = exMessage.Substring(0, exMessage.Length > 200 ? 200 : exMessage.Length);

                LogManager.GetLogger("    ").Error("GetCurrentMethodFullName()     !!!", ex);
            }
            return MethodFullNameStr;

            // return "TestName";
        }
    }
}

log4net.config

"1.0" encoding="utf-8"?>


  
    
"log4net" type="log4net.Config.log4NetConfigurationSectionHandler,log4net" />
"ADONetAppenderMySqlClient" type="log4net.Appender.ADONetAppender"> "0" /> "ReconnectOnError" value="true"/> "MySql.Data.MySqlClient.MySqlConnection, MySql.Data" /> "server=10.1.0.228;user id=lijun;pwd=520777;port=3306;pooling=True;database=hurongclub_log;charset=utf8;" providerName="MySql.Data.MySqlClient;" /> "INSERT INTO loginfo (ID,CreateTime,Thread,Level,Logger,Message,Exception,UserID,PlatName,Module,Operation,DeviceID,HRVersion,CustomerID,RequestUrl,RequestIPs) VALUES(@ID,@log_date,@thread,@log_level,@logger,@message,@exception,@UserID,@PlatName,@Module,@Operation,@DeviceID,@HRVersion,@CustomerID,@RequestUrl,@RequestIPs)" /> "@log_date" /> "DateTime" /> "log4net.Layout.RawTimeStampLayout" /> "@thread" /> "String" /> "255" /> "log4net.Layout.PatternLayout"> "%thread" /> "@log_level" /> "String" /> "50" /> "log4net.Layout.PatternLayout"> "%level" /> "@logger" /> "String" /> "255" /> "log4net.Layout.PatternLayout"> "%logger" /> "@message" /> "String" /> "4000" /> "log4net.Layout.PatternLayout"> "%message" /> "@exception" /> "String" /> "2000" /> "log4net.Layout.ExceptionLayout" /> "@ID" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{ID}" /> "@UserID" /> "Int32" /> "HuRongClub.Log.CustomLayout"> "%property{UserID}" /> "@PlatName" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{PlatName}" /> "@Module" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{Module}" /> "@Operation" /> "String" /> "200" /> "HuRongClub.Log.CustomLayout"> "%property{Operation}" /> "@RequestIPs" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{RequestIPs}" /> "@RequestUrl" /> "String" /> "500" /> "HuRongClub.Log.CustomLayout"> "%property{RequestUrl}" /> "@DeviceID" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{DeviceID}" /> "@HRVersion" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{HRVersion}" /> "@CustomerID" /> "String" /> "10" /> "HuRongClub.Log.CustomLayout"> "%property{CustomerID}" /> "log4net.Filter.LevelRangeFilter"> "LevelMin" value="INFO" /> "LevelMax" value="ERROR" /> "InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> "File" value="logfile\Info\" /> "AppendToFile" value="true" /> "datePattern" value="yyyy-MM-dd.'Info.LOG'" /> "staticLogFileName" value="false" /> "rollingStyle" value="Composite" /> "MaxSizeRollBackups" value="-1" /> "MaximumFileSize" value="5000KB" /> "log4net.Layout.PatternLayout"> "%date -[%thread] %-5level -%logger %message %newline%newline" /> "log4net.Filter.LevelRangeFilter"> "LevelMin" value="INFO" /> "LevelMax" value="INFO" /> "DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender"> "File" value="logfile\Debug\" /> "AppendToFile" value="true" /> "datePattern" value="yyyy-MM-dd.'debug.LOG'" /> "staticLogFileName" value="false" /> "rollingStyle" value="Composite" /> "MaxSizeRollBackups" value="-1" /> "MaximumFileSize" value="5000KB" /> "log4net.Layout.PatternLayout"> "%date -[%thread] %-5level -%logger %message %newline%newline" /> "log4net.Filter.LevelRangeFilter"> "LevelMin" value="DEBUG" /> "LevelMax" value="DEBUG" /> "WarningRollingFileAppender" type="log4net.Appender.RollingFileAppender"> "File" value="logfile\Warn\" /> "AppendToFile" value="true" /> "datePattern" value="yyyy-MM-dd.'warn.log'" /> "staticLogFileName" value="false" /> "rollingStyle" value="Composite" /> "MaxSizeRollBackups" value="-1" /> "MaximumFileSize" value="5000KB" /> "log4net.Layout.PatternLayout"> "%n :%date %n :%-5level %n :%logger %n :%message %newline%newline" /> "log4net.Filter.LevelRangeFilter"> "LevelMin" value="WARN" /> "LevelMax" value="WARN" /> "ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> "File" value="logfile\Error\" /> "AppendToFile" value="true" /> "datePattern" value="yyyy-MM-dd.'error.LOG'" /> "staticLogFileName" value="false" /> "rollingStyle" value="Composite" /> "MaxSizeRollBackups" value="-1" /> "MaximumFileSize" value="5000KB" /> "log4net.Layout.PatternLayout"> "%n :%date %n :%-5level %n :%logger %n :%message %newline%newline" /> "log4net.Filter.LevelRangeFilter"> "LevelMin" value="Error" /> "LevelMax" value="ERROR" /> "All" /> ref ref="ErrorRollingFileAppender" /> ref ref="DebugRollingFileAppender" /> ref ref="WarningRollingFileAppender" /> ref ref="InfoRollingFileAppender" /> "v4.0" sku=".NETFramework,Version=v4.0" />