Author: dpsenner Date: Wed Aug 14 07:34:34 2013 New Revision: 1513751 URL: http://svn.apache.org/r1513751 Log: LOG4NET-388 fixed a NotSupportedException thrown in SystemInfo when encountering a dynamic assembly
Kudos to Piotr Westfalewicz for working out the patch. Modified: logging/log4net/trunk/src/Util/SystemInfo.cs logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs Modified: logging/log4net/trunk/src/Util/SystemInfo.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/src/Util/SystemInfo.cs?rev=1513751&r1=1513750&r2=1513751&view=diff ============================================================================== --- logging/log4net/trunk/src/Util/SystemInfo.cs (original) +++ logging/log4net/trunk/src/Util/SystemInfo.cs Wed Aug 14 07:34:34 2013 @@ -457,10 +457,38 @@ namespace log4net.Util { try { - // This call requires FileIOPermission for access to the path - // if we don't have permission then we just ignore it and - // carry on. - return myAssembly.Location; +#if NET_4_0 + if (myAssembly.IsDynamic) + { + return "Dynamic Assembly"; + } +#else + if (myAssembly is System.Reflection.Emit.AssemblyBuilder) + { + return "Dynamic Assembly"; + } + else if(myAssembly.GetType().FullName == "System.Reflection.Emit.InternalAssemblyBuilder") + { + return "Dynamic Assembly"; + } +#endif + else + { + // This call requires FileIOPermission for access to the path + // if we don't have permission then we just ignore it and + // carry on. + return myAssembly.Location; + } + } + catch (NotSupportedException) + { + // The location information may be unavailable for dynamic assemblies and a NotSupportedException + // is thrown in those cases. See: http://msdn.microsoft.com/de-de/library/system.reflection.assembly.location.aspx + return "Dynamic Assembly"; + } + catch (TargetInvocationException ex) + { + return "Location Detect Failed (" + ex.Message + ")"; } catch (ArgumentException ex) { Modified: logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs?rev=1513751&r1=1513750&r2=1513751&view=diff ============================================================================== --- logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs (original) +++ logging/log4net/trunk/tests/src/Util/SystemInfoTest.cs Wed Aug 14 07:34:34 2013 @@ -23,6 +23,11 @@ using log4net.Util; using NUnit.Framework; +#if NET_4_0 +using System.Linq.Expressions; +using System.Reflection; +#endif + namespace log4net.Tests.Util { /// <summary> @@ -31,6 +36,39 @@ namespace log4net.Tests.Util [TestFixture] public class SystemInfoTest { + +#if NET_4_0 + /// <summary> + /// It's "does not throw not supported exception" NOT + /// "returns 'Dynamic Assembly' string for dynamic assemblies" by purpose. + /// <see cref="Assembly.GetCallingAssembly"/> can be JITted and inlined in different release configurations, + /// thus we cannot determine what the exact result of this test will be. + /// In 'Debug' GetCallingAssembly should return dynamic assembly named: 'Anonymously Hosted DynamicMethods Assembly' + /// whereas in 'Release' this will be inlined and the result will be something like 'X:\Y\Z\log4net.Tests.dll'. + /// Therefore simple check against dynamic assembly + /// in <see cref="SystemInfo.AssemblyLocationInfo"/> to avoid <see cref="NotSupportedException"/> 'Debug' release. + /// </summary> + [Test] + public void TestAssemblyLocationInfoDoesNotThrowNotSupportedExceptionForDynamicAssembly() + { + var systemInfoAssemblyLocationMethod = GetAssemblyLocationInfoMethodCall(); + + Assert.DoesNotThrow(() => systemInfoAssemblyLocationMethod()); + } + + private static Func<string> GetAssemblyLocationInfoMethodCall() + { + var method = typeof(SystemInfoTest).GetMethod("TestAssemblyLocationInfoMethod", new Type[0]); + var methodCall = Expression.Call(null, method, new Expression[0]); + return Expression.Lambda<Func<string>>(methodCall, new ParameterExpression[0]).Compile(); + } + + public static string TestAssemblyLocationInfoMethod() + { + return SystemInfo.AssemblyLocationInfo(Assembly.GetCallingAssembly()); + } +#endif + [Test] public void TestGetTypeFromStringFullyQualified() {