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

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

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

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