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 = , 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 = , 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 = , 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 = , 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 = , 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 = , 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 = , 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 = ;
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(, exMessage.Length > ? : 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="" />
<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="" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter> <!--ID-->
<parameter>
<parameterName value="@ID" />
<dbType value="String" />
<size value="" />
<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="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{PlatName}" />
</layout>
</parameter>
<!--Module -->
<parameter>
<parameterName value="@Module" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{Module}" />
</layout>
</parameter>
<!--Operation -->
<parameter>
<parameterName value="@Operation" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{Operation}" />
</layout>
</parameter>
<!--RequestIPs -->
<parameter>
<parameterName value="@RequestIPs" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{RequestIPs}" />
</layout>
</parameter>
<!--RequestUrl -->
<parameter>
<parameterName value="@RequestUrl" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{RequestUrl}" />
</layout>
</parameter>
<!--DeviceID -->
<parameter>
<parameterName value="@DeviceID" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{DeviceID}" />
</layout>
</parameter>
<!--HRVersion -->
<parameter>
<parameterName value="@HRVersion" />
<dbType value="String" />
<size value="" />
<layout type="HuRongClub.Log.CustomLayout">
<conversionPattern value="%property{HRVersion}" />
</layout>
</parameter>
<!--CustomerID -->
<parameter>
<parameterName value="@CustomerID" />
<dbType value="String" />
<size value="" />
<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>

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

  1. 一点ASP.NET MVC Html.Helper类的方法

    一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...

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

    文章摘自:http://www.cnblogs.com/zhangziqiu/archive/2009/03/18/1415005.html MVC在view页面,经常需要用到很多封装好的HTML控件 ...

  3. ASP.NET Core MVC – Tag Helper 组件

    ASP.NET Core Tag Helpers系列目录,这是第五篇,共五篇: ASP.NET Core MVC – Tag Helpers 介绍 ASP.NET Core MVC – Caching ...

  4. Asp.net Core + Log4net + ELK 搭建日志中心

    原文:Asp.net Core + Log4net + ELK 搭建日志中心 Docker中一键安装ELK 对于这种工具类的东西,第一步就直接到docker的hub中查找了,很幸运,不仅有Elasti ...

  5. .NET中的加密算法总结(自定义加密Helper类续)

    1.1.1 摘要 相信许多人都使用过.NET提供的加密算法,而且在使用的过程我们必须了解每种加密算法的特点(对称或非对称,密钥长度和初始化向量等等).我也看到过很多人写过.NET中加密算法总结,但我发 ...

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

    在手机端输入网址不方全,通常会将网址做成一个二维码,然后用手机扫一下就可以打开预览.我们每改一下样式,就在手机上点一下刷新或电脑上按一下F5,这在最初的时候,也不觉得有什么问题,因为拿到我手上的静态页 ...

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

    最近使用task时候需要把异常记录日志,直接注入非单例模式的实例进入异步线程,在高并发情况下会出现一些问题. 所以需要把异常反馈给主线程 ,并且不在主线程里进行等待,研究相关资料后,自己写了一个简单的 ...

  8. Util和Helper类

    Util和Helper Util Util类,应该是一个无状态的类,只有静态方法. 比如在获取某些类的全局实例化对象的时候可以使用. public class ParamUtil { ... publ ...

  9. 基于log4net的帮助类Log

    using log4net; using System; using System.Collections.Generic; using System.Diagnostics; using Syste ...

随机推荐

  1. 关于苹果手机设置fiddler代理后无网络无法抓包的问题

    1.设置代理后,需要在苹果手机的关于本机中,打开证书信任 这样就可以抓包咯

  2. VC检测内存泄漏(Detected memory leaks!)

    Detected memory leaks!Dumping objects ->{98500} normal block at 0x05785AD0, 152 bytes long.Data: ...

  3. js里apply用法

    1.Function.apply,用于构造函数的继承,继承另外一个构建对象的属性与方法 function People(name,age){     this.name = name;     thi ...

  4. 2019 CSP-J复赛游记

    不出行?不出行考屁呢? 今天的CSP-J似乎比去年简单了一些,可它... 好了,来说一说我的情况. T1:太水,5分钟秒 T2:这个数据有点尴尬,双重循环铁定爆,用链表有有一点小题大做.本蒟蒻在考场上 ...

  5. 恕我直言,在座的各位根本不会写 Java!

    恕我直言,在座的各位根本不会写 Java! java思维导图 作者:Lrwin,软件架构师. 导语 自 2013 年毕业后,今年已经是我工作的第 4 个年头了,总在做 Java 相关的工作,终于有时间 ...

  6. 015_matlab运行C语言

    视频教程:https://v.qq.com/x/page/q3039wsuged.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...

  7. linux 下安装git的步骤方法

    ①.获取github最新的Git安装包下载链接,进入Linux服务器,执行下载,命令为: wget https://github.com/git/git/archive/v2.17.0.tar.gz  ...

  8. JS中的let变量和var变量的区别

    let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]]; let允许你声明一个作用域被限制在块级中的变量.语句或者表达式.在F ...

  9. web前端开发初级

    Web 页面制作基础 Web 的相关概念 WWWWebsiteURLWeb StandardWeb BrowserWeb Server HTML 基础 标记语言从 HTML 到 XHTMLHTML 的 ...

  10. 驱动中遍历模块,以及获取ntoskrnl.exe基址

    方法是基于PsLoadModuleList方式 驱动中遍历模块 一丶简介 简介: 进入内核了.遍历内核中使用的模块该怎么办. 其实在驱动中.我们的DriverEntry入口位置. 提供了两个参数. 一 ...