MVC添加记录日志(log4net)
一、在项目中添加--->管理NUGET程序包--->浏览-->搜索log4net-->安装log4net
二、安装成功后会在项目中显示App_Data文件夹
添加Config文件夹和log4net.config
这里log4net.config文件内容已写好,无需变动
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<!--日志记录组建配置-->
<log4net>
<!--定义输出到文件中-->
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Logs\\Tracer\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value="100"/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="DEBUG" />
<levelMax value="INFO" />
</filter>
</appender>
<!--定义输出到文件中-->
<appender name="RollingFileTracer" type="log4net.Appender.RollingFileAppender">
<!--定义文件存放位置-->
<file value="Logs\\Error\\"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>
<staticLogFileName value="false"/>
<param name="MaxSizeRollBackups" value="100"/>
<layout type="log4net.Layout.PatternLayout">
<!--每条日志末尾的文字说明-->
<!--输出格式-->
<!--样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] - info-->
<conversionPattern value="%n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] - %n错误描述:%message%newline %n"/>
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
</appender>
<root>
<level value="ALL"/>
<!--文件形式记录日志-->
<appender-ref ref="RollingLogFileAppender"/>
<appender-ref ref="RollingFileTracer"/>
</root>
</log4net>
</configuration>
三、然后在项目中添加Filters文件夹和ExceptionAttribute.cs,具体代码如下:
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1.Filters
{
/// <summary>
/// 异常处理过滤器,使用log4net记录日志,并跳转至错误页面
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class ExceptionAttribute : HandleErrorAttribute
{
ILog log = LogManager.GetLogger(typeof(ExceptionAttribute));
public override void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled)
{
string message = string.Format("消息类型:{0}\r\n消息内容:{1}\r\n引发异常的方法:{2}\r\n引发异常源:{3}"
, filterContext.Exception.GetType().Name
, filterContext.Exception.Message
, filterContext.Exception.TargetSite
, filterContext.Exception.Source + filterContext.Exception.StackTrace
);
//记录日志
log.Error(message);
//转向
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult("/Common/Error");
}
base.OnException(filterContext);
}
}
}
在以下文件下添加
[assembly: log4net.Config.XmlConfigurator(ConfigFile = @"Config\log4net.config", Watch = true)]
如下图:
在此文件夹的类文件下添加引用
using WebApplication1.Filters;
using System.Web;
using System.Web.Mvc;
namespace WebApplication1
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new ExceptionAttribute());//要添加的代码
}
}
}
这样就完成了!!!!!
注意添加using引用,不然会报错。
可以在Logs>Error文件下查看日志信息。