Event Logging and Distributed Logging in ASP.NET (4/6) | WebReference

Event Logging and Distributed Logging in ASP.NET (4/6)

To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

Event Logging in .NET

Once EventCollection has been implemented it is time to implement the LocalEventLog that will use the EventCollection class to buffer the local events. This is also a fairly simple class as shown below. This class is implemented as a singleton with a property to access the singleton instance. It contains a hashtable of collections of events. The individual event collections are created as required. Almost all the work gets done in the LogEvent method. Here we check first to see if we need to create a new EventCollection. If we do, then an instance of EventCollection is created and added to the hashtable. Then the event is added to the collection. Of course, we check to see whether the buffer is full before adding to the set. If it is full the remote method is called to flush the set. The class also has an initialization method for setting up the remoting infrastructure.

   public class LocalEventLog
   {
      private static LocalEventLog _instance = new LocalEventLog();
      public static LocalEventLog Instance
      {
         get
         {
            return _instance;
         }
      }
      private LocalEventLog()
      {
      }
      private static string _GlobalLogUrl = "";
      private static GlobalEventLog _GlobalLog = null;
      private Hashtable _events = new Hashtable();
      public static bool Init(string globalLogUrl)
      {
         _GlobalLogUrl = globalLogUrl;
         TcpChannel chan = new TcpChannel();
         ChannelServices.RegisterChannel(chan);
         _GlobalLog = GlobalEventLog.Activator.GetObject(
                      typeof(DEvents.GlobalEventLog),
                      _GlobalLogUrl);
         return true;
      }
/// <summary>
/// Possible enhancements: 
/// (a) The flushing can be done by a separate thread. Then this 
/// thread just creates another empty set and carries on. The 
/// flushing thread is handed over the full set which flushes 
/// the events and destroys the set. 
/// (b) Instead of flushing to just one global log the flushing 
/// could be done to many log listeners.
/// </summary>
/// <param name="s"></param>
/// <param name="e"></param>
      public void LogEvent(EventSource s, Event e)
      {
         // get the event set for this source
         // if none exist then create one
         if(!_events.ContainsKey(s))
         {
            EventCollection eSet = new EventCollection(s);
            _events.Add(s,eSet);
         }
         EventCollection currentSet = (EventCollection) _events[s];
         // add the event to the set
         // if the set is full flush this set to the 
         // global sink
         lock(currentSet)
         {
            if(currentSet.IsFull)
            {
               // Flush the events
               foreach(Event ee in currentSet)
               {
                  _GlobalLog.WriteLog(s, ee);
               }
               currentSet.Clear();
            }
            // add the event
            currentSet.addEvent(e);
         }
      }
   }

Now all that remains for our library is to implement the GlobalEventLog class. As is evident from the code above all we need is a method called WriteLog. This method can do pretty much whatever it wants. It can write the logs to a database and/or write to the Windows event log. For our present purpose of demonstration we just write to the console.

   public class GlobalEventLog : MarshalByRefObject 
   {
      public void WriteLog(EventSource s, Event e)
      {
         System.Console.WriteLine("Source: " + s.MachineName + ":" 
                                             + e.ToString());
      }
   }

To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

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

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