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

发布于 2025-01-19 04:15:29 字数 1287 浏览 5 评论 0原文

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

ErrorSignal.FromCurrentContext.Raise(ex)

我的情况几乎与这是一个,唯一的区别是我使用XML记录而不是SQL。我还为我用于跟踪目的的例外生成了一个唯一的ID。

当我禁用errorlog web.config中的模块时,如 ,我也失去了手动记录例外的能力。那时什么都没有记录。

这是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

是否可以关闭自动记录,但请保留手册?当我自动打开时,我会收到两个条目,当我关闭它时,我一无所获。

I'd like to disable automatic exception logging in Elmah, while leaving manual logging enabled, e.g.

ErrorSignal.FromCurrentContext.Raise(ex)

My situation is nearly identical to this one, with the sole difference being that I'm using XML logging instead of SQL. I'm also generating a unique ID for the exception that I'm using for tracking purposes.

When I disable the ErrorLog module in Web.config, however, as suggested in the answer, I also lose the ability to manually log the exception. Nothing gets logged at that point.

Here's the code from 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(
quot;~/oops/{sCode}")
End Sub

...and here's LinkedException:

Imports System.Web

Public Class LinkedException
  Inherits HttpException

  Public Sub New(ExceptionCode As String, ex As HttpException)
    MyBase.New(ex.ErrorCode, 
quot;[{ExceptionCode}] {ex.Message}", ex)
  End Sub
End Class

Is it possible to turn off automatic logging but leave manual turned on? When I turn on automatic I get two entries, when I turn it off I get none.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2025-01-26 04:15:29

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

As it turns out, it's best to let ELMAH do the heavy lifting and not use Application_Error at all.

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 = 
quot;[{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(
quot;~/oops/{sCode}", True)
  End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文