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" />