Author: bodewig Date: Mon Sep 12 11:58:00 2011 New Revision: 1169705 URL: http://svn.apache.org/viewvc?rev=1169705&view=rev Log: Allow EventLog category to be specified via the LoggingEvent's properties or a configured value. Based on patch by Ron Grabowsky. LOG4NET-38 Modified: logging/log4net/trunk/src/Appender/EventLogAppender.cs logging/log4net/trunk/src/Util/SystemInfo.cs Modified: logging/log4net/trunk/src/Appender/EventLogAppender.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/EventLogAppender.cs?rev=1169705&r1=1169704&r2=1169705&view=diff ============================================================================== --- logging/log4net/trunk/src/Appender/EventLogAppender.cs (original) +++ logging/log4net/trunk/src/Appender/EventLogAppender.cs Mon Sep 12 11:58:00 2011 @@ -47,6 +47,11 @@ namespace log4net.Appender /// set using the EventID property () /// on the . /// + /// + /// The Category of the event log entry can be + /// set using the Category property () + /// on the . + /// /// /// There is a limit of 32K characters for an event log message /// @@ -231,7 +236,24 @@ namespace log4net.Appender set { m_eventId = value; } } - #endregion // Public Instance Properties + + /// + /// Gets or sets the Category to use unless one is explicitly specified via the LoggingEvent's properties. + /// + /// + /// + /// The Category of the event log entry will normally be + /// set using the Category property () + /// on the . + /// This property provides the fallback value which defaults to 0. + /// + /// + public short Category + { + get { return m_category; } + set { m_category = value; } + } + #endregion // Public Instance Properties #region Implementation of IOptionHandler @@ -355,7 +377,7 @@ namespace log4net.Appender // int eventID = m_eventId; - // Look for the EventLogEventID property + // Look for the EventID property object eventIDPropertyObj = loggingEvent.LookupProperty("EventID"); if (eventIDPropertyObj != null) { @@ -386,6 +408,38 @@ namespace log4net.Appender } } + short category = m_category; + // Look for the Category property + object categoryPropertyObj = loggingEvent.LookupProperty("Category"); + if (categoryPropertyObj != null) + { + if (categoryPropertyObj is short) + { + category = (short) categoryPropertyObj; + } + else + { + string categoryPropertyString = categoryPropertyObj as string; + if (categoryPropertyString == null) + { + categoryPropertyString = categoryPropertyObj.ToString(); + } + if (categoryPropertyString != null && categoryPropertyString.Length > 0) + { + // Read the string property into a number + short shortVal; + if (SystemInfo.TryParse(categoryPropertyString, out shortVal)) + { + category = shortVal; + } + else + { + ErrorHandler.Error("Unable to parse event category property [" + categoryPropertyString + "]."); + } + } + } + } + // Write to the event log try { @@ -401,7 +455,7 @@ namespace log4net.Appender using(SecurityContext.Impersonate(this)) { - EventLog.WriteEntry(m_applicationName, eventTxt, entryType, eventID); + EventLog.WriteEntry(m_applicationName, eventTxt, entryType, eventID, category); } } catch(Exception ex) @@ -500,7 +554,12 @@ namespace log4net.Appender /// private int m_eventId = 0; - #endregion // Private Instance Fields + /// + /// The event category to use unless one is explicitly specified via the LoggingEvent's properties. + /// + private short m_category = 0; + + #endregion // Private Instance Fields #region Level2EventLogEntryType LevelMapping Entry Modified: logging/log4net/trunk/src/Util/SystemInfo.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/SystemInfo.cs?rev=1169705&r1=1169704&r2=1169705&view=diff ============================================================================== --- logging/log4net/trunk/src/Util/SystemInfo.cs (original) +++ logging/log4net/trunk/src/Util/SystemInfo.cs Mon Sep 12 11:58:00 2011 @@ -835,7 +835,55 @@ namespace log4net.Util #endif } - /// + /// + /// Parse a string into an value + /// + /// the string to parse + /// out param where the parsed value is placed + /// true if the string was able to be parsed into an integer + /// + /// + /// Attempts to parse the string into an integer. If the string cannot + /// be parsed then this method returns false. The method does not throw an exception. + /// + /// + public static bool TryParse(string s, out short val) + { +#if NETCF + val = 0; + try + { + val = short.Parse(s, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture); + return true; + } + catch + { + } + + return false; +#else + // Initialise out param + val = 0; + + try + { + double doubleVal; + if (Double.TryParse(s, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out doubleVal)) + { + val = Convert.ToInt16(doubleVal); + return true; + } + } + catch + { + // Ignore exception, just return false + } + + return false; +#endif + } + + /// /// Lookup an application setting /// /// the application settings key to lookup