CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
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:
The
code is really simple; it writes tracing information when entering and exiting from
the
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;In
the
The row that raises that assertion is:
Trace.Assert(dbConn.State == ConnectionState.Open,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.
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:
Class |
Description |
EventLogTraceListener |
Using this class, you will redirect tracing messages to the Windows Event Log |
TextWriterTraceListener |
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 voidFirstly, 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
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 682
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved