You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by ni...@apache.org on 2005/12/16 17:32:40 UTC

svn commit: r357179 - in /logging/log4net/trunk: src/Core/LoggingEvent.cs tests/src/Core/FixingTest.cs tests/src/log4net.Tests.csproj

Author: niall
Date: Fri Dec 16 08:31:21 2005
New Revision: 357179

URL: http://svn.apache.org/viewcvs?rev=357179&view=rev
Log:
Fix for LOG4NET-62. Fixing values now fixes them and prevents others from being updated.

Added:
    logging/log4net/trunk/tests/src/Core/FixingTest.cs
Modified:
    logging/log4net/trunk/src/Core/LoggingEvent.cs
    logging/log4net/trunk/tests/src/log4net.Tests.csproj

Modified: logging/log4net/trunk/src/Core/LoggingEvent.cs
URL: http://svn.apache.org/viewcvs/logging/log4net/trunk/src/Core/LoggingEvent.cs?rev=357179&r1=357178&r2=357179&view=diff
==============================================================================
--- logging/log4net/trunk/src/Core/LoggingEvent.cs (original)
+++ logging/log4net/trunk/src/Core/LoggingEvent.cs Fri Dec 16 08:31:21 2005
@@ -562,7 +562,7 @@
 		{
 			get
 			{
-				if (m_data.LocationInfo == null) 
+				if (m_data.LocationInfo == null  && this.m_cacheUpdatable) 
 				{
 					m_data.LocationInfo = new LocationInfo(m_callerStackBoundaryDeclaringType);
 				}
@@ -660,7 +660,7 @@
 		{
 			get 
 			{ 
-				if (m_data.Message == null)
+				if (m_data.Message == null && this.m_cacheUpdatable)
 				{
 					if (m_message == null)
 					{
@@ -740,7 +740,7 @@
 		{
 			get
 			{
-				if (m_data.ThreadName == null)
+				if (m_data.ThreadName == null && this.m_cacheUpdatable)
 				{
 #if NETCF
 					// Get thread ID only
@@ -823,7 +823,7 @@
 		{
 			get
 			{
-				if (m_data.UserName == null) 
+				if (m_data.UserName == null  && this.m_cacheUpdatable) 
 				{
 #if (NETCF || SSCLI)
 					// On compact framework there's no notion of current Windows user
@@ -871,7 +871,7 @@
 		{
 			get
 			{
-				if (m_data.Identity == null)
+				if (m_data.Identity == null  && this.m_cacheUpdatable)
 				{
 #if (NETCF || SSCLI)
 					// On compact framework there's no notion of current thread principals
@@ -919,7 +919,7 @@
 		{
 			get 
 			{ 
-				if (m_data.Domain == null)
+				if (m_data.Domain == null  && this.m_cacheUpdatable)
 				{
 					m_data.Domain = SystemInfo.ApplicationFriendlyName;
 				}
@@ -1104,7 +1104,7 @@
 		/// </remarks>
 		public string GetExceptionString() 
 		{
-			if (m_data.ExceptionString == null)
+			if (m_data.ExceptionString == null  && this.m_cacheUpdatable)
 			{
 				if (m_thrownException != null)
 				{
@@ -1213,10 +1213,15 @@
 		{
 			object forceCreation = null;
 
+			//Unlock the cache so that new values can be stored
+			//This may not be ideal if we are no longer in the correct context
+			//and someone calls fix. 
+			m_cacheUpdatable=true;
+
 			// determine the flags that we are actually fixing
 			FixFlags updateFlags = (FixFlags)((flags ^ m_fixFlags) & flags);
 
-			if (updateFlags > 0)
+			if (updateFlags > 0) 
 			{
 				if ((updateFlags & FixFlags.Message) != 0)
 				{
@@ -1225,13 +1230,6 @@
 
 					m_fixFlags |= FixFlags.Message;
 				}
-				if ((updateFlags & FixFlags.Message) != 0)
-				{
-					// Force the message to be rendered
-					forceCreation = this.RenderedMessage;
-
-					m_fixFlags |= FixFlags.Message;
-				}
 				if ((updateFlags & FixFlags.ThreadName) != 0)
 				{
 					// Grab the thread name
@@ -1289,6 +1287,9 @@
 			if (forceCreation != null) 
 			{
 			}
+
+			//Finaly lock everything we've cached.
+			m_cacheUpdatable=false;
 		}
 
 		#endregion Public Instance Methods
@@ -1323,7 +1324,7 @@
 
 		private void CacheProperties()
 		{
-			if (m_data.Properties == null)
+			if (m_data.Properties == null  && this.m_cacheUpdatable)
 			{
 				if (m_compositeProperties == null)
 				{
@@ -1491,6 +1492,15 @@
 		/// Not serialized.
 		/// </remarks>
 		private FixFlags m_fixFlags = FixFlags.None;
+
+		/// <summary>
+		/// Indicated that the internal cache is updateable (ie not fixed)
+		/// </summary>
+		/// <remarks>
+		/// This is a seperate flag to m_fixFlags as it allows incrementel fixing and simpler
+		/// changes in the caching strategy.
+		/// </remarks>
+		private bool m_cacheUpdatable = true;
 
 		#endregion Private Instance Fields
 

Added: logging/log4net/trunk/tests/src/Core/FixingTest.cs
URL: http://svn.apache.org/viewcvs/logging/log4net/trunk/tests/src/Core/FixingTest.cs?rev=357179&view=auto
==============================================================================
--- logging/log4net/trunk/tests/src/Core/FixingTest.cs (added)
+++ logging/log4net/trunk/tests/src/Core/FixingTest.cs Fri Dec 16 08:31:21 2005
@@ -0,0 +1,145 @@
+#region Copyright & License
+//
+// Copyright 2001-2005 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+#endregion
+
+using System;
+using System.Diagnostics;
+using System.Globalization;
+
+using log4net.Config;
+using log4net.Util;
+using log4net.Layout;
+using log4net.Core;
+using log4net.Appender;
+using log4net.Repository;
+
+using log4net.Tests.Appender;
+
+using NUnit.Framework;
+
+namespace log4net.Tests.Core
+{
+	/// <summary>
+	/// </remarks>
+	[TestFixture] public class FixingTest
+	{
+		private LoggingEventData BuildStandardEventData()
+		{
+			LoggingEventData ed=new LoggingEventData();
+			ed.LoggerName=typeof(FixingTest).FullName;
+			ed.Level=Level.Warn;
+			ed.Message="Logging event works";
+			ed.Domain="ReallySimpleApp";
+			ed.LocationInfo=new LocationInfo(typeof(FixingTest).Name,"Main","Class1.cs","29");	//Completely arbitary
+			ed.ThreadName=System.Threading.Thread.CurrentThread.Name;
+			ed.TimeStamp=new DateTime(2005,12,14,14,07,35,0);									//Completely arbitary
+			ed.ExceptionString="Exception occured here";
+			ed.UserName="TestUser";
+			return ed;
+		}
+
+		static FixingTest()
+		{
+			log4net.LogManager.CreateRepository("Test Repository");
+			System.Threading.Thread.CurrentThread.Name="Test thread";
+		}
+
+		[Test] public void TestUnfixedValues()
+		{
+			LoggingEventData ed=BuildStandardEventData();
+			
+			LoggingEvent evt=new LoggingEvent(
+				ed.LocationInfo.GetType(),
+				log4net.LogManager.GetRepository("Test Repository"),
+				ed.LoggerName,
+				ed.Level,
+				ed.Message,
+				new Exception("This is the exception")
+				);
+
+
+			Assert.AreEqual("domain-NUnitAddin.NUnit.dll",evt.Domain,"Domain is incorrect");
+			Assert.AreEqual("System.Exception: This is the exception",evt.GetExceptionString(),"Exception is incorrect");
+			Assert.AreEqual(FixFlags.None,evt.Fix,"Fixed Fields is incorrect");
+			Assert.AreEqual("",evt.Identity,"Identity is incorrect");
+			Assert.AreEqual(Level.Warn,evt.Level,"Level is incorrect");
+			Assert.AreEqual("get_LocationInformation",evt.LocationInformation.MethodName,"Location Info is incorrect");
+			Assert.AreEqual("log4net.Tests.Core.FixingTest",evt.LoggerName,"LoggerName is incorrect");
+			Assert.AreEqual(log4net.LogManager.GetRepository("Test Repository"),evt.Repository,"Repository is incorrect");
+			Assert.AreEqual("Test thread",evt.ThreadName,"ThreadName is incorrect");
+			Assert.IsNotNull(evt.TimeStamp,"TimeStamp is incorrect");
+			Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name ,evt.UserName,"UserName is incorrect");
+			Assert.AreEqual("Logging event works",evt.RenderedMessage,"Message is incorrect");
+		}
+
+		[Test] public void TestAllFixedValues()
+		{
+			LoggingEventData ed=BuildStandardEventData();
+			
+			LoggingEvent evt=new LoggingEvent(
+				ed.LocationInfo.GetType(),
+				log4net.LogManager.GetRepository("Test Repository"),
+				ed.LoggerName,
+				ed.Level,
+				ed.Message,
+				new Exception("This is the exception")
+				);
+			evt.Fix=FixFlags.All;
+
+			Assert.AreEqual("domain-NUnitAddin.NUnit.dll",evt.Domain,"Domain is incorrect");
+			Assert.AreEqual("System.Exception: This is the exception",evt.GetExceptionString(),"Exception is incorrect");
+			Assert.AreEqual(FixFlags.LocationInfo| FixFlags.UserName| FixFlags.Identity| FixFlags.Partial|FixFlags.Message | FixFlags.ThreadName | FixFlags.Exception | FixFlags.Domain | FixFlags.Properties,evt.Fix,"Fixed Fields is incorrect");
+			Assert.AreEqual("",evt.Identity,"Identity is incorrect");
+			Assert.AreEqual(Level.Warn,evt.Level,"Level is incorrect");
+			Assert.AreEqual("get_LocationInformation",evt.LocationInformation.MethodName,"Location Info is incorrect");
+			Assert.AreEqual("log4net.Tests.Core.FixingTest",evt.LoggerName,"LoggerName is incorrect");
+			Assert.AreEqual(log4net.LogManager.GetRepository("Test Repository"),evt.Repository,"Repository is incorrect");
+			Assert.AreEqual("Test thread",evt.ThreadName,"ThreadName is incorrect");
+			Assert.IsNotNull(evt.TimeStamp,"TimeStamp is incorrect");
+			Assert.AreEqual(System.Security.Principal.WindowsIdentity.GetCurrent().Name ,evt.UserName,"UserName is incorrect");
+			Assert.AreEqual("Logging event works",evt.RenderedMessage,"Message is incorrect");
+		}
+
+		[Test] public void TestNoFixedValues()
+		{
+			LoggingEventData ed=BuildStandardEventData();
+			
+			LoggingEvent evt=new LoggingEvent(
+				ed.LocationInfo.GetType(),
+				log4net.LogManager.GetRepository("Test Repository"),
+				ed.LoggerName,
+				ed.Level,
+				ed.Message,
+				new Exception("This is the exception")
+				);
+			evt.Fix=FixFlags.None;
+
+			Assert.IsNull(evt.Domain,"Domain is incorrect");
+			Assert.IsNull(evt.GetExceptionString(),"Exception is incorrect");
+			Assert.AreEqual(FixFlags.None,evt.Fix,"Fixed Fields is incorrect");
+			Assert.IsNull(evt.Identity,"Identity is incorrect");
+			Assert.AreEqual(Level.Warn,evt.Level,"Level is incorrect");
+			Assert.IsNull(evt.LocationInformation,"Location Info is incorrect");
+			Assert.AreEqual("log4net.Tests.Core.FixingTest",evt.LoggerName,"LoggerName is incorrect");
+			Assert.AreEqual(log4net.LogManager.GetRepository("Test Repository"),evt.Repository,"Repository is incorrect");
+			Assert.IsNull(evt.ThreadName,"ThreadName is incorrect");
+			Assert.IsNotNull(evt.TimeStamp,"TimeStamp is incorrect");
+			Assert.IsNull(evt.UserName,"UserName is incorrect");
+			Assert.IsNull(evt.RenderedMessage,"Message is incorrect");
+		}
+	}
+}

Modified: logging/log4net/trunk/tests/src/log4net.Tests.csproj
URL: http://svn.apache.org/viewcvs/logging/log4net/trunk/tests/src/log4net.Tests.csproj?rev=357179&r1=357178&r2=357179&view=diff
==============================================================================
--- logging/log4net/trunk/tests/src/log4net.Tests.csproj (original)
+++ logging/log4net/trunk/tests/src/log4net.Tests.csproj Fri Dec 16 08:31:21 2005
@@ -143,6 +143,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "Core\FixingTest.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "Core\ShutdownTest.cs"
                     SubType = "Code"
                     BuildAction = "Compile"