logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1169705 - in /logging/log4net/trunk/src: Appender/EventLogAppender.cs Util/SystemInfo.cs
Date Mon, 12 Sep 2011 11:58:00 GMT
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 <c>EventID</c> property (<see cref="LoggingEvent.Properties"/>)
 	/// on the <see cref="LoggingEvent"/>.
 	/// </para>
+    /// <para>
+    /// The <c>Category</c> of the event log entry can be
+	/// set using the <c>Category</c> property (<see cref="LoggingEvent.Properties"/>)
+	/// on the <see cref="LoggingEvent"/>.
+	/// </para>
 	/// <para>
 	/// There is a limit of 32K characters for an event log message
 	/// </para>
@@ -231,7 +236,24 @@ namespace log4net.Appender
             set { m_eventId = value; }
         }
 
-		#endregion // Public Instance Properties
+
+        /// <summary>
+        /// Gets or sets the <c>Category</c> to use unless one is explicitly
specified via the <c>LoggingEvent</c>'s properties.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// The <c>Category</c> of the event log entry will normally be
+	    /// set using the <c>Category</c> property (<see cref="LoggingEvent.Properties"/>)
+	    /// on the <see cref="LoggingEvent"/>.
+        /// This property provides the fallback value which defaults to 0.
+        /// </para>
+        /// </remarks>
+        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
         /// </summary>
         private int m_eventId = 0;
 
-		#endregion // Private Instance Fields
+        /// <summary>
+        /// The event category to use unless one is explicitly specified via the <c>LoggingEvent</c>'s
properties.
+        /// </summary>
+        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
 		}
 
-		/// <summary>
+        /// <summary>
+        /// Parse a string into an <see cref="Int16"/> value
+        /// </summary>
+        /// <param name="s">the string to parse</param>
+        /// <param name="val">out param where the parsed value is placed</param>
+        /// <returns><c>true</c> if the string was able to be parsed into
an integer</returns>
+        /// <remarks>
+        /// <para>
+        /// Attempts to parse the string into an integer. If the string cannot
+        /// be parsed then this method returns <c>false</c>. The method does
not throw an exception.
+        /// </para>
+        /// </remarks>
+        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
+        }
+
+        /// <summary>
 		/// Lookup an application setting
 		/// </summary>
 		/// <param name="key">the application settings key to lookup</param>



Mime
View raw message