C# ELMAH日志处理异常

C# ELMAH日志处理异常,c#,asp.net-mvc,error-handling,elmah,C#,Asp.net Mvc,Error Handling,Elmah,我使用ELMAH尝试记录处理过的异常(发生在try捕获中的异常)。但是,我无法让ELMAH记录try-catch中发生的任何异常 以下是我的行动: [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model) { try { throw new Exception("Log me elmah");

我使用ELMAH尝试记录处理过的异常(发生在try捕获中的异常)。但是,我无法让ELMAH记录try-catch中发生的任何异常

以下是我的行动:

    [ValidateAntiForgeryToken]       
    public async Task<ActionResult> Login(LoginViewModel model) {

        try {
            throw new Exception("Log me elmah");
        }
        catch (Exception e) {
            ModelState.AddModelError("", "Something unexpected happened, please try again.");
            return View(model);
        }
    }
这是我的
global.asax

  public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);        
        }      
    }
以下是我的寄存器全局筛选器方法:

 public class FilterConfig {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
            filters.Add(new ElmahExceptionLogger());
            filters.Add(new HandleErrorAttribute());



            /*Append no cache to all actions in all controllers so we don't cache any pages, I dont particularly like it because it means an increased server load
            However there are reasons to this; The first being data gets updated regularly and we want users to have the most up-to-date data
            And also you can press back after logging out to get to the cached page before. It can be overridden per action if needed */
            filters.Add(new OutputCacheAttribute {
                VaryByParam = "*",
                Duration = 0,
                NoStore = true
            });
        }
    }

有人知道如何让ELMAH在try Catch中记录我的异常吗?

当然,只有未处理的异常才会触发它。这些过滤器只针对允许冒泡的错误(即未处理的错误)运行。如果要记录已处理的异常,则需要将该
ErrorSignal.FromCurrentContext().Raise()
逻辑放在catch块中

catch (Exception e)
{
    ModelState.AddModelError("", "Something unexpected happened, please try again.");
    ErrorSignal.FromCurrentContext().Raise(e);
    return View(model);
}
如果你发现自己经常这样做,那么我建议你停止使用Elmah。Elmah不是一个通用的日志框架,它适合于未处理的错误。最好使用诸如或之类的日志记录系统,然后将这些日志记录到一个专门的系统,例如。

虽然不是完全“自动”的,但我采取了一种方法,至少可以使Try-Catch块中的日志记录更容易

我首先创建了Exception类的扩展:

    Imports Elmah
    Imports System
    Imports System.Web
    Imports System.Runtime.CompilerServices


Public Module ElmahExtension

    <Extension()>
    Public Sub LogToElmah(ex As Exception)


        If HttpContext.Current Is Nothing Then

            ErrorLog.GetDefault(Nothing).Log(New [Error](ex))
            Dim req = New HttpRequest(String.Empty, "https://YOURWEBSITE", Nothing)
            Dim res = New HttpResponse(Nothing)
        Else
            ErrorSignal.FromCurrentContext().Raise(ex)
            ErrorLog.GetDefault(HttpContext.Current).Log(New [Error](ex))
        End If
    End Sub
End Module
这将把异常对象传递给ELMAH并记录错误


所以不是完全自动的,但更容易。特别是,如果您使用诸如ReSharper之类的工具来创建包含“ex.LogToELMAH”的代码快捷方式。

我理解这一点,但我想知道是否有一种自动方式来记录未处理的exception@Andrew你把这两个搞混了。这不是一个未处理的异常,因为您不允许它冒泡。你已经处理好了。对不起,是打字错误。我的意思是,有没有一种方法可以自动记录已处理的异常。但我想不会,谢谢你的帮助response@Andrew不,不会的,因为你抓住了例外。如果您正在捕获它,那么为它运行的唯一代码将在catch块中,除非您选择重新捕获它。“而且,重述你已经处理过的事情并不是一个好主意。”安德鲁我在问题的末尾添加了一段关于更好地记录这类事情的方法。
    Imports Elmah
    Imports System
    Imports System.Web
    Imports System.Runtime.CompilerServices


Public Module ElmahExtension

    <Extension()>
    Public Sub LogToElmah(ex As Exception)


        If HttpContext.Current Is Nothing Then

            ErrorLog.GetDefault(Nothing).Log(New [Error](ex))
            Dim req = New HttpRequest(String.Empty, "https://YOURWEBSITE", Nothing)
            Dim res = New HttpResponse(Nothing)
        Else
            ErrorSignal.FromCurrentContext().Raise(ex)
            ErrorLog.GetDefault(HttpContext.Current).Log(New [Error](ex))
        End If
    End Sub
End Module
   Try

      YOUR CODE HERE

  Catch
      ex.LogToElmah()
  End Try