logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1169681 - in /logging/log4net/trunk: src/Appender/EventLogAppender.cs tests/src/Appender/EventLogAppenderTest.cs
Date Mon, 12 Sep 2011 10:37:31 GMT
Author: bodewig
Date: Mon Sep 12 10:37:30 2011
New Revision: 1169681

URL: http://svn.apache.org/viewvc?rev=1169681&view=rev
Log:
Deal with SecurityException in EventLogAppender more gracefully.  LOG4NET-310

Modified:
    logging/log4net/trunk/src/Appender/EventLogAppender.cs
    logging/log4net/trunk/tests/src/Appender/EventLogAppenderTest.cs

Modified: logging/log4net/trunk/src/Appender/EventLogAppender.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/EventLogAppender.cs?rev=1169681&r1=1169680&r2=1169681&view=diff
==============================================================================
--- logging/log4net/trunk/src/Appender/EventLogAppender.cs (original)
+++ logging/log4net/trunk/src/Appender/EventLogAppender.cs Mon Sep 12 10:37:30 2011
@@ -233,60 +233,60 @@ namespace log4net.Appender
 		/// </remarks>
 		override public void ActivateOptions() 
 		{
-			base.ActivateOptions();
+            try {
+                base.ActivateOptions();
 
-			if (m_securityContext == null)
-			{
-				m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);
-			}
-
-			bool sourceAlreadyExists = false;
-			string currentLogName = null;
-
-			using(SecurityContext.Impersonate(this))
-			{
-				sourceAlreadyExists = EventLog.SourceExists(m_applicationName);
-				if (sourceAlreadyExists)
-				{
-					currentLogName = EventLog.LogNameFromSourceName(m_applicationName, m_machineName);
-				}
-			}
-
-			if (sourceAlreadyExists && currentLogName != m_logName)
-			{
-				LogLog.Debug(declaringType, "Changing event source [" + m_applicationName + "] from log
[" + currentLogName + "] to log [" + m_logName + "]");
-			}
-			else if (!sourceAlreadyExists)
-			{
-				LogLog.Debug(declaringType, "Creating event source Source [" + m_applicationName + "]
in log " + m_logName + "]");
-			}
-
-			string registeredLogName = null;
-
-			using(SecurityContext.Impersonate(this))
-			{
-				if (sourceAlreadyExists && currentLogName != m_logName)
-				{
-					//
-					// Re-register this to the current application if the user has changed
-					// the application / logfile association
-					//
-					EventLog.DeleteEventSource(m_applicationName, m_machineName);
-					CreateEventSource(m_applicationName, m_logName, m_machineName);
-
-					registeredLogName = EventLog.LogNameFromSourceName(m_applicationName, m_machineName);
-				}
-				else if (!sourceAlreadyExists)
-				{
-					CreateEventSource(m_applicationName, m_logName, m_machineName);
-
-					registeredLogName = EventLog.LogNameFromSourceName(m_applicationName, m_machineName);
-				}
-			}
-
-			m_levelMapping.ActivateOptions();
-
-			LogLog.Debug(declaringType, "Source [" + m_applicationName + "] is registered to log ["
+ registeredLogName + "]");		
+                if (m_securityContext == null) {
+                    m_securityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);
+                }
+
+                bool sourceAlreadyExists = false;
+                string currentLogName = null;
+
+                using (SecurityContext.Impersonate(this)) {
+                    sourceAlreadyExists = EventLog.SourceExists(m_applicationName);
+                    if (sourceAlreadyExists) {
+                        currentLogName = EventLog.LogNameFromSourceName(m_applicationName,
m_machineName);
+                    }
+                }
+
+                if (sourceAlreadyExists && currentLogName != m_logName) {
+                    LogLog.Debug(declaringType, "Changing event source [" + m_applicationName
+ "] from log [" + currentLogName + "] to log [" + m_logName + "]");
+                }
+                else if (!sourceAlreadyExists) {
+                    LogLog.Debug(declaringType, "Creating event source Source [" + m_applicationName
+ "] in log " + m_logName + "]");
+                }
+
+                string registeredLogName = null;
+
+                using (SecurityContext.Impersonate(this)) {
+                    if (sourceAlreadyExists && currentLogName != m_logName) {
+                        //
+                        // Re-register this to the current application if the user has changed
+                        // the application / logfile association
+                        //
+                        EventLog.DeleteEventSource(m_applicationName, m_machineName);
+                        CreateEventSource(m_applicationName, m_logName, m_machineName);
+
+                        registeredLogName = EventLog.LogNameFromSourceName(m_applicationName,
m_machineName);
+                    }
+                    else if (!sourceAlreadyExists) {
+                        CreateEventSource(m_applicationName, m_logName, m_machineName);
+
+                        registeredLogName = EventLog.LogNameFromSourceName(m_applicationName,
m_machineName);
+                    }
+                }
+
+                m_levelMapping.ActivateOptions();
+
+                LogLog.Debug(declaringType, "Source [" + m_applicationName + "] is registered
to log [" + registeredLogName + "]");
+            }
+            catch (System.Security.SecurityException ex) {
+                ErrorHandler.Error("Caught a SecurityException trying to access the EventLog.
 Most likely the event source "
+                    + m_applicationName
+                    + " doesn't exist and must be created by a local administrator.  Will
disable EventLogAppender", ex);
+                Threshold = Level.Off;
+            }
 		}
 
 		#endregion // Implementation of IOptionHandler

Modified: logging/log4net/trunk/tests/src/Appender/EventLogAppenderTest.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Appender/EventLogAppenderTest.cs?rev=1169681&r1=1169680&r2=1169681&view=diff
==============================================================================
--- logging/log4net/trunk/tests/src/Appender/EventLogAppenderTest.cs (original)
+++ logging/log4net/trunk/tests/src/Appender/EventLogAppenderTest.cs Mon Sep 12 10:37:30 2011
@@ -40,9 +40,10 @@ namespace log4net.Tests.Appender
 		/// event log enumeration is returned
 		/// </summary>
 		[Test]
-		public void TestGetEntryType()
+		public void TestGetEntryTypeForExistingApplicationName()
 		{
 			EventLogAppender eventAppender = new EventLogAppender();
+            eventAppender.ApplicationName = "Winlogon";
 			eventAppender.ActivateOptions();
 
 			Assert.AreEqual(
@@ -74,6 +75,18 @@ namespace log4net.Tests.Appender
 				GetEntryType(eventAppender, Level.Off));
 		}
 
+        /// <summary>
+        /// ActivateOption tries to create an event source if it doesn't exist but this is
going to fail on more modern Windows versions unless the code is run with local administrator
privileges.
+        /// </summary>
+        [Test]
+        [Platform(Exclude = "Win2K,WinXP")]
+        public void ActivateOptionsDisablesAppenderIfSourceDoesntExist()
+        {
+            EventLogAppender eventAppender = new EventLogAppender();
+            eventAppender.ActivateOptions();
+            Assert.AreEqual(Level.Off, eventAppender.Threshold);
+        }
+
 		//
 		// Helper functions to dig into the appender
 		//
@@ -82,5 +95,6 @@ namespace log4net.Tests.Appender
 		{
 			return (EventLogEntryType)Utils.InvokeMethod(appender, "GetEntryType", level);
 		}
+
 	}
 }
\ No newline at end of file



Mime
View raw message