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 2007/12/22 20:19:13 UTC
svn commit: r606477 - in /logging/log4net/trunk: src/Util/PatternString.cs
src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs
tests/src/Util/PatternStringTest.cs
Author: rgrabowski
Date: Sat Dec 22 11:19:13 2007
New Revision: 606477
URL: http://svn.apache.org/viewvc?rev=606477&view=rev
Log:
Fix for LOG4NET-115. Added EnvironmentFolderPathPatternConverter to allow System.Environment.SpecialFolder pattern string expansion.
Added:
logging/log4net/trunk/src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs
logging/log4net/trunk/tests/src/Util/PatternStringTest.cs
Modified:
logging/log4net/trunk/src/Util/PatternString.cs
Modified: logging/log4net/trunk/src/Util/PatternString.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/PatternString.cs?rev=606477&r1=606476&r2=606477&view=diff
==============================================================================
--- logging/log4net/trunk/src/Util/PatternString.cs (original)
+++ logging/log4net/trunk/src/Util/PatternString.cs Sat Dec 22 11:19:13 2007
@@ -288,6 +288,7 @@
s_globalRulesRegistry.Add("date", typeof(DatePatternConverter));
#if !NETCF
s_globalRulesRegistry.Add("env", typeof(EnvironmentPatternConverter));
+ s_globalRulesRegistry.Add("envFolderPath", typeof(EnvironmentFolderPathPatternConverter));
#endif
s_globalRulesRegistry.Add("identity", typeof(IdentityPatternConverter));
s_globalRulesRegistry.Add("literal", typeof(LiteralPatternConverter));
Added: logging/log4net/trunk/src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs?rev=606477&view=auto
==============================================================================
--- logging/log4net/trunk/src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs (added)
+++ logging/log4net/trunk/src/Util/PatternStringConverters/EnvironmentFolderPathPatternConverter.cs Sat Dec 22 11:19:13 2007
@@ -0,0 +1,85 @@
+#region Apache License
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to you 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
+
+#if !NETCF
+
+using System;
+using System.IO;
+
+namespace log4net.Util.PatternStringConverters
+{
+ /// <summary>
+ /// Write an <see cref="System.Environment.SpecialFolder" /> folder path to the output
+ /// </summary>
+ /// <remarks>
+ /// <para>
+ /// Write an special path environment folder path to the output writer.
+ /// The value of the <see cref="log4net.Util.PatternConverter.Option"/> determines
+ /// the name of the variable to output. <see cref="log4net.Util.PatternConverter.Option"/>
+ /// should be a value in the <see cref="System.Environment.SpecialFolder" /> enumeration.
+ /// </para>
+ /// </remarks>
+ /// <author>Ron Grabowski</author>
+ internal sealed class EnvironmentFolderPathPatternConverter : PatternConverter
+ {
+ /// <summary>
+ /// Write an special path environment folder path to the output
+ /// </summary>
+ /// <param name="writer">the writer to write to</param>
+ /// <param name="state">null, state is not set</param>
+ /// <remarks>
+ /// <para>
+ /// Writes the special path environment folder path to the output <paramref name="writer"/>.
+ /// The name of the special path environment folder path to output must be set
+ /// using the <see cref="log4net.Util.PatternConverter.Option"/>
+ /// property.
+ /// </para>
+ /// </remarks>
+ override protected void Convert(TextWriter writer, object state)
+ {
+ try
+ {
+ if (Option != null && Option.Length > 0)
+ {
+ Environment.SpecialFolder specialFolder =
+ (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), Option, true);
+
+ string envFolderPathValue = Environment.GetFolderPath(specialFolder);
+ if (envFolderPathValue != null && envFolderPathValue.Length > 0)
+ {
+ writer.Write(envFolderPathValue);
+ }
+ }
+ }
+ catch (System.Security.SecurityException secEx)
+ {
+ // This security exception will occur if the caller does not have
+ // unrestricted environment permission. If this occurs the expansion
+ // will be skipped with the following warning message.
+ LogLog.Debug("EnvironmentPatternConverter: Security exception while trying to expand environment variables. Error Ignored. No Expansion.", secEx);
+ }
+ catch (Exception ex)
+ {
+ LogLog.Error("EnvironmentPatternConverter: Error occurred while converting environment variable.", ex);
+ }
+ }
+ }
+}
+
+#endif // !NETCF
\ No newline at end of file
Added: logging/log4net/trunk/tests/src/Util/PatternStringTest.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Util/PatternStringTest.cs?rev=606477&view=auto
==============================================================================
--- logging/log4net/trunk/tests/src/Util/PatternStringTest.cs (added)
+++ logging/log4net/trunk/tests/src/Util/PatternStringTest.cs Sat Dec 22 11:19:13 2007
@@ -0,0 +1,30 @@
+using System;
+using log4net.Util;
+using NUnit.Framework;
+
+namespace log4net.Tests.Util
+{
+ [TestFixture]
+ public class PatternStringTest
+ {
+ [Test]
+ public void TestEnvironmentFolderPathPatternConverter()
+ {
+ string[] specialFolderNames = Enum.GetNames(typeof(Environment.SpecialFolder));
+
+ foreach (string specialFolderName in specialFolderNames)
+ {
+ string pattern = "%envFolderPath{" + specialFolderName + "}";
+
+ PatternString patternString = new PatternString(pattern);
+
+ string evaluatedPattern = patternString.Format();
+
+ Environment.SpecialFolder specialFolder =
+ (Environment.SpecialFolder)Enum.Parse(typeof(Environment.SpecialFolder), specialFolderName);
+
+ Assert.AreEqual(Environment.GetFolderPath(specialFolder), evaluatedPattern);
+ }
+ }
+ }
+}