asp.net 的log4net的helper类

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

asp.net 的log4net的helper类的相关文章

Asp.net 使用log4net日志功能

Asp.net使用log4net日志功能 1. 在bin文件夹下面引入 log4net.dll.你只需要引入log4net.dll 既可,其他dll是我抠图的时候扣多了 2 . Global.asax 中加入 void Application_Start(object sender,EventArgs e) { // 在应用程序启动时运行的代码 log4net.Config.DOMConfigurator.Configure(); } 3.修改配置文件(web.config) log日志既可以写

MVC中使用HTML Helper类扩展HTML控件

文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件,这篇文章主要讲述,怎么自己扩展和定制自己需要的控件. ---------------------------------------------------------- HTML Helper类是ASP.NET MVC框架中提供的生成HTML控件代码的类. 本质上与第一种方式一样, 只是我们可以

asp.net利用log4net写入日志到SqlServer数据库

asp.net利用log4net写入日志到SqlServer数据库 作者: Kein  来源: 博客园  发布时间: 2010-10-14 07:19  阅读: 6427 次  推荐: 6   原文链接   [收藏] 摘要:Log4net是一个开源的错误日志记录项目,asp.net利用log4net写入日志到SqlServer数据库.下面就我的安装部署log4net到MS sql server的经验与大家分享. asp.net利用log4net写入日志到SqlServer数据库,Log4net是

ASP.NET使用log4Net日志组件教程(按日期与按日志大小切割)

对于一个大型网站与系统来说,日志是必备的工具,通过日志你可以非常清楚程序的运行情况,及时得到反馈来解决问题,下面介绍ASP.NET版本的log4Net日志组件是个非常强大的工具,最新版本for .net2.0 1.2.10.0 本人搞了一天,终于知道搞清楚怎样使用了,简单记录一下. 以下介绍的方法是log4net使用单独的配置文件的. 开始行动: 第一步:在项目引用log4net.dll文件 第二步: 在Web.config文件中加入一句代码,位置如下: <configuration>   

之前采用的是Helper类的方法重构时改用了扩展方法

在手机端输入网址不方全,通常会将网址做成一个二维码,然后用手机扫一下就可以打开预览.我们每改一下样式,就在手机上点一下刷新或电脑上按一下F5,这在最初的时候,也不觉得有什么问题,因为拿到我手上的静态页,通常由切片的同事做好了兼容性测试,需要一边刷新浏览器,一边改样式的机会不多.随着我们尝试用Less,stylus,这样的css工具,一方面,需要用到gulp这样的工具在后台自动监听我们的样式改动,另一方面,手动刷新的时候,gulp的脚本未必转换完了.这时候迫切需要浏览器自动刷新. 到此为止如果还没

Java逆向基础之Byteman的扩展Helper类

上一篇文章提到我们可以在规则文件中调用自己写的扩展Helper类 看一个例子 新建目标项目和扩展Helper类项目,结构如下 其中目标项目使用上一篇博客的第二个例子的代码 扩展Helper类项目需要用到第三方jar,在C:\byteman-download-4.0.2\lib找到byteman.jar复制过来 TraceHelper.java代码如下 package com.vvvtimes; import org.jboss.byteman.rule.Rule; import org.jbos

枚举类型取值helper类

枚举类型取值: 1.根据枚举下标拿取值(默认为 0,1,2,3,4) 2.根据枚举描述拿到枚举值 3.根据枚举下标拿到枚举值(不限下标,如101开头) 调用方法 调用1:Enum.GetNames(typeof(AddAttributeType))[0];   //注:这个适用于枚举下标为默认0开始的,拿到的值为string类型的枚举值(如果下标超出会报异常) 调用 2:EnumMethodHelper.EnumToDescription("101", typeof(AddAttrib

Task的在主线程处理异常信息的Helper类

最近使用task时候需要把异常记录日志,直接注入非单例模式的实例进入异步线程,在高并发情况下会出现一些问题. 所以需要把异常反馈给主线程 ,并且不在主线程里进行等待,研究相关资料后,自己写了一个简单的helper类封装了下. 1 using System; 2 using System.Threading; 3 using System.Threading.Tasks; 4 5 namespace Xyfy.Helper 6 { 7 /// <summary> 8 /// 9 /// <

Helper 类在Java和C++中的设计

Java 有三个选择 1)采用普通的类 有公有构造函数,没有成员变量,提供了很多成员函数作为方法,调用代码如下: Helper helper = new Helper(); helper.f1(); 这种方法的缺点是创建对象的开销是不必要的,我们知道创建对象意味着,首先要分配内存,然后在该内存上创建对象.在一个大量创建helper对象的场景中,这种负担是很大的. 2)为了避免这种反复创建对象的开销,可以采用Singleton延迟创建技术,确保整个进程中只有一个对象,并且只有第一次调用的时候才会被