using log4net; using System; using System.Diagnostics; namespace MxWeiXinPF.Common.log { public static class LogHelper { // private static LogMessage message = null; #region error 错误、异常时候记录日志 /// <summary> /// 错误、异常时候记录日志 /// </summary> /// <param name="message"></param> public static void Error(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message); } /// <summary> /// 错误、异常 记录日志 /// </summary> /// <param name="message"></param> /// <param name="exception"></param> public static void Error(object message, Exception exception) { LogManager.GetLogger(GetCurrentMethodFullName()).Error(message, exception); } /// <summary> /// 错误、异常 记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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); } /// <summary> /// 错误、异常 记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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 请求,用户行为 记录日志 /// <summary> /// 请求,用户行为 记录日志 /// </summary> /// <param name="message"></param> public static void Info(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message); } /// <summary> /// 请求,用户行为 记录日志 /// </summary> /// <param name="message"></param> /// <param name="ex"></param> public static void Info(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Info(message, ex); } /// <summary> /// 请求,用户行为 记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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); } /// <summary> /// 请求,用户行为 记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="ex">异常</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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 调试记录日志 /// <summary> /// 调试记录日志 /// </summary> /// <param name="message"></param> public static void Debug(object message) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message); } /// <summary> /// 调试记录日志 /// </summary> /// <param name="message"></param> /// <param name="ex"></param> public static void Debug(object message, Exception ex) { LogManager.GetLogger(GetCurrentMethodFullName()).Debug(message, ex); } /// <summary> /// 调试记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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); } /// <summary> /// 错误、异常 记录日志 /// </summary> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> /// <param name="message">记录内容</param> 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); } /// <summary> /// 错误、异常 记录日志 /// </summary> /// <param name="message">记录内容</param> /// <param name="userid">userid</param> /// <param name="platName">平台</param> /// <param name="module">模块</param> /// <param name="operating">操作行为</param> /// <param name="flgValue">值</param> /// <param name="addip">ip</param> 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文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.log4NetConfigurationSectionHandler,log4net" /> </configSections> <log4net> <!--数据库记录--> <!--错误日志--> <appender name="ADONetAppenderMySqlClient" type="log4net.Appender.ADONetAppender"> <bufferSize value="0" /> <param name="ReconnectOnError" value="true"/> <connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" /> <connectionString value="server=10.1.0.228;user id=lijun;pwd=520777;port=3306;pooling=True;database=hurongclub_log;charset=utf8;" providerName="MySql.Data.MySqlClient;" /> <commandText value="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)" /> <!--logger4net--> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@thread" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%thread" /> </layout> </parameter> <parameter> <parameterName value="@log_level" /> <dbType value="String" /> <size value="50" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@logger" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> <!--ID--> <parameter> <parameterName value="@ID" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{ID}" /> </layout> </parameter> <!--UserID --> <parameter> <parameterName value="@UserID" /> <dbType value="Int32" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{UserID}" /> </layout> </parameter> <!--PlatName --> <parameter> <parameterName value="@PlatName" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{PlatName}" /> </layout> </parameter> <!--Module --> <parameter> <parameterName value="@Module" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{Module}" /> </layout> </parameter> <!--Operation --> <parameter> <parameterName value="@Operation" /> <dbType value="String" /> <size value="200" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{Operation}" /> </layout> </parameter> <!--RequestIPs --> <parameter> <parameterName value="@RequestIPs" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{RequestIPs}" /> </layout> </parameter> <!--RequestUrl --> <parameter> <parameterName value="@RequestUrl" /> <dbType value="String" /> <size value="500" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{RequestUrl}" /> </layout> </parameter> <!--DeviceID --> <parameter> <parameterName value="@DeviceID" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{DeviceID}" /> </layout> </parameter> <!--HRVersion --> <parameter> <parameterName value="@HRVersion" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{HRVersion}" /> </layout> </parameter> <!--CustomerID --> <parameter> <parameterName value="@CustomerID" /> <dbType value="String" /> <size value="10" /> <layout type="HuRongClub.Log.CustomLayout"> <conversionPattern value="%property{CustomerID}" /> </layout> </parameter> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> <!--Txt记录形式--> <!--信息日志--> <appender name="InfoRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="logfile\Info\" /> <param name="AppendToFile" value="true" /> <param name="datePattern" value="yyyy-MM-dd.‘Info.LOG‘" /> <param name="staticLogFileName" value="false" /> <param name="rollingStyle" value="Composite" /> <param name="MaxSizeRollBackups" value="-1" /> <param name="MaximumFileSize" value="5000KB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date -[%thread] %-5level -%logger %message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <!--调试日志--> <appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="logfile\Debug\" /> <param name="AppendToFile" value="true" /> <param name="datePattern" value="yyyy-MM-dd.‘debug.LOG‘" /> <param name="staticLogFileName" value="false" /> <param name="rollingStyle" value="Composite" /> <param name="MaxSizeRollBackups" value="-1" /> <param name="MaximumFileSize" value="5000KB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date -[%thread] %-5level -%logger %message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="DEBUG" /> <param name="LevelMax" value="DEBUG" /> </filter> </appender> <!--警告日志--> <appender name="WarningRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="logfile\Warn\" /> <param name="AppendToFile" value="true" /> <param name="datePattern" value="yyyy-MM-dd.‘warn.log‘" /> <param name="staticLogFileName" value="false" /> <param name="rollingStyle" value="Composite" /> <param name="MaxSizeRollBackups" value="-1" /> <param name="MaximumFileSize" value="5000KB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n记录时间:%date %n日志级别:%-5level %n日 志 类:%logger %n日志描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="WARN" /> <param name="LevelMax" value="WARN" /> </filter> </appender> <!--错误日志--> <appender name="ErrorRollingFileAppender" type="log4net.Appender.RollingFileAppender"> <param name="File" value="logfile\Error\" /> <param name="AppendToFile" value="true" /> <param name="datePattern" value="yyyy-MM-dd.‘error.LOG‘" /> <param name="staticLogFileName" value="false" /> <param name="rollingStyle" value="Composite" /> <param name="MaxSizeRollBackups" value="-1" /> <param name="MaximumFileSize" value="5000KB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%n记录时间:%date %n日志级别:%-5level %n日 志 类:%logger %n日志描述:%message %newline%newline" /> </layout> <filter type="log4net.Filter.LevelRangeFilter"> <param name="LevelMin" value="Error" /> <param name="LevelMax" value="ERROR" /> </filter> </appender> <root> <level value="All" /> <!--数据库 形式记录日志--> <!--<appender-ref ref="ADONetAppenderMySqlClient" />--> <!--文件形式记录日志--> <appender-ref ref="ErrorRollingFileAppender" /> <appender-ref ref="DebugRollingFileAppender" /> <appender-ref ref="WarningRollingFileAppender" /> <appender-ref ref="InfoRollingFileAppender" /> </root> </log4net> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> </configuration>
原文地址:https://www.cnblogs.com/puzi0315/p/11318568.html
时间: 2024-10-10 16:07:24