LOG4NET-388 fixed a NotSupportedException thrown in SystemInfo when encountering a dynamic assembly
Kudos to Piotr Westfalewicz for working out the patch.
diff --git a/src/Util/SystemInfo.cs b/src/Util/SystemInfo.cs
index f001aac..d61d5c9 100644
--- a/src/Util/SystemInfo.cs
+++ b/src/Util/SystemInfo.cs
@@ -457,10 +457,38 @@
{
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)
{
diff --git a/tests/src/Util/SystemInfoTest.cs b/tests/src/Util/SystemInfoTest.cs
index 9400d41..9e74406 100644
--- a/tests/src/Util/SystemInfoTest.cs
+++ b/tests/src/Util/SystemInfoTest.cs
@@ -23,6 +23,11 @@
using NUnit.Framework;
+#if NET_4_0
+using System.Linq.Expressions;
+using System.Reflection;
+#endif
+
namespace log4net.Tests.Util
{
/// <summary>
@@ -31,6 +36,39 @@
[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()
{