static void

ASP 2 Errors with health monitoring

Logging Errors

Remember to remove any custom logging from Global.asax (All Error Default is only *unhandled* errors). This is BOTH email (define the System.Net section) and SQL logging (redefining the SqlWebEventProvider - if you are not using the default local SQLExpress instance).

<healthMonitoring enabled="true" >
  <
providers>
    <
add name="MailEventProvider" type="System.Web.Management.SimpleMailWebEventProvider"
        from="[email protected]" to="[email protected]"
        
bodyHeader="An error occurred"
        bodyFooter="Health Monitoring Provider"
        subjectPrefix="Error recorded."
        maxEventLength="4096" maxMessagesPerNotification="1" buffer="false" />
    <
remove name="SqlWebEventProvider"/>
    <
add name="SqlWebEventProvider"
      type="System.Web.Management.SqlWebEventProvider"
      connectionStringName=""
      maxEventDetailsLength="1073741823"
      buffer="false" />
  </
providers>
  <
rules>
    <!--
All Errors Default is defined in
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Web.config
-->
    <
remove name="All Errors Default"/>
    <
add name="All Errors to SQL" eventName="All Errors" provider="SqlWebEventProvider" profile="Default" minInterval="00:00:30" />
    <
add name="All Errors To Email" eventName="All Errors" provider="MailEventProvider" />
  </
rules>
</
healthMonitoring>

Viewing Errors

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="EventId">
    <Columns>
        <asp:BoundField DataField="EventTimeUtc" DataFormatString="{0:d}" HeaderText="EventTimeUtc"
            HtmlEncode="False" SortExpression="EventTimeUtc" />
        <asp:BoundField DataField="ExceptionType" HeaderText="ExceptionType" SortExpression="ExceptionType" />
        <asp:CommandField ButtonType="Button" ShowSelectButton="True" />
    </Columns>
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource2" >
    <Fields>
        <asp:TemplateField HeaderText="Details" SortExpression="Details" ShowHeader="false">
            <ItemTemplate>
                <pre style="white-space: pre-wrap;white-space: -moz-pre-wrap;word-wrap: break-word; "><asp:Literal ID="Label1" runat="server" Text='<%# Eval("Details") %>'/></pre>
            </ItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings: MyConnectionString %>"
    SelectCommand="SELECT Details FROM aspnet_WebEvent_Events WHERE (EventId = @EventId)">
    <SelectParameters>
     <asp:ControlParameter ControlID="GridView1" Name="EventId" PropertyName="SelectedValue" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
    SelectCommand="SELECT TOP 100 EventId, EventTimeUtc, ExceptionType, Details FROM aspnet_WebEvent_Events WHERE (EventCode = '3005') ORDER BY EventTimeUtc DESC">
</asp:SqlDataSource>