Logger does not work in newly created AppDomain

1.1k Views Asked by At

In my Application I use Common.Logging library in order to abstract logging functionality. In startup assembly it was configured (in app.config file) to work against Log4Net library. There are established some Appenders: ConsoleAppender, RollingFileAppender, TraceAppender. Everything works fine in the single AppDomain. However I have found that logging does not work in the newly created AppDomain. i.e:

Logger.Info( "Logging works here" ); // Logging works here

var appDomain = AppDomain.CreateDomain(
                              "AppDomain." + componentHost.FullName,
                               AppDomain.CurrentDomain.Evidence,
                               AppDomain.CurrentDomain.SetupInformation );

var proxy = (IComponentHost) appDomain.CreateInstanceAndUnwrap(
                               componentHost.Assembly.FullName,
                               componentHost.FullName,
                               false,
                               BindingFlags.Default,
                               null,
                               new object[] { },
                               null,
                               null );

proxy.Init(); // Logging does not work in Init() method stack

I'm trying to find a solution, both using app.config configuration as well as Common.Logging API (i.e LogManager.Reset() method), but it doesn't solve a problem.

How can I force Common.Logging / Log4Net to work properly in newly created AppDomain? Please for help.

2

There are 2 best solutions below

0
On BEST ANSWER

I have a similar issue with log4net. I used XmlConfigurator.Configure() without parameters that uses Assembly.GetCallingAssembly() to get configuration file. I replaced it with:

var configFile = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
if (!configFile.Exists)
    throw new Exception($"Failed to find configuration file '{configFile.FullName}'");
XmlConfigurator.Configure(configFile);
2
On

From the code you show, it looks like you're not providing a configuration file for your app domain. It's been a few years since I've done this, but if I recall correctly, the configuration file for a new app domain is empty by default.

Here's an example of how to use the AppDomainSetup class:

AppDomainSetup ads = new AppDomainSetup();

ads.SetConfigurationBytes( Encoding.UTF8.GetBytes(
@"<configuration>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5"></remove>
  </DbProviderFactories>
</configuration>" ) ); 

(Credit to Tom Overton for the code sample.)

See http://social.msdn.microsoft.com/Forums/vstudio/en-US/a23ff0ad-8a4c-4aaf-8281-dcc7e840f8a5/assigning-appconfig-to-appdomain?forum=clr for his explanation of this issue.