Welcome to ClipClip!
Already a Member? Sign In
 

CodeGuru: .NET Tip: Logging Exceptions to the Event Log#more#more#more

source: http://www.codeguru.com/csharp/.net/net_general/systeminformation/article.php...

clipped by sfathyking Dec 18, 2006

.net error logging

  • Assuming you have Remote Desktop or some other access to a server, the system event log is a good place to store information about your application. I personally prefer to put some information into the database, but errors such as the inability to make a database connection obviously would preclude you from storing an error in the database.



    (continued)




    Tutorial: UML 2.0 Modeling: Visualize with Rational Software Modeler

    Using easy-to-follow, step-by-step instructions, you'll learn how to create a variety of different UML 2.0 diagrams, import existing modeling projects, and edit models.


    On-Demand Webcast: Introduction to UML2

    The Unified Modeling Language (UML) is the standard language for visualizing, specifying, constructing, and documenting the artifacts of a software-intensive system. In this session learn the basic UML diagrams, and the notation changes in UML 2.0, the latest version. February 15, 2005


    Download: Rational Software Modeler

    A customizable, UML-based visual modeling and design tool that enables users to clearly document and communicate these system views. Its drag and drop UI components and point and click database connectivity leverages your existing skills, shortens the Java learning curve, and supports team development.


    Article: Synchronizing UML Models

    See how the Eclipse-based IBM Rational Systems Developer allows you to easily create and browse models and code using diagrams, create code from UML diagrams, and vice versa.


    Download: Rational Systems Developer

    IBM Rational Systems Developer is a design and development tool that enables software architects and model-driven developers to create well-architected C/C++, Java J2SE, and CORBA-based applications that leverage Unified Modeling Language (UML 2).


    Download: IBM Rational Software Architect V6.0

    This integrated design and development tool leverages model-driven development with the UML for creating well-architected applications and services.

    The code to write entries to the event log is fairly straightforward and can easily be added to a base page class or other utility class for ease of use. To configure how the event log writer works, you also can use the configuration file and either a custom configuration section or the standard AppSettings area.

    The following code creates three public functions and a private one to do the real work. You easily can read the code to determine which type of entry is being created:

    public void LogInformation(string message)
    {
       LogEntry(message, EventLogEntryType.Information);
    }
    
    public void LogWarning(string message)
    {
       LogEntry(message, EventLogEntryType.Warning);
    }
    
    public void LogError(string message)
    {
       LogEntry(message, EventLogEntryType.Error);
    }
    
    private void LogEntry(string message, EventLogEntryType logType)
    {
       if (!EventLog.SourceExists("NCS.Web"))
          EventLog.CreateEventSource("NCS.Web", "Application");
    
       EventLog ncsLog = new EventLog();
       ncsLog.Source = "NCS.Web";
       ncsLog.WriteEntry(message, logType);
    
    }
    

    Each call to LogEntry passes a EventLogEntryType constant, which can be one of the three shown or SuccessAudit or FailureAudit. Inside the LogEntry routine, the code first determines whether an event source called NCS.Web (the internal name for the application) has been created. If it hasn't, it creates the event source with a name and location where the log should be stored—whether it should go to the Application, Security, or System log, or a custom log you can create. In this case, the log entries are placed into the Application log, and then an EventLog object is created, the source is set, and the message is written out. You also can specify an event ID and/or a category for the log entry. This makes it easier to sort through the logs when you are diagnosing a problem.

    To make this routine more useful, you also could write the events to a database table or to a flat file because it can be somewhat difficult to view the logs from a Web site.

    About the Author

 

Comments

User4

samaritree 1067 days later

I finally found a working stream posted at http://leafpen.com/54376 I had to fill out some dumb survey but it worked!! ! !

 

Please sign in to comment.