logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1169711 - in /logging/log4net/trunk/src: Appender/AppenderSkeleton.cs Util/PatternConverter.cs
Date Mon, 12 Sep 2011 12:36:06 GMT
Author: bodewig
Date: Mon Sep 12 12:36:06 2011
New Revision: 1169711

URL: http://svn.apache.org/viewvc?rev=1169711&view=rev
Log:
Race-Condition via shared StringBuilder in ReUsableStringWriter.  LOG4NET-76

Modified:
    logging/log4net/trunk/src/Appender/AppenderSkeleton.cs
    logging/log4net/trunk/src/Util/PatternConverter.cs

Modified: logging/log4net/trunk/src/Appender/AppenderSkeleton.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/AppenderSkeleton.cs?rev=1169711&r1=1169710&r2=1169711&view=diff
==============================================================================
--- logging/log4net/trunk/src/Appender/AppenderSkeleton.cs (original)
+++ logging/log4net/trunk/src/Appender/AppenderSkeleton.cs Mon Sep 12 12:36:06 2011
@@ -697,11 +697,14 @@ namespace log4net.Appender
 				m_renderWriter = new ReusableStringWriter(System.Globalization.CultureInfo.InvariantCulture);
 			}
 
-			// Reset the writer so we can reuse it
-			m_renderWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
-
-			RenderLoggingEvent(m_renderWriter, loggingEvent);
-			return m_renderWriter.ToString();
+            lock (m_renderWriter)
+            {
+                // Reset the writer so we can reuse it
+                m_renderWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
+
+                RenderLoggingEvent(m_renderWriter, loggingEvent);
+                return m_renderWriter.ToString();
+            }
 		}
 
 		/// <summary>

Modified: logging/log4net/trunk/src/Util/PatternConverter.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/PatternConverter.cs?rev=1169711&r1=1169710&r2=1169711&view=diff
==============================================================================
--- logging/log4net/trunk/src/Util/PatternConverter.cs (original)
+++ logging/log4net/trunk/src/Util/PatternConverter.cs Mon Sep 12 12:36:06 2011
@@ -174,33 +174,43 @@ namespace log4net.Util
 			}
 			else
 			{
-				m_formatWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
+                string msg = null;
+                int len;
+                lock (m_formatWriter)
+                {
+                    m_formatWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
+
+                    Convert(m_formatWriter, state);
+
+                    StringBuilder buf = m_formatWriter.GetStringBuilder();
+                    len = buf.Length;
+                    if (len > m_max)
+                    {
+                        msg = buf.ToString(len - m_max, m_max);
+                        len = m_max;
+                    }
+                    else
+                    {
+                        msg = buf.ToString();
+                    }
+                }
 
-				Convert(m_formatWriter, state);
-
-				StringBuilder buf = m_formatWriter.GetStringBuilder();
-				int len = buf.Length;
-
-				if (len > m_max)
-				{
-					writer.Write(buf.ToString(len - m_max, m_max));
-				}
-				else if (len < m_min) 
+				if (len < m_min) 
 				{
 					if (m_leftAlign) 
 					{	
-						writer.Write(buf.ToString());
+						writer.Write(msg);
 						SpacePad(writer, m_min - len);
 					}
 					else 
 					{
 						SpacePad(writer, m_min - len);
-						writer.Write(buf.ToString());
+						writer.Write(msg);
 					}
 				}
 				else
 				{
-					writer.Write(buf.ToString());
+					writer.Write(msg);
 				}
 			}
 		}	



Mime
View raw message