logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zimney, Christopher M." <christopher.zim...@wamu.net>
Subject RE: EventID and EventLogAppender
Date Thu, 21 Jul 2005 21:09:01 GMT
Hehe... You can't do this out of the box, at least I couldn't figure out
how after many hours.

Ended up extending the EventLogAppender...
------------------------------------------

public class XmlEventLogAppender : EventLogAppender
	{
		#region Properties.

		private const string EVENT_ID_KEY	= "EventID";

		private const int DEFAULT_EVENT_ID	= 0;

		#endregion

		#region Methods.

		/// <summary>
		/// Initializes a new instance of the <see
cref="XmlEventLogAppender"/> 
		/// class.
		/// </summary>
		public XmlEventLogAppender ()
		{
		}

		/// <summary>
		/// Helper method that checks for the EventID within a
LoggingEvent's 
		/// Properties collection.
		/// </summary>
		/// <param name="loggingEvent">The LoggingEvent to
examine.</param>
		/// <returns>An integer representing the
EventID.</returns>
		private int GetEventID (LoggingEvent loggingEvent)
		{
			int eventID = DEFAULT_EVENT_ID;

			// Look for the EventID property that may have
been applied 
			// for a specific logging event by the
application. 
			object eventIDPropertyObj =
loggingEvent.Properties[EVENT_ID_KEY];
			if(eventIDPropertyObj != null)
			{
				if (eventIDPropertyObj is int)
				{
					eventID =
(int)eventIDPropertyObj;
				}
				else if (eventIDPropertyObj is string)
				{
					// Read the string property into
a number
					try
					{
						eventID =
int.Parse((string)eventIDPropertyObj, 
	
CultureInfo.InvariantCulture);
					}
					catch(Exception ex)
					{
	
EmitToOutput.AddMessage("Unable to parse event ID property [" + 
	
eventIDPropertyObj + "].  " + ex.Message, true);
					}
				}
			}

			return eventID;
		}


		#endregion

		#region Override implementation of EventLogAppender

		/// <summary>
		/// This method is called by the
AppenderSkeleton.DoAppend method of 
		/// the log4net framework.
		/// </summary>
		/// <remarks>
		/// There is a limit of 10K characters for an event log
message in the 
		/// WaMu Online environment.
		/// </remarks>
		/// <param name="loggingEvent">The event to log.</param>
		protected override void Append (LoggingEvent
loggingEvent)
		{
			int eventID = DEFAULT_EVENT_ID;

			try
			{
				string eventTxt =
RenderLoggingEvent(loggingEvent);

				eventID = GetEventID(loggingEvent);

				// There is a limit of 10K characters
for an event log message.
				if (eventTxt.Length > 10000)
				{
					eventTxt = eventTxt.Substring(0,
10000);
				}

				EventLog.WriteEntry(ApplicationName,
eventTxt, 
	
GetEntryType(loggingEvent.Level), eventID);
			}
			catch(Exception ex)
			{
				EmitToOutput.AddMessage("Unable to write
to event log [" + LogName + 
							"] using source
[" + ApplicationName + "].  " + 
							ex.Message,
true);
			}
		}

		
		#endregion
	} 

-----Original Message-----
From: Jeremiah Voris [mailto:jeremiahv@gmail.com] 
Sent: Thursday, July 21, 2005 9:06 AM
To: Log4NET User
Subject: EventID and EventLogAppender

I've tried just about everything I can think of and I just can't seem to
figure out how to get EventID set to anything other than zero when using
EventLogAppender. Is there a different form of Log.Error() I'm supposed
to be using that will let me set this? Or am I missing some way to set a
parameter prior to that call? I've been banging my head against the wall
trying to figure this one out. Any help to point me in the correct
direction would be greatly appreciated.

--
Jeremiah

Mime
View raw message