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

How to turn off automatic Elmah logging?

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

ErrorSignal.FromCurrentContext.Raise(ex)

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

当我在 Web.config 中禁用 ErrorLog 模块时,但是,正如 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

是否可以关闭自动记录但保持手动打开?当我打开自动时,我得到两个条目,当我关闭它时,我得到 none.

事实证明,最好让 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