13.5. Uncovering Problems Within Dual-Use Components

Problem

Because you intend to use a business component in web and non-web applications, you want to enable tracing within the component without having to reference its current HTTP context.

Solution

Modify web.config, as shown in Example 13-12, to add the WebPageTraceListener listener to the Listeners collection and make it available to your application.

In your non-web-specific components, add plain-vanilla Trace.Write statements to output any desired information to the trace log, as shown in our sample component in Examples 13-13 (VB) and 13-14 (C#).

A web form and the associated VB and C# code-behind we’ve written to test the tracing in our non-web-specific component are shown in Examples 13-15, 13-16 through 13-17.

Discussion

The .NET Framework uses the concept of trace listeners in its handling of trace messages. By default, the TraceListeners collection contains a single listener (DefaultTraceListener) when you enable tracing. Additional listeners can be added via the web.config file or programmatically. When a Trace.Write is executed, all listeners in the TraceListeners collection receive and process the message. This mechanism allows you to add trace statements to your components without the need to add a reference to the System.Web assembly.

ASP.NET 1.x does not provide any functionality to write trace information within your application’s business and data tiers and display it in the page trace data. If you need this functionality ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.