Scrigroup - Documente si articole


HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Default Listener Application


+ Font mai mare | - Font mai mic

Default Listener Application

The Trace class provides a Listeners collection that allows us to add a new listener application. When no new listener object is added to the collection, the Trace class uses the default listener application: the Output debug window. This window is provided by the Visual Studio .NET IDE during debugging. Let's see a simple example, TraceExample1:

static void Main()

The code is really simple; it writes tracing information when entering and exiting from the Main method, plus the variable's values in the loop. In the next screenshot, you can see how the Visual Studio .NET Output listener shows the information:

The Trace class also provides two useful methods to assert error notification: Assert() and Fail(). The former allows developer to check a condition provided as parameter and write a message into the listener when this condition is false. The latter writes a message into the listener each time a failure occurs. When no other listener is added to the collection, the Assert() method displays a message box to inform the user about an assertion failure. The following snippet of code, TraceAssert.cs, can be tested when the SQL Server service has been stopped deliberately in order to raise a connection exception:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;

namespace TraceAssert

private static void DBThread()



In the Main method, a new thread is created and started. The new thread runs the code within the DBThread() method. This code simply tries to contact the pubs SQL Server database, and retrieve all the data contained within the authors table. If the SQL Server service were not available, the following assertion failure would be displayed upon execution of the code:

The row that raises that assertion is:

Trace.Assert(dbConn.State == ConnectionState.Open,
'Error', 'Connection failed');

As you can see, the first parameter checks whether the state of the connection is Open. It will be set to false when the connection has not been opened, so the assertion will be displayed. As you will see later in the chapter, you can deactivate tracing messaging using the application configuration file. In that way, you can decide whether or not to display assert messages at run time.

Using Different Listener Applications

In this section, we will see how to change the default listener application. The Trace class (and the Debug class as you will see later) exposes the Listeners collection, which contains a collection of listener applications. Without adding any new listener class, the DefaultTraceListener will point to the Output debug window provided by Visual Studio .NET. However, the .NET Framework provides another two classes that can be used as listener applications:




Using this class, you will redirect tracing messages to the Windows Event Log


Using this class you will redirect tracing messages to a text file, or to a stream

In a multi-threaded application, you change the default listener with one of the listed listeners if you need to trace an application's behavior during its execution outside of Visual Studio .NET. Naturally, the Output debug window is available only during debug. Using these two classes, you could choose whether trace messages are placed in the Windows Event log, or inside a text file. Usually, when you know that your application will run in an operating system equipped with the Event Log, the EventLogTraceListener class is the best solution to choose. The reasons include:

The Event Log is managed by the operating system.

The Event Log allows administrators to specify security settings for the log.

The Event Log has to be read with the Event Viewer. This displays in a better visual environment than occurs with text file in Notepad.

Changing the default listener is simple, so let's see an example, TraceEventLog.cs:

static void Main(string[] args)

Firstly, we have to create a new listener object. In the example above, a new EventLogTraceListener has been created in order to use the Windows event log as listener application. The class constructor accepts a string where we can specify the name of the source that has written an entry. The constructor will instantiate a new EventLog object assigning the specified source name to the Source property of the EventLog class, automatically.

The next step is adding the new listener object to the Listeners collection using the Add() method and providing the reference to the listener object. Finally, we can start to write tracing messages that will be redirected to the listener application.

Opening up the Windows event log using the Event Viewer application, you should see the new entry appearing in the Application Log section:

You can double-click the item inside the Application Log report to examine the message:

The code that we have examined above adds a new listener to the Listeners collection so that you will receive tracing messages both in the Output debug window, and in the event log. If you want to remove the default listener in order to use just the event log application, you have to call the RemoveAt() method, as illustrated in the code below:

static void Main(string[] args)
// Create a trace listener for the event log.
EventLogTraceListener eltl = new EventLogTraceListener('TraceLog');
// Remove the default listener Trace.Listeners.RemoveAt(0);
// Add the event log trace listener to the collection. Trace.Listeners.Add(eltl);
// Write output to the event log. Trace.WriteLine('Entered in Main()');

Politica de confidentialitate | Termeni si conditii de utilizare



Vizualizari: 627
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved