You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by bo...@apache.org on 2015/05/12 06:14:57 UTC

svn commit: r1678858 - /logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs

Author: bodewig
Date: Tue May 12 04:14:56 2015
New Revision: 1678858

URL: http://svn.apache.org/r1678858
Log:
LOG4NET-458 close and re-open syslog if multiple identities are used in different appenders

Modified:
    logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs

Modified: logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs?rev=1678858&r1=1678857&r2=1678858&view=diff
==============================================================================
--- logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs (original)
+++ logging/log4net/trunk/src/log4net/Appender/LocalSyslogAppender.cs Tue May 12 04:14:56 2015
@@ -356,9 +356,6 @@ namespace log4net.Appender
 			// so we do not need to hold on to the string itself, holding on to the
 			// handle will keep the heap ansi string alive.
 			m_handleToIdentity = Marshal.StringToHGlobalAnsi(identString);
-
-			// open syslog
-			openlog(m_handleToIdentity, 1, m_facility);
 		}
 
 		#endregion // IOptionHandler Implementation
@@ -378,17 +375,28 @@ namespace log4net.Appender
 		/// </para>
 		/// </remarks>
 #if FRAMEWORK_4_0_OR_ABOVE
-        [System.Security.SecuritySafeCritical]
+		[System.Security.SecuritySafeCritical]
 #endif
-        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
-        protected override void Append(LoggingEvent loggingEvent) 
+		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, UnmanagedCode = true)]
+		protected override void Append(LoggingEvent loggingEvent) 
 		{
 			int priority = GeneratePriority(m_facility, GetSeverity(loggingEvent.Level));
 			string message = RenderLoggingEvent(loggingEvent);
 
 			// Call the local libc syslog method
 			// The second argument is a printf style format string
-			syslog(priority, "%s", message);
+                        lock (SYSLOG_LOCK)
+                        {
+                            if (m_handleToIdentity != LAST_IDENTITY)
+                            {
+                                if (LAST_IDENTITY != IntPtr.Zero)
+                                {
+                                    closelog();
+                                }
+                                openlog(LAST_IDENTITY = m_handleToIdentity, 1, m_facility);
+                            }
+                            syslog(priority, "%s", message);
+                        }
 		}
 
 		/// <summary>
@@ -532,6 +540,18 @@ namespace log4net.Appender
 		/// </summary>
 		private LevelMapping m_levelMapping = new LevelMapping();
 
+		/// <summary>
+		///   lock object that ensures openlog and syslog
+		///   actually use the identity configured.
+		/// </summary>
+		private static readonly object SYSLOG_LOCK = new object();
+
+		/// <summary>
+		///   Holds the last identity used for openlog in
+		///   order to avoid redundant calss to openlog.
+		/// </summary>
+		private static IntPtr LAST_IDENTITY = IntPtr.Zero;
+
 		#endregion // Private Instances Fields
 
 		#region External Members