Event Logging and Distributed Logging in ASP.NET (3/6)
[previous] [next] |
Event Logging in .NET
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;
}
}
[previous] [next] |
Created: January 16, 2003
Revised: January 16, 2003
URL: https://webreference.com/programming/asp/logging/3.html