简体   繁体   English

如何在 ASP.NET MVC 中使用 NLog 记录未处理的异常

[英]How log an unhandled exception with NLog in ASP.NET MVC

In my ASP.NET MVC project I have an action with [LogErrors] attribute as below:在我的 ASP.NET MVC 项目中,我有一个带有 [LogErrors] 属性的操作,如下所示:

[LogErrors]
public ActionResult Index()
 {
    var i = 0;
    var c = 10 / i;
    return View();
 }

I made an aunhandled exception without trycatch(devide 10 by 0) in this action and I must log this exception error text and else log in which action this exception happened in a text file with NLog.在此操作中,我在没有 trycatch(除 10 乘 0)的情况下创建了一个未处理的异常,我必须记录此异常错误文本,否则在带有 NLog 的文本文件中记录此异常发生的操作。 I made the [LogErrors] as below:我制作了[LogErrors]如下:

 public class LogErrorsAttribute : FilterAttribute, IExceptionFilter
{
    void IExceptionFilter.OnException(ExceptionContext filterContext)
    {
        if (filterContext != null && filterContext.Exception != null)
        {
            string controller = filterContext.RouteData.Values["controller"].ToString();
            string action = filterContext.RouteData.Values["action"].ToString();
            string loggerName = string.Format("{0}Controller.{1}", controller, action);

            NLog.LogManager.GetLogger(loggerName).Error(string.Empty, filterContext.Exception);
        }
    }
}

and my NLog.config is as below:我的 NLog.config 如下:

 <?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" >
  <targets>
    <target name="file" xsi:type="File" fileName="C:\Logs\${shortdate}.log"
       layout="
--------------------- ${level} (${longdate}) ----------------------${newline}
IP: ${aspnet-request-ip}${newline}
Call Site: ${callsite}${newline}
${level} message: ${message}${newline}
Id: ${activityid}${newline}
aspnet-sessionid: ${aspnet-sessionid}${newline}
aspnet-request-method: ${aspnet-request-method}${newline}
aspnet-request-host: ${aspnet-request-host}${newline}
aspnet-request-form: ${aspnet-request-form}${newline}
aspnet-request-cookie: ${aspnet-request-cookie}${newline}
aspnet-request: ${aspnet-request:serverVariable=HTTP_URL}${aspnet-request:queryString}${newline}
aspnet-mvc-controller: ${aspnet-mvc-controller}${newline}
aspnet-mvc-action: ${aspnet-mvc-action}${newline}
aspnet-appbasepath: ${aspnet-appbasepath}${newline}
              " encoding="UTF8"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
  </rules>
</nlog>

How fix my configs to log this exception error text and else log that in which action this exception happened in a text file?如何修复我的配置以记录此异常错误文本,并在文本文件中记录此异常发生在哪个操作中? Any help will be appriciated!任何帮助将不胜感激!

You need to send a message to .Error and send the Exception as first parameter.您需要向.Error发送消息并将Exception作为第一个参数发送。

Otherwise it roughly translated to string.Format("", filterContext.Exception) .否则,它大致翻译为string.Format("", filterContext.Exception)

So something like this:所以是这样的:

NLog.LogManager.GetLogger(loggerName)
               .Error(filterContext.Exception, "Unhandled exception in controller");

If that isn't working, then please check the NLog troubleshooting guide如果这不起作用,请查看NLog 故障排除指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM