代码之家  ›  专栏  ›  技术社区  ›  InteXX

如何关闭Elmah自动日志记录?

  •  0
  • InteXX  · 技术社区  · 2 年前

    我想在Elmah中禁用自动异常日志记录,同时启用手动日志记录,例如。

    ErrorSignal.FromCurrentContext.Raise(ex)
    

    我的情况与 this one ,唯一的区别是我使用的是XML日志记录,而不是SQL。我还为用于跟踪目的的异常生成了一个唯一的ID。

    当我禁用 ErrorLog 模块在 Web.config ,但是,如中所建议的 the answer ,我也失去了手动记录异常的能力。此时没有任何记录。

    这是来自的代码 Global.asax :

    Sub Application_Error(Sender As Object, e As EventArgs)
      Dim sCode As String
    
      sCode = Utils.NewId(IdModes.AlphaNumeric)
    
      Try
        Throw New LinkedException(sCode, Me.Server.GetLastError)
    
      Catch ex As LinkedException
        ErrorSignal.FromCurrentContext.Raise(ex)
    
      Finally
        Me.Server.ClearError()
    
      End Try
    
      Me.Context.Redirect($"~/oops/{sCode}")
    End Sub
    

    …这是 LinkedException :

    Imports System.Web
    
    Public Class LinkedException
      Inherits HttpException
    
      Public Sub New(ExceptionCode As String, ex As HttpException)
        MyBase.New(ex.ErrorCode, $"[{ExceptionCode}] {ex.Message}", ex)
      End Sub
    End Class
    

    是否可以关闭自动日志记录,但保持手动打开?当我打开自动时,我会得到两个条目,当我关闭它时,我不会得到任何条目。

    0 回复  |  直到 2 年前
        1
  •  0
  •   InteXX    2 年前

    事实证明,最好让ELMAH来完成繁重的工作,而不要使用 Application_Error 完全

    Imports Elmah
    
    Public Class Global_asax
      Inherits HttpApplication
    
      Private Sub ErrorLog_Filtering(Sender As Object, e As ExceptionFilterEventArgs)
        Dim oContext As HttpContext
        Dim oError As [Error]
        Dim sCode As String
    
        oContext = TryCast(e.Context, HttpContext)
        sCode = Utils.NewId(IdModes.AlphaNumeric)
    
        If oContext.IsNotNothing Then
          ' Create a customized error containing the error code
          oError = New [Error](e.Exception, oContext) With {.User = $"[{sCode}] { .User}"}
    
          ' Log the customized error
          ErrorLog.GetDefault(oContext).Log(oError)
    
          ' Dismiss the incoming exception so
          ' we don't get a duplicate entry
          e.Dismiss()
        End If
    
        Me.Response.Redirect($"~/oops/{sCode}", True)
      End Sub
    End Class