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 rg...@apache.org on 2006/07/29 21:17:45 UTC

svn commit: r426832 - in /logging/log4net/trunk: src/Layout/Pattern/ExceptionPatternConverter.cs tests/src/Layout/PatternLayoutTest.cs

Author: rgrabowski
Date: Sat Jul 29 12:17:44 2006
New Revision: 426832

URL: http://svn.apache.org/viewvc?rev=426832&view=rev
Log:
Fix for LOG4NET-77. ExceptionPatternConverter now supports outputting specific information (Source, StackTace, etc.) about an Exception.

Modified:
    logging/log4net/trunk/src/Layout/Pattern/ExceptionPatternConverter.cs
    logging/log4net/trunk/tests/src/Layout/PatternLayoutTest.cs

Modified: logging/log4net/trunk/src/Layout/Pattern/ExceptionPatternConverter.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Layout/Pattern/ExceptionPatternConverter.cs?rev=426832&r1=426831&r2=426832&view=diff
==============================================================================
--- logging/log4net/trunk/src/Layout/Pattern/ExceptionPatternConverter.cs (original)
+++ logging/log4net/trunk/src/Layout/Pattern/ExceptionPatternConverter.cs Sat Jul 29 12:17:44 2006
@@ -1,6 +1,6 @@
 #region Copyright & License
 //
-// Copyright 2001-2005 The Apache Software Foundation
+// Copyright 2001-2006 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.
@@ -16,10 +16,8 @@
 //
 #endregion
 
-using System;
-using System.Text;
+using System.Diagnostics;
 using System.IO;
-
 using log4net.Core;
 
 namespace log4net.Layout.Pattern
@@ -49,7 +47,7 @@
 		public ExceptionPatternConverter()
 		{
 			// This converter handles the exception
-			this.IgnoresException = false;
+			IgnoresException = false;
 		}
 
 		/// <summary>
@@ -64,18 +62,69 @@
 		/// trailing newline.
 		/// </para>
 		/// <para>
-		/// If there is no exception then nothing will be output
+		/// If there is no exception or the exception property specified
+		/// by the Option value does not exist then nothing will be output
 		/// and no trailing newline will be appended.
 		/// It is typical to put a newline before the exception
 		/// and to have the exception as the last data in the pattern.
 		/// </para>
+		/// <para>Recognized values for the Option parameter are:</para>
+		/// <list type="bullet">
+		///		<item>
+		///			<description>Message</description>
+		///		</item>
+		///		<item>
+		///			<description>Source</description>
+		///		</item>
+		///		<item>
+		///			<description>StackTrace</description>
+		///		</item>
+		///		<item>
+		///			<description>TargetSite</description>
+		///		</item>
+		///		<item>
+		///			<description>HelpLink</description>
+		///		</item>		
+		/// </list>
+		/// </para>
 		/// </remarks>
 		override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
 		{
-			string exceptionString = loggingEvent.GetExceptionString();
-			if (exceptionString != null && exceptionString.Length > 0) 
+			if (loggingEvent.ExceptionObject != null && Option != null && Option.Length > 0)
+			{
+				switch (Option.ToLower())
+				{
+					case "message":
+						WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.Message);
+						break;
+					case "source":
+						WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.Source);
+						break;
+					case "stacktrace":
+						WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.StackTrace);
+						break;
+					case "targetsite":
+						WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.TargetSite);
+						break;
+					case "helplink":
+						WriteObject(writer, loggingEvent.Repository, loggingEvent.ExceptionObject.HelpLink);
+						break;
+					default:
+						// do not output SystemInfo.NotAvailableText
+						break;
+				}
+			}
+			else
 			{
-				writer.WriteLine(exceptionString);
+				string exceptionString = loggingEvent.GetExceptionString();
+				if (exceptionString != null && exceptionString.Length > 0) 
+				{
+					writer.WriteLine(exceptionString);
+				}
+				else
+				{
+					// do not output SystemInfo.NotAvailableText
+				}
 			}
 		}
 	}

Modified: logging/log4net/trunk/tests/src/Layout/PatternLayoutTest.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Layout/PatternLayoutTest.cs?rev=426832&r1=426831&r2=426832&view=diff
==============================================================================
--- logging/log4net/trunk/tests/src/Layout/PatternLayoutTest.cs (original)
+++ logging/log4net/trunk/tests/src/Layout/PatternLayoutTest.cs Sat Jul 29 12:17:44 2006
@@ -128,5 +128,24 @@
 				loggingEvent.WriteRenderedMessage(writer);
 			}
 		}
+
+		[Test] public void TestExceptionPattern()
+		{
+			StringAppender stringAppender = new StringAppender();
+			PatternLayout layout = new PatternLayout("%exception{stacktrace}");
+			stringAppender.Layout = layout;
+
+			ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
+			BasicConfigurator.Configure(rep, stringAppender);
+
+			ILog log1 = LogManager.GetLogger(rep.Name, "TestExceptionPattern");
+
+			Exception exception = new Exception("Oh no!");
+			log1.Info("TestMessage", exception);
+			
+			Assert.AreEqual(SystemInfo.NullText, stringAppender.GetString());
+
+			stringAppender.Reset();	
+		}
 	}
 }