logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dpsen...@apache.org
Subject svn commit: r1483375 - /logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs
Date Thu, 16 May 2013 14:02:23 GMT
Author: dpsenner
Date: Thu May 16 14:02:22 2013
New Revision: 1483375

URL: http://svn.apache.org/r1483375
Log:
LOG4NET-376 fixed a race condition in AbsoluteTimeDateFormatter by adding a lock around s_lastTimeStrings

Modified:
    logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs

Modified: logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs?rev=1483375&r1=1483374&r2=1483375&view=diff
==============================================================================
--- logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs (original)
+++ logging/log4net/trunk/src/DateFormatter/AbsoluteTimeDateFormatter.cs Thu May 16 14:02:22
2013
@@ -101,64 +101,67 @@ namespace log4net.DateFormatter
 		/// </remarks>
 		virtual public void FormatDate(DateTime dateToFormat, TextWriter writer)
 		{
-			// Calculate the current time precise only to the second
-			long currentTimeToTheSecond = (dateToFormat.Ticks - (dateToFormat.Ticks % TimeSpan.TicksPerSecond));
+			lock (s_lastTimeStrings)
+			{
+				// Calculate the current time precise only to the second
+				long currentTimeToTheSecond = (dateToFormat.Ticks - (dateToFormat.Ticks % TimeSpan.TicksPerSecond));
 
-                        string timeString = null;
-			// Compare this time with the stored last time
-			// If we are in the same second then append
-			// the previously calculated time string
-                        if (s_lastTimeToTheSecond != currentTimeToTheSecond)
-                        {
-                            s_lastTimeStrings.Clear();
-                        }
-                        else
-                        {
-                            timeString = (string) s_lastTimeStrings[GetType()];
-                        }
-
-                        if (timeString == null)
-                        {
-				// lock so that only one thread can use the buffer and
-				// update the s_lastTimeToTheSecond and s_lastTimeStrings
+				string timeString = null;
+				// Compare this time with the stored last time
+				// If we are in the same second then append
+				// the previously calculated time string
+				if (s_lastTimeToTheSecond != currentTimeToTheSecond)
+				{
+					s_lastTimeStrings.Clear();
+				}
+				else
+				{
+					timeString = (string)s_lastTimeStrings[GetType()];
+				}
 
-				// PERF: Try removing this lock and using a new StringBuilder each time
-				lock(s_lastTimeBuf)
+				if (timeString == null)
 				{
-                                        timeString = (string) s_lastTimeStrings[GetType()];
+					// lock so that only one thread can use the buffer and
+					// update the s_lastTimeToTheSecond and s_lastTimeStrings
 
-                                        if (timeString == null)
-                                        {
-						// We are in a new second.
-						s_lastTimeBuf.Length = 0;
+					// PERF: Try removing this lock and using a new StringBuilder each time
+					lock (s_lastTimeBuf)
+					{
+						timeString = (string)s_lastTimeStrings[GetType()];
+
+						if (timeString == null)
+						{
+							// We are in a new second.
+							s_lastTimeBuf.Length = 0;
 
-						// Calculate the new string for this second
-						FormatDateWithoutMillis(dateToFormat, s_lastTimeBuf);
+							// Calculate the new string for this second
+							FormatDateWithoutMillis(dateToFormat, s_lastTimeBuf);
 
-						// Render the string buffer to a string
-                                                timeString = s_lastTimeBuf.ToString();
+							// Render the string buffer to a string
+							timeString = s_lastTimeBuf.ToString();
 
 #if NET_1_1
 						// Ensure that the above string is written into the variable NOW on all threads.
 						// This is only required on multiprocessor machines with weak memeory models
 						System.Threading.Thread.MemoryBarrier();
 #endif
-						// Store the time as a string (we only have to do this once per second)
-                                                s_lastTimeStrings[GetType()] = timeString;
-						s_lastTimeToTheSecond = currentTimeToTheSecond;
+							// Store the time as a string (we only have to do this once per second)
+							s_lastTimeStrings[GetType()] = timeString;
+							s_lastTimeToTheSecond = currentTimeToTheSecond;
+						}
 					}
 				}
+				writer.Write(timeString);
 			}
-			writer.Write(timeString);
-	
+			
 			// Append the current millisecond info
 			writer.Write(',');
 			int millis = dateToFormat.Millisecond;
-			if (millis < 100) 
+			if (millis < 100)
 			{
 				writer.Write('0');
 			}
-			if (millis < 10) 
+			if (millis < 10)
 			{
 				writer.Write('0');
 			}



Mime
View raw message