Recipe 13.7. Writing Trace Data to the Event Log with Controllable Levels


Problem

You want your application to output trace information to the event log and, at the same time, control what level of information is output.

Solution

Modify web.config to:

  1. Add the EventLogTraceListener listener to the Listeners collection and make it available to your application, as shown in Example 13-18.

  2. Add the TraceSwitch, as shown in Example 13-18.

In the classes you want to output trace information, create a TRaceSwitch object using the name of the traceSwitch you added to the web.config file, and use the WriteIf and WriteLineIf methods of the TRace class to output the required messages, as we demonstrate in our sample application shown in Examples 13-19, 13-20 through 13-21.

Discussion

The technique we advocate for writing trace information to the event log involves adding the EventLogTraceListener to the listener collection in web.config to write the trace information to the event log. We find it useful to control the level of messages output to the event log, such as outputting only error messages or outputting error and warning messages. Controlling the level of messages that are output involves the use of switches (more about this in a minute).

As discussed in Recipe 13.5, you can add additional listeners to the traceListeners collection via the web.config file. When a TRace.Write or trace.WriteLine is executed, all listeners in the TRaceListeners collection receive and process their output.

The support that the .NET Framework provides for TRaceListeners is more powerful when coupled with switchesw. Switches provide the ability to control when trace information is sent to the traceListeners configured for your application.

Two switch types are provided in the .NET Framework: BooleanSwitch and TRaceSwitch. The BooleanSwitch class supports two states (on and off) that turn the trace output on and off. The traceSwitch class supports five levels (off, error, warning, info, and verbose) to provide the ability to output messages only for the configured levels.

You must first add the switch and listener information to your web.config file, as shown in Example 13-18. The switch data includes the name of the switch and the value for the switch. The switch name is the name used in your code to access the switch configuration. The value defines the message level to output, as shown in Table 13-1.

Table 13-1. Switch level values

Value

Meaning

0

Output no messages

1

Output only error messages

2

Output error and warning messages

3

Output error, warning, and informational messages

4

Output all messages


To output trace messages that use the switch information, you need to create a traceSwitch object passing the name of the switch and a general description of the switch. After creating the traceSwitch, you use it with the WriteIf and WriteLineIf methods of the trace class to output your messages. The first parameter of either method defines the level for which the message should be output. In other words, if you only want the message to be output when the switch is configured for "warnings," set the first parameter to the traceWarning property of the switch you created. The second parameter should be set to the message you want to output.

We are not outputting the trace information to the web form, as we have in other examples in this chapter, so it is unnecessary to add the TRace="true" statement to the @ Page directive in the .aspx page or to turn on application-level tracing in the web.config file.


The name used in the constructor of the TRaceSwitch must match the name of the switch in the web.config file. If you fail to use the exact name defined in the web.config file, you can wind up spending a great deal of time trying to determine why your messages are not being output as expected.


In a web application, referencing the TRace class without further qualifying the namespace will actually reference the System.Web.Trace class, which does not support the WriteIf and WriteLineIf methods. To access the TRace class in the System.Diagnostics namespace that provides the WriteIf and WriteLineIf methods, fully qualify the reference:

 System.Diagnostics.Trace.WriteIf(level,  Message) 


See Also

Recipe 13.5

Example 13-18. web.config settings for adding the trace listener and trace switch

 <configuration> … <system.diagnostics> <switches> <!-- This switch controls messages written to the event log. To control the level of message written to the log set the value attribute as follows: "0" - output no messages "1" - output only error messages "2" - output error and warning messages "3" - output error, warning, and informational messages "4" - output all messages --> <add name="EventLogSwitch" value="1"/> </switches> <trace autoflush="true" indentsize="0"> <listeners> <add name="EventLogTraceListener" type="System.Diagnostics.EventLogTraceListener,  System,  Version=2.0.0.0,  Culture=neutral,  PublicKeyToken=b77a5c561934e089" initializeData="Application" /> </listeners> </trace> </system.diagnostics> </configuration> 

Example 13-19. Writing trace information as a function of trace level (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" AutoEventWireup="false" CodeFile="CH13TestTracingWithLevelControlVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH13TestTracingWithLevelControlVB" Title="Test Tracing With Level Control" %> <asp:Content  runat="server" ContentPlaceHolder> <div align="center" > Tracing With Output To Event Log with Trace Levels (VB) </div> </asp:Content> 

Example 13-20. Writing trace information as a function of trace level (.vb)

 Option Explicit On Option Strict On Imports System Imports System.Diagnostics Namespace ASPNetCookbook.VBExamples ''' <summary> ''' This class provides the code-behind for ''' CH13TestTracingWithLevelControlVB.aspx ''' </summary> Partial Class CH13TestTracingWithLevelControlVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the page load event. It ''' is responsible for initializing the controls on the page. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Private Sub Page_Load(ByVal sender As Object, _   ByVal e As System.EventArgs) Handles Me.Load Dim generalTraceSwitch As TraceSwitch 'create the trace switch generalTraceSwitch = New TraceSwitch("EventLogSwitch", _  "Used throughout the application") 'write trace data if error level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceError, _  "This is an error message") 'write trace data if warning level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceWarning, _  "This is an warning message") 'write trace data if info level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceInfo, _  "This is an info message") 'write trace data if verbose level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceVerbose, _  "This is an verbose message") End Sub 'Page_Load End Class 'CH13TestTracingWithLevelControlVB End Namespace 

Example 13-21. Writing trace information as a function of trace level (.cs)

 using System; using System.Diagnostics; namespace ASPNetCookbook.CSExamples { /// <summary> /// This class provides the code-behind for /// CH13TestTracingWithLevelControlCS.aspx /// </summary> public partial class CH13TestTracingWithLevelControlCS : System.Web.UI.Page { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the page load event. /// It is responsible for initializing the controls on the page. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void Page_Load(object sender, EventArgs e) { TraceSwitch generalTraceSwitch = null; // create the trace switch generalTraceSwitch = new TraceSwitch("EventLogSwitch",  "Used throughout the application"); // write trace data if error level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceError, "This is an error message"); // write trace data if warning level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceWarning, "This is an warning message"); // write trace data if info level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceInfo, "This is an info message"); // write trace data if verbose level is enabled System.Diagnostics.Trace.WriteIf(generalTraceSwitch.TraceVerbose, "This is an verbose message"); } // Page_Load } // CH13TestTracingWithLevelControlCS } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net