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. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

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