【问题标题】:How could I still log with asp.net healthMonitoring an Exception that was Catch?我如何仍然使用 asp.net healthMonitoring 记录 Catch 异常?
【发布时间】:2011-11-09 05:02:04
【问题描述】:

我在我的 Web 应用程序中配置了 HealthMonitoring 以记录数据库中的所有异常。

但是,当我通过向用户显示一条好消息来优雅地处理一些异常时,它们并没有登录到数据库中。如果我像Throw ex 之类的东西,则会显示标准错误页面并且错误没有得到妥善处理。

我可以在错误处理程序的末尾添加其他内容来告诉系统也记录此错误吗?

这里是代码示例:

Dim errorText As String = "Doing Something"
Try
   'Do Something

Catch ex As Exception
        DataActionError.Text = String.Format("There was a problem {0}. ", errorText)
        DataActionError.Visible = True
        'add something here?
End Try

以及我的 web.config 的 healthMonitoring 部分:

<healthMonitoring enabled="true">
  <eventMappings>
    <clear />
    <add name="All Errors" 
         type="System.Web.Management.WebBaseErrorEvent"
         startEventCode="0" 
         endEventCode="2147483647" />
  </eventMappings>

  <providers>
    <clear />
    <add connectionStringName="MyConnectionString"
         maxEventDetailsLength="1073741823"
         buffer="false" 
         name="SqlWebEventProvider"
         type="System.Web.Management.SqlWebEventProvider" />
  </providers>

  <rules>
    <clear />
    <add name="All Errors To Database" 
         eventName="All Errors" 
         provider="SqlWebEventProvider"
         profile="Default" 
         minInstances="1" maxLimit="Infinite" 
         minInterval="00:00:00" />
  </rules>
</healthMonitoring>

【问题讨论】:

    标签: .net asp.net vb.net exception exception-handling


    【解决方案1】:

    您可以提出自己的事件,然后由提供者使用。
    您不能自己提出WebBaseErrorEvent,但您可以创建自定义网络事件并调整您的配置以捕获它。 见
    http://msdn.microsoft.com/en-us/library/ms998306
    http://msdn.microsoft.com/en-us/library/ms227980.aspx

    【讨论】:

    • 谢谢,第一个链接很有帮助。
    【解决方案2】:

    这里是我用来创建和引发自定义网络事件的代码。

    更新的代码示例:

    Dim errorText As String = "Doing Something"
    Try
       'Do Something
    
    Catch ex As Exception
        Dim ErrorDescr As String = String.Format("There was a problem {0}", errorText)
        DataActionError.Text = ErrorDescr 
        DataActionError.Visible = True
    
        Dim ErrorEvent As CustomWebEvents.HandeledExceptionWebAuditEvent = _
                New CustomWebEvents.HandeledExceptionWebAuditEvent(ErrorDescr , Ex)
        ErrorEvent.Raise()
    
    End Try
    

    HandeledExceptionWebAuditEvent 类代码(注意,I had to put this in a separated assembly to make this work。):

    using System;
    using System.Web.Management;
    
    namespace CustomWebEvents
    {
        public class HandeledExceptionWebAuditEvent : WebAuditEvent
        {
            private Exception _ex;
    
            public HandeledExceptionWebAuditEvent(string msg, Exception ex)
                : base(msg, ex.Source, WebEventCodes.WebExtendedBase + 1)
            {
                _ex = ex;
            }
    
             public override void FormatCustomEventDetails(WebEventFormatter formatter )
             {
                base.FormatCustomEventDetails(formatter);
                formatter.AppendLine("Activity Description: Handeled Exception");
                formatter.AppendLine("Message:" + base.Message);
                formatter.AppendLine("Exception Message:" + _ex.Message);
                formatter.AppendLine("Exception StackTrace:" + _ex.StackTrace);
             }
    
        } 
    
    }
    

    更新了我的 web.config 的 healthMonitoring 部分:

    <healthMonitoring enabled="true">
      <eventMappings>
        <clear />
        <add name="All Errors" 
             type="System.Web.Management.WebBaseErrorEvent"
             startEventCode="0" 
             endEventCode="2147483647" />
        <add name="Handeled Exception"
             type="CustomWebEvents.HandeledExceptionWebAuditEvent, CustomWebEvents"
         />
      </eventMappings>
    
      <providers>
        <clear />
        <add connectionStringName="MyConnectionString"
             maxEventDetailsLength="1073741823"
             buffer="false" 
             name="SqlWebEventProvider"
             type="System.Web.Management.SqlWebEventProvider" />
      </providers>
    
      <rules>
        <clear />
        <add name="All Errors To Database" 
             eventName="All Errors" 
             provider="SqlWebEventProvider"
             profile="Default" 
             minInstances="1" maxLimit="Infinite" 
             minInterval="00:00:00" />
    
        <add name="Handeled Exception To Database" 
             eventName="Handeled Exception" 
             provider="SqlWebEventProvider"
             profile="Default" 
             minInstances="1" maxLimit="Infinite" 
             minInterval="00:00:00" />
    
      </rules>
    </healthMonitoring>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-25
      • 2014-08-23
      • 1970-01-01
      • 2011-01-07
      • 2014-09-09
      • 1970-01-01
      相关资源
      最近更新 更多