Handling Different Types Of Events

Generally , you log entries to the Windows 2000 Event Log when something significant happens in your web application. These “events” fall into two categories: expected events and unexpected events.

Expected Events

Expected events are things that are not completely out of the ordinary, but you want to make a record of the fact that they did happen. Generally , you determine whether an expected event happened by using a logic structure. Items can be logged to the proper event log (or not logged at all), depending on the outcome of the logic structure. Listings 8.5 and 8.6 provide examples.

Listing 8.5. Logging Expected Events with Logic Structures (C#)
<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Diagnostics"%> 

<script language="C#" runat="server"> 
      //build a generic function for logging, to prevent redundant code 
      void LogStuff(string message, EventLogEntryType eventType, 
            short eventID) 
      {
            EventLog el = new EventLog(); 
         el.MachineName = "."; //local computer 
         el.Source = "Test Source"; 
         el.Log = "Test Log"; 

         if (!EventLog.SourceExists(el.Source)) 
         {
               //Event source doesn't exist, so create a new one 
               EventLog.CreateEventSource(el.Source,el.Log); 
         } 

         el.WriteEntry(message,eventType,eventID); 
         el.Close(); 
     } 
</script> 

<% 
     //grab the "second" portion of the time 
     int second = DateTime.Now.Second; 

     //write to a different log, depending on the value 
     if(second % 2 == 0) 
     {
           LogStuff("The value is even: " + second. ToString(), 
                 EventLogEntryType.Information, 1); 
     } 
     else if(second == 7) 
     {
           LogStuff("Beware of superstitions: " + second. ToString(), 
                 EventLogEntryType.Warning, 2); 
     } 
     else 
     {
           LogStuff("The value is odd: " + second. ToString(), 
                 EventLogEntryType.Information, 3); 
     } 

     Response.Write("Done!"); 
%> 

Listing 8.6. Logging Expected Events with Logic Structures (Visual Basic .NET)
<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Diagnostics" %> 

<script language="VB" runat="server"> 
     'build a generic function for logging, to prevent redundant code 
     Sub LogStuff(message As String, eventType As EventLogEntryType, _ 
          eventID as Short) 

          Dim el As EventLog = New EventLog() 

          el.MachineName = "." "local computer 
          el.Source = "Test Source" 
          el.Log = "Test Log" 

          If Not EventLog.SourceExists(el.Source) Then 
               ‘Event source doesn't exist, so create a new one 
               EventLog.CreateEventSource(el.Source,el.Log) 
          End If 

          el.WriteEntry(message,eventType,eventID) 
          el.Close() 
     End Sub 
</script> 

<% 
     'grab the "second" portion of the time 
     Dim second As Integer = DateTime.Now.Second 

     'write to a different log, depending on the value 
     If second Mod 2 = 0 Then 
          LogStuff("The value is even: " & second. ToString(), _ 
               EventLogEntryType.Information, 1) 
     ElseIf second = 7 Then 
          LogStuff("Beware of superstitions: " & second. ToString(), _ 
               EventLogEntryType.Warning, 2) 
     Else 
          LogStuff("The value is odd: " & second. ToString(), _ 
               EventLogEntryType.Information, 3) 
     End If 

     Response.Write("Done!") 
%> 

The example starts by defining a LogStuff function. Because several different types of log entries can be made on the page, it makes sense to consolidate the logic into a utility function. In your own web applications, you might also want to encapsulate this logic into a lightweight utility component. For now, a utility function will suffice.

The example itself is not very complex. You retrieve the “second” portion of the current time and write an entry to the custom event log based on the value that you obtain. Notice that each call to LogStuff feeds in a different value for the event ID (the last parameter). You can use the sorting capabilities of the Windows 2000 Event Log Viewer to group similar events for analysis. Also, the EventLogEntryType parameter is set to Warning when the number 7 (often considered lucky by superstitious people) comes up.

Unexpected Events

Unexpected events happen when errors occur in your web application. Microsoft’s .NET Framework provides structured error handling to capture and handle these errors. Structured error handling also presents the perfect place to log these errors to the Windows 2000 Event Log. The examples provided in Listings 8.7 and 8.8 will help clarify.

Listing 8.7. Logging Unexpected Events with Structured Error Handling (C#)
<%@ Page Language="C#" %> 
<%@ Import Namespace="System.Diagnostics" %> 

<script language="C#" runat="server"> 
      //build a generic function for logging, to prevent redundant code 
      void LogStuff(string message, EventLogEntryType eventType, 
            short eventID) 
      {
            //code truncated - see example 8.7 
      } 
</script> 

<% 
      int value1 = 10; 
      int value2 = 0; 
      int value3 = 0; 

      try 
      {
            value3 = value1 / value2; 
      } 
      catch (DivideByZeroException e) 
      {
            //log the error to the event log 
            LogStuff(e.Message, EventLogEntryType.Error, 1); 
      } 

      Response.Write("Done!"); 
%> 

Listing 8.8. Logging Unexpected Events with Structured Error Handling (Visual Basic .NET)
<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Diagnostics" %> 

<script language="VB" runat="server"> 
     'build a generic function for logging, to prevent redundant code 
     Sub LogStuff(message As String, eventType As EventLogEntryType, _ 
          'code truncated - see example 8.8 
     End Sub 
</script> 

<% 
     Dim value1 As Integer = 10 
     Dim value2 As Integer = 0 
     Dim value3 As Integer = 0 

     Try 
          value3 = value1 / value2 
     Catch e As OverflowException 
           ‘log the error to the event log 
           LogStuff(e.Message, EventLogEntryType.Error, 1) 
     End Try 

     Response.Write("Done!") 
%> 

You start off by declaring three integer variables. Next, you open a structured error handling “try” block. You then intentionally manufacture a DivideByZeroException (OverflowException, in Visual Basic) by dividing value1 by value2 (which translates to 10 / 0). The “catch” block intercepts the exception because it was defined as the proper type. Had the error not been of DivideByZeroException (OverflowException, in Visual Basic) , a normal runtime error would have occurred on the page. To prevent this, you can declare another catch block underneath it for the generic Exception class. That way, specific exceptions will be caught, but if a strange exception occurs, then you’re still covered.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset