spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / asp / logging To page 1To page 2current pageTo page 4To page 5To page 6
[previous] [next]

Event Logging in .NET

C/C++ Developer (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Eclipse Helios Update Brings New PHP Tools
Internet Explorer 9 Ups Standards Support
JBoss Portal 5 Release Easier to Use


The Event and EventSource classes come first. These are fairly simple classes with some readonly variables and a constructor. These can be modified to contain any other information about events as required. Notice that the Event class does not have a message string. Instead it has an eventid and an array of inserts. This is to make this event logger similar to Win32 event logging as described earlier. Also, both these classes are marked Serializable since they need to be passed as arguments to a remote method call.

   [Serializable]
   public class EventSource 
   {
      public readonly int      ApplicationId;   
      public readonly string   MachineName;      
      public readonly int      ProcessId;   

      public EventSource(int appId) 
      {
         ApplicationId = appId;
         try 
         { 
            MachineName = System.Environment.MachineName;   
            ProcessId = System.Diagnostics.Process.GetCurrentProcess().Id; 
         } 
         catch { }
         if(MachineName == null)
         {
            MachineName = "";
         }
         else
         {
            MachineName = MachineName.Trim();
         }
      }
   }   

   [Serializable]
   public class Event 
   {
      public readonly int      EventId;
      public readonly long   When;            
      public readonly string[]   Inserts;

      public Event( int eventId, string[] inserts) 
      {
         EventId = eventId;
         When = System.DateTime.Now.Ticks;            
         Inserts = inserts;
      }

      public override string ToString()
      {         
         return EventId + ":" + new DateTime(When).ToString() + ":" + 
                Inserts.ToString();
      }
   }

Once EventSource and Event is defined the EventCollection class almost writes itself. It has an array of Event and the EventSource that it represents passed to it in the constructor. It also has helper properties to check the state of the collection and a method to add events to this collection. It should be noticed that this class is not thread safe and synchronization is the responsibility of the caller.

   public class EventCollection : IEnumerable
   {
      public const int SIZE = 10;

      protected readonly EventSource _eventSource = null;
      protected readonly Event[] _arrEvent = null;
      protected int   _eventCount = 0;

      public EventCollection(EventSource source) 
      {
         _eventSource = source;
         _arrEvent = new Event[SIZE];
         _eventCount = 0;
      }

      public EventSource EventSource 
      {
         get 
         { 
            return _eventSource; 
         } 
      }

      IEnumerator IEnumerable.GetEnumerator() 
      {
         return _arrEvent.GetEnumerator();
      }


      public bool IsFull 
      {
         get 
         { 
            return _eventCount >= _arrEvent.Length; 
         }
      }

      /// <summary>
      /// This method is not thread safe. Thread safety is 
      /// the responsibility of the caller.
      /// </summary>
      /// <param name="Event"></param>   
      internal void addEvent(Event event) 
      {
         if (this.IsFull) 
         {
            throw new Exception("Event Set full");
         }
         _arrEvent[_eventCount] = event;
         _eventCount++;
      }

      internal void Clear()
      {
         _eventCount = 0;
      }
   }

home / programming / asp / logging To page 1To page 2current pageTo page 4To page 5To page 6
[previous] [next]

webref The latest from WebReference.com Browse >
Flashmaps' DynamicLocator: Interactive Maps for Small Areas · Flashmaps' AreaSelector: Interactive Maps for Wide Areas · The DB Mapper: Interactive Street-level Maps of U.S. and Canada
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags

Created: January 16, 2003
Revised: January 16, 2003

URL: http://webreference.com/programming/asp/logging/3.html