title: "The type LogWriter cannot be constructed" in web app in new AppDomain
body: <p>I'm trying to use the Microsoft Enterprise Library 5.0 Logging Application Block in a web application. I can write to the log from the main application, but when I attempt this from a second AppDomain, any attempt to log throws an exception (shown below).</p>
  
  <p>The exact same code, in a console application, works ok. I have narrowed it down to the following test code (just need to change the assembly and type name constants to match your project):</p>
  
  <pre><code>public class LogTester
  {
      public const string LogWriterAssembly = "LogTest";
      public const string LogWriterType = "LogTest.LogWriter";
  
      public static void DoLogTest()
      {
          LogWriter localLogger = new LogWriter();
          localLogger.Log("In first AppDomain");
  
          AppDomain domain = GetAppDomain();
  
          LogWriter remoteLogger = (LogWriter)domain.CreateInstanceAndUnwrap(LogWriterAssembly, LogWriterType);
          remoteLogger.Log("In second AppDomain");
      }
  
      private static AppDomain GetAppDomain()
      {
          string baseFolder = AppDomain.CurrentDomain.BaseDirectory;
  
          string folder = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath;
  
          if (String.IsNullOrEmpty(folder))
              folder = baseFolder;
  
          AppDomainSetup setup = new AppDomainSetup();
          setup.ApplicationName = "TestAppName";
          setup.ApplicationBase = baseFolder;
          setup.PrivateBinPath = folder;
          setup.ShadowCopyFiles = "true";
  
          return AppDomain.CreateDomain("TestAppName", null, setup);
      }
  }
  
  public class LogWriter : MarshalByRefObject
  {
      public void Log(string message)
      {
          Logger.Write(AppDomain.CurrentDomain.FriendlyName + ": " + message);
      }
  }
  </code></pre>
  
  <p>The logging configuration I'm using is just the default (created by the Enterprise Library Configuration Utility):</p>
  
  <pre><code>&lt;configSections&gt;
      &lt;section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /&gt;
  &lt;/configSections&gt;
  &lt;loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"&gt;
      &lt;listeners&gt;
          &lt;add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              source="Enterprise Library Logging" formatter="Text Formatter"
              log="" machineName="." traceOutputOptions="None" /&gt;
      &lt;/listeners&gt;
      &lt;formatters&gt;
          &lt;add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
              template="Timestamp: {timestamp}{newline}&amp;#xA;Message: {message}{newline}&amp;#xA;Category: {category}{newline}&amp;#xA;Priority: {priority}{newline}&amp;#xA;EventId: {eventid}{newline}&amp;#xA;Severity: {severity}{newline}&amp;#xA;Title:{title}{newline}&amp;#xA;Machine: {localMachine}{newline}&amp;#xA;App Domain: {localAppDomain}{newline}&amp;#xA;ProcessId: {localProcessId}{newline}&amp;#xA;Process Name: {localProcessName}{newline}&amp;#xA;Thread Name: {threadName}{newline}&amp;#xA;Win32 ThreadId:{win32ThreadId}{newline}&amp;#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
              name="Text Formatter" /&gt;
      &lt;/formatters&gt;
      &lt;categorySources&gt;
          &lt;add switchValue="All" name="General"&gt;
              &lt;listeners&gt;
                  &lt;add name="Event Log Listener" /&gt;
              &lt;/listeners&gt;
          &lt;/add&gt;
      &lt;/categorySources&gt;
      &lt;specialSources&gt;
          &lt;allEvents switchValue="All" name="All Events" /&gt;
          &lt;notProcessed switchValue="All" name="Unprocessed Category" /&gt;
          &lt;errors switchValue="All" name="Logging Errors &amp;amp; Warnings"&gt;
              &lt;listeners&gt;
                  &lt;add name="Event Log Listener" /&gt;
              &lt;/listeners&gt;
          &lt;/errors&gt;
      &lt;/specialSources&gt;
  &lt;/loggingConfiguration&gt;
  </code></pre>
  
  <p>In a console app, all works fine, both log messages are written. In a web app, however, I get the following exception when trying to write the second message:</p>
  
  <pre><code>[InvalidOperationException: The type LogWriter cannot be constructed. You must configure the container to supply this value.]
  
  [ResolutionFailedException: Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter", name = "(none)".
  Exception occurred while: while resolving.
  Exception is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.
  -----------------------------------------------
  At the time of the exception, the container was:
  
    Resolving Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(none)
  ]
  
  [ActivationException: Activation error occured while trying to get instance of type LogWriter, key ""]
     System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10582079
     System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type) +336
     MvcApplication1.Controllers.LogWriter.Log(String message) in c:\dev\Sage Data Cloud\Development\MvcApplication1\MvcApplication1\Controllers\DefaultController.cs:65
     MvcApplication1.Controllers.LogTester.DoLogTest() in c:\dev\Sage Data Cloud\Development\MvcApplication1\MvcApplication1\Controllers\DefaultController.cs:38
     MvcApplication1.Controllers.DefaultController.Index() in c:\dev\Sage Data Cloud\Development\MvcApplication1\MvcApplication1\Controllers\DefaultController.cs:18
     lambda_method(Closure , ControllerBase , Object[] ) +101
     System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
     System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +50
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass42.&lt;BeginInvokeSynchronousActionMethod&gt;b__41() +75
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +44
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass39.&lt;BeginInvokeActionMethodWithFilters&gt;b__33() +126
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4f.&lt;InvokeActionMethodFilterAsynchronously&gt;b__49() +323
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass37.&lt;BeginInvokeActionMethodWithFilters&gt;b__36(IAsyncResult asyncResult) +44
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +139
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2a.&lt;BeginInvokeAction&gt;b__20() +68
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass25.&lt;BeginInvokeAction&gt;b__22(IAsyncResult asyncResult) +184
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +136
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
     System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
     System.Web.Mvc.&lt;&gt;c__DisplayClass1d.&lt;BeginExecuteCore&gt;b__18(IAsyncResult asyncResult) +40
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +47
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +47
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
     System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__3(IAsyncResult asyncResult) +45
     System.Web.Mvc.Async.&lt;&gt;c__DisplayClass4.&lt;MakeVoidDelegate&gt;b__3(IAsyncResult ar) +47
     System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
     System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
     System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
     System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
     System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629708
     System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +155
  </code></pre>
  
  <p>I have references to the required Enterprise Library assemblies, so the Logging, ServiceLocation, Unity and Unity.Configuration assemblies are copied locally. </p>
  
  <p>I've tried debugging into the Enterprise Library code using .NET Reflector, but the object creation is all done using dynamically generated code which I can't step into (and I don't want to get into trying to reverse engineer that from the uncommented decompiled source!).</p>
  
  <p>Just wondering if anyone has come across this issue before, and possible solutions? Can't find this same issue anywhere else.</p>
  
  <p>Thanks in advance.
  Pete</p>