.net mvc 如何使用 log4net 日志

         对与开发者来说,异常日记是比较重要的。他能精确定位,帮助我们准确攻破bugLog4net是一个著名的开源日志组件。利用log4net可以方便的将日志信息记录到文件中,并且我们还可以记载控制要记载的日志级别,可以记载的日志类别包括:FATAL(致命错误)、ERROR(一般错误)、WARN(警告)、INFO(一般信息)、DEBUG(调试信息)。要想获取最新版本的log4net组件库,可以到官方网站http://logging.apache.org/log4net/下载,或者用 NuGet extension manager 来添加log4net 到站点中:

PM> install-package Log4Net

1.在Web.config配置相关的配置文件

   

<configSections>     
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4Net"/>
  </configSections>
<!--日志的配置开始-->
    <log4net>
      <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="D:\Temp\log4net.log" />
        <appendToFile value="true" />
        <maximumFileSize value="500KB" />
        <maxSizeRollBackups value="2" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %level %logger - %message%newline" />
        </layout>
      </appender>
      <root>
        <level value="All" />
        <appender-ref ref="RollingFile" />
      </root>
    </log4net>
  <!--日志的配置结束-->

 2.在程序一开始的时候,我们需要在Global.asax.cs文件中的Application_Start事件中进行一下初始化配置。

   

protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //读取日志  如果使用log4net,应用程序一开始的时候,都要进行初始化配置
            log4net.Config.XmlConfigurator.Configure();
        }

 3我们可以写一个类,定义一个静态方法,这个方法将可以将我们的错误消息进行输出。因为不仅仅一个地方需要,所以我们把它定义为了静态方法。

class LogHelper
    {       
        public static void WriteLog(string txt)
        {
            ILog log = LogManager.GetLogger("log4netlogger");
            log.Error(txt);
        }
    }

 4.自定义一个类,使这个类继承自HandleErrorAttribute这个类,并重写其内部方法。这个方法内部代码的作用就是处理异常,将异常信息记录日志。

public class MyExceptionFileAttribute:HandleErrorAttribute
    {       
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
            Common.LogHelper.WriteLog(filterContext.Exception.ToString());           
        }
}

 5.由于全局都需要进行异常的处理,我们需要在FilterConfig.cs RegisterGlobalFilters方法中注册我们自己的异常处理过滤器。

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new JsonExceptionFilterAttribute());
            //filters.Add(new HttpResponseExceptionAttribute());
            //注册异常处理过滤器。
            filters.Add(new MyExceptionFileAttribute());
        }

 例图:



 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值