Click or drag to resize

ErrorLogger Class

Represents a logger that can be used for logging handled as well as unhandled exceptions across multiple application types (Console Application, Windows Service, Web Application, Web Service).
Inheritance Hierarchy
SystemObject
  SystemMarshalByRefObject
    System.ComponentModelComponent
      GSF.ErrorManagementErrorLogger
        GSF.Windows.ErrorManagementErrorLogger

Namespace: GSF.ErrorManagement
Assembly: GSF.Core (in GSF.Core.dll) Version: 2.4.207-beta+1781b796b2aa7a54013a031eb432fe4ccee31867
Syntax
View Source

The ErrorLogger type exposes the following members.

Constructors
 NameDescription
Public methodErrorLogger Initializes a new instance of the ErrorLogger class.
Public methodErrorLogger(IContainer) Initializes a new instance of the ErrorLogger class.
Top
Properties
 NameDescription
Public propertyCode exampleActionTextMethod Gets or sets the Delegate that provides text stating the action(s) that can be taken by the end-user after an Exception is encountered.
Public propertyApplicationName Gets the name of the currently executing application.
Public propertyApplicationType Gets the type of the currently executing application.
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event.
(Inherited from Component)
Public propertyContactEmail Gets or sets the e-mail address where e-mail messages containing Exception information are to be sent when the LogToEmail property is set to true.
Public propertyContactName Gets or sets the name of the person who can be contacted by the end-user in case of an Exception.
Public propertyContactPhone Gets or sets the phone number that can be used by the end-user to communicate about an encountered Exception.
Public propertyContainerGets the IContainer that contains the Component.
(Inherited from Component)
Public propertyDatabaseLogSize Gets or sets the maximum exception log size to maintain when logging exceptions to the database.
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode.
(Inherited from Component)
Public propertyEnabled Gets or sets a boolean value that indicates whether the ErrorLogger object is currently enabled.
Public propertyErrorLog Get the LogFile object used for logging Exception information to a file.
Public propertyCode exampleErrorTextMethod Gets or sets the Delegate that provides common text stating what could have possibly caused the Exception.
Protected propertyEventsGets the list of event handlers that are attached to this Component.
(Inherited from Component)
Public propertyExitOnUnhandledException Gets or sets a boolean value that indicates whether the application will terminate after logging an unhandled Exception.
Public propertyHandleUnhandledException Gets or sets a boolean value that indicates whether the ErrorLogger object must register itself to handle Exception that are not trapped inside of a try-catch block.
Public propertyIsDisposed Gets a flag that indicates whether the object has been disposed.
Public propertyLastException Get the last encountered Exception.
Public propertyLoggers Gets a list of methods registered for logging information about an encountered Exception.
Public propertyLogToDatabase Gets or sets a boolean value that indicates whether Exception information is to be written to the database.
Public propertyLogToEmail Gets or sets a boolean value that indicates whether Exception information is to be sent in an e-mail to the ContactEmail address.
Public propertyLogToEventLog Gets or sets a boolean value that indicates whether Exception information is to be written to the EventLog.
Public propertyLogToFile Gets or sets a boolean value that indicates whether Exception information is to be written to the ErrorLog.
Public propertyLogToUI Gets or sets a boolean value that indicates whether Exception information is to be displayed on the application UI.
Public propertyLogUserInfo Gets or sets a boolean value that indicates whether information about local user (for windows applications) or remote user (for web applications) is to be logged when logging an Exception.
Public propertyMoreInfoTextMethod Gets or sets the Delegate that provides text containing detailed information about the encountered Exception.
Public propertyName Gets the unique identifier of the ErrorLogger object.
Public propertyPersistSettings Gets or sets a boolean value that indicates whether the settings of ErrorLogger object are to be saved to the config file.
Public propertyCode exampleScopeTextMethod Gets or sets the Delegate that provides text stating what is going to happen as a result of the Exception.
Public propertySettingsCategory Gets or sets the category under which the settings of ErrorLogger object are to be saved to the config file if the PersistSettings property is set to true.
Public propertySiteGets or sets the ISite of the Component.
(Inherited from Component)
Public propertySmtpServer Gets or sets the SMTP server to be used for sending e-mail messages containing Exception information to the ContactEmail address.
Public propertyStatus Gets the descriptive status of the ErrorLogger object.
Public propertySuppressInteractiveLogging Gets or sets flag that controls if Loggers requiring interaction either directly or indirectly when logging handled Exception using the Log(Exception) method are enabled.
Top
Methods
 NameDescription
Public methodBeginInit Performs necessary operations before the ErrorLogger object properties are initialized.
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject)
Public methodDisposeReleases all resources used by the Component.
(Inherited from Component)
Protected methodDispose(Boolean) Releases the unmanaged resources used by the ErrorLogger object and optionally releases the managed resources.
(Overrides ComponentDispose(Boolean))
Public methodEndInit Performs necessary operations after the ErrorLogger object properties are initialized.
Public methodEqualsDetermines whether the specified object is equal to the current object.
(Inherited from Object)
Protected methodExceptionToDatabase Logs encountered Exception to the database.
Protected methodExceptionToEmail Logs encountered Exception to an e-mail message.
Protected methodExceptionToEventLog Logs encountered Exception to the EventLog.
Protected methodExceptionToFile Logs encountered Exception to the ErrorLog.
Protected methodExceptionToUI Logs encountered Exception to the application UI.
Protected methodExceptionToWebPage Shows Exception information in a Web Site.
Protected methodExceptionToWindowsCui Shows Exception information in a Console Application.
Protected methodExceptionToWindowsGui Shows Exception information in a Windows Application.
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.
(Inherited from Component)
Protected methodGetEmailAttachments Gets or sets the comma-separated or semicolon-separated list of file names to be attached to the Mail message.
Public methodStatic memberGetExceptionInfo Gets information about an Exception complete with system and application information.
Protected methodGetExtendedMoreInfoText Allows other loggers to extend "more info text".
Public methodGetHashCodeServes as the default hash function.
(Inherited from Object)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Protected methodGetMoreInfoText Default Delegate for MoreInfoTextMethod.
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container.
(Inherited from Component)
Public methodGetTypeGets the Type of the current instance.
(Inherited from Object)
Public methodInitialize Initializes the ErrorLogger object.
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject)
Public methodLoadSettings Loads saved settings for the ErrorLogger object from the config file if the PersistSettings property is set to true.
Public methodLog(Exception) Logs information about the encountered Exception.
Public methodLog(Exception, Boolean) Logs information about the encountered Exception.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object.
(Inherited from Object)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject)
Protected methodOnLoggingException Raises the LoggingException event.
Protected methodRegister Registers the ErrorLogger object to handle unhandled Exception if the HandleUnhandledException property is set to true.
Public methodSaveSettings Saves settings for the ErrorLogger object to the config file if the PersistSettings property is set to true.
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden.
(Inherited from Component)
Protected methodUnregister Unregister the ErrorLogger object from handling unhandled Exception.
Top
Events
 NameDescription
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method.
(Inherited from Component)
Public eventLoggingException Occurs when an Exception is encountered while logging an Exception.
Top
Fields
 NameDescription
Public fieldStatic memberDefaultContactEmail Specifies the default value for the ContactEmail property.
Public fieldStatic memberDefaultContactName Specifies the default value for the ContactName property.
Public fieldStatic memberDefaultContactPhone Specifies the default value for the ContactPhone property.
Public fieldStatic memberDefaultExitOnUnhandledException Specifies the default value for the ExitOnUnhandledException property.
Public fieldStatic memberDefaultHandleUnhandledException Specifies the default value for the HandleUnhandledException property.
Public fieldStatic memberDefaultLogToDatabase Specifies the default value for the LogToDatabase property.
Public fieldStatic memberDefaultLogToEmail Specifies the default value for the LogToEmail property.
Public fieldStatic memberDefaultLogToEventLog Specifies the default value for the LogToEventLog property.
Public fieldStatic memberDefaultLogToFile Specifies the default value for the LogToFile property.
Public fieldStatic memberDefaultLogToUI Specifies the default value for the LogToUI property.
Public fieldStatic memberDefaultLogUserInfo Specifies the default value for the LogUserInfo property.
Public fieldStatic memberDefaultPersistSettings Specifies the default value for the PersistSettings property.
Public fieldStatic memberDefaultSettingsCategory Specifies the default value for the SettingsCategory property.
Public fieldStatic memberDefaultSmtpServer Specifies the default value for the SmtpServer property.
Top
Extension Methods
 NameDescription
Public Extension MethodGetEnumValueOrDefault Gets the enumeration constant for value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Public Extension MethodGetEnumValueOrDefaultT Gets the enumeration constant for this value, if defined in the enumeration, or a default value.
(Defined by EnumExtensions)
Top
Remarks

Adapted from exception handling code by Jeff Atwood of CodingHorror.com. Demo projects for handling unhandled exception in both windows and web environment by Jeff Atwood are available at The Code Project web site. See: http://www.codeproject.com/script/articles/list_articles.asp?userid=450027

Error logger with Windows Forms capabilities can be found in the GSF.Windows assembly.

Example
This example shows how to use the ErrorLogger component to log handled and unhandled exceptions:
C#
using System;
using System.IO;
using GSF.ErrorManagement;

class Program
{
    static ErrorLogger s_logger;

    static Program()
    {
        s_logger = new ErrorLogger();
        s_logger.LogToUI = true;                    // Show exception info on the UI.
        s_logger.LogToFile = true;                  // Log exception info to a file.
        s_logger.LogToEmail = true;                 // Send exception info in an e-mail.
        s_logger.LogToEventLog = true;              // Log exception info to the event log.
        s_logger.ContactEmail = "dev@email.com";    // Provide an e-mail address.
        s_logger.HandleUnhandledException = true;   // Configure to handle unhandled exceptions.
        s_logger.PersistSettings = true;            // Save settings to the config file.
        s_logger.Initialize();                      // Initialize ErrorLogger component for use.
    }

    static void Main(string[] args)
    {
        try
        {
            // This may cause a handled FileNotFoundException if the file doesn't exist.
            string data = File.ReadAllText(@"c:\NonExistentFile.txt");
        }
        catch (Exception ex)
        {
            // When logging handled exceptions we want to disable loggers (UI logger and E-mail logger) that
            // may require interaction either directly or indirectly as it can be annoying. All the loggers
            // are enabled automatically after the handled exception has been logged.
            s_logger.SuppressInteractiveLogging();
            s_logger.Log(ex);
        }

        int numerator = 1;
        int denominator = 0;
        int result = numerator / denominator;   // This will cause an unhandled DivideByZeroException.

        Console.ReadLine();
    }
}
See Also