Home > Articles > Microsoft > Other Microsoft

  • Print
  • + Share This
This chapter is from the book

Managing Unhandled Exceptions

In Chapter 4, "Components and .NET Assemblies," we discuss the AppDomain class in detail. This class allows for response to events such as when an assembly is loaded or unloaded and when an unhandled exception occurs. When an unhandled exception occurs, it raises an UnhandledException event within the containing AppDomain. This event passes information back to your code in an UnhandledExceptionEventArgs object. This gives you one last chance to work with an exception, even if no Catch block exists to trap the error.

Table 3.2 details the more important properties of the UnhandledExceptionEventArgs class.

Table 3.2 Important Properties of the UnhandledExceptionEventArgs Class

Property

Description

ExceptionObject

Receives the unhandled exception object corresponding to the current domain

IsTerminating

A Boolean value that indicates whether the CLR is terminating


An event handler may be attached to the UnhandledException event to take custom actions such as writing the exception-related information to a log file. A log maintained over a period of time may help you to analyze and identify patterns providing useful debugging information. Several methods may be used to log event-related information, including the following:

  • Windows Event Log

  • Custom log files

  • Databases such as SQL Server 2000

  • Email notifications

The Windows Event Log provides the most dependable method of logging, because it's a core part of the operating system with few dependencies. If you log to a file, an email, or a database, your logging might fail due to loss of network connectivity or file access.

To log information to the Event Log, you can use the EventLog class. The EventLog class allows your code to write to the Windows Event Logs (Application, System, and Security) available in Windows 2000 and later versions of the operating system.

CAUTION

The option to use the EventLog class is not available in older versions of the Windows operating system, such as Windows 98, and therefore should not be used if the application will be deployed on legacy platforms.

Table 3.3 details some of the more important members of the EventLog class.

Table 3.3 Important Members of the EventLog Class

Member

Type

Description

Clear

Method

Removes all entries from the Event Log and leaves it empty

CreateEventSource

Method

Creates an event source that you can use to write to a standard or custom Event Log

Entries

Property

Gets the contents of the Event Log

Log

Property

Specifies the name of the log to read from or write to

MachineName

Property

Specifies the name of the computer on which to read or write events

Source

Property

Specifies the event source name to register and use when writing to the Event Log

SourceExists

Method

Specifies whether the event source exists on a computer

WriteEntry

Method

Writes an entry in the Event Log


  • + Share This
  • 🔖 Save To Your Account