ASP.NET Futures (July 2007): Adding Diagnostics to an ASP.NET AJAX Web page
This section provides information about the following:
- Diagnostics. A new feature enables you to capture errors in client script
and automatically report them to a Web service in your application. You can write
a handler for the
DiagnosticsService.OnClientException event to record
the exception in a log or database. Diagnostics are enabled in Web pages by adding
an instance of the DiagnosticsControl class. Diagnostics capture can
be toggled by using the diagnostics entry in the Web.config file.
Enabling Diagnostics in an ASP.NET Web Page
To enable the diagnostics service for an ASP.NET Web page, add the DiagnosticsControl
control to the page. When diagnostics are enabled in the Web.config file, the
DiagnosticsControl control downloads the diagnostics scripts and begins
capturing script errors.
The following example shows the markup for the DiagnosticsControl class
in an ASP.NET Web page
<asp:ScriptManager id="scriptManager" runat="server">
</asp:ScriptManager>
<asp:Diagnostics id="Diagnostics1" runat="server" />
ASP.NET
Creating a Diagnostics Web Service
In this release, you add a diagnostics Web service by creating a Web service named
DiagnosticsService.asmx to your Web application. Set the Web service's
class to Microsoft.Web.Preview.Diagnostics.DiagnosticsService,Microsoft.Web.Preview The
following example shows the declaration for the Web service. The Web service does not need to contain
any code; it just serves as an endpoint for the client script that passes error information.
Note A built-in diagnostics Web service might be added in a future
release.
<%@ WebService Language="C#" Class="Microsoft.Web.Preview.Diagnostics.DiagnosticsService,Microsoft.Web.Preview" %>
DiagnosticsService.asmx
Handling Client Errors in Server Code
To use server code to capture (and log) an error that is passed from client script,
create a handler in the Global.asax file for the DiagnosticsService.OnClientException
event. Error information is passed in the ClientExceptionEventArgs
parameter that is passed to the event handler, as shown in the following example.
void Application_Start(object sender, EventArgs e)
{
DiagnosticsService.OnClientException += new ClientExceptionHandler(DiagnosticsService_OnClientException);
}
void DiagnosticsService_OnClientException(object o, ClientExceptionEventArgs e)
{
//Log to health monitoring or application database.
}
global.asax
Enabling Diagnostics in the Web Application
To enable diagnostics, set enabled="true" in the diagnostics section
of the Web.config file. The following example shows the parts of a Web.config file that are required.
<configSections>
<sectionGroup name="microsoft.web.preview" type="Microsoft.Web.Preview.Configuration.PreviewSectionGroup, Microsoft.Web.Preview">
<section name="diagnostics" type="Microsoft.Web.Preview.Configuration.DiagnosticsSection, Microsoft.Web.Preview"/>
</sectionGroup>
</configSections>
<microsoft.web.preview>
...
<diagnostics enabled="true"/>
...
</microsoft.web.preview>
web.config
Diagnostics Service Example
In the following example you can see all the files that are needed to implement
client-script diagnostics. The server code that handles the error shows how you might
log error information in a database. The example also includes an ASP.NET Web page that
shows errors that have been logged in the database.
Diagnostics example
|