'Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Writer' threw an exception of type 'System.Configuration.ConfigurationErrorsException'

Topics: Enterprise Library Core, Logging Application Block
Nov 16, 2011 at 6:11 AM
Edited Nov 16, 2011 at 6:18 AM

Hi,

I am using VS 2005 windows application.To log the error I use enterprise library logger 4.1.0.0.

It is working fine when I run through .NET and I am able to log the errors.

I am calling the .NET project through wrapper class from Access Project.

When I call from access project I get the following error.

'Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Writer' threw an exception of type 'System.Configuration.ConfigurationErrorsException'

 I use the below code to get the appsettings and connectionstring details.

System.Configuration.Configuration config =System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

 

 

Nov 16, 2011 at 11:10 PM
Edited Nov 17, 2011 at 12:30 AM

The problem is that the AppDomain.CurrentDomain.BaseDirectory for your assembly is not the same directory as where your configuration file is located.  The AppDomain is running out of the office directory where Access.exe is located (something like C:\Program Files\Microsoft Office\Office10\ depending on your OS and system setup).

To overcome this you can manually load your configuration file from a known location.  E.g:
FileConfigurationSource fcs = new FileConfigurationSource(
    @"C:\Users\username\Documents\MyAppConfig\AccessInterop.dll.config");
var le = new LogEntry();
le.Message = "Message!";
le.Categories.Add("General");
var logger = new LogWriterFactory(fcs).Create();
logger.Write(le);

Note that you will also have to resolve any dependent assemblies.  The easiest way to do this is to add them to the GAC.  If you are only logging then you can add the following assemblies to the GAC:

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practicies.ObjectBuilder2.dll

If you are using more functionality of Enterprise Library you would have to add the appropriate assemblies.

This should solve your issue.  

For completeness, if anyone is using Enterprise Library 5 then the same approach will work with a slightly different implementation.  

The code for Enterprise Library 5 would look like:
FileConfigurationSource fcs = new FileConfigurationSource(
    @"C:\Users\username\Documents\MyAppConfig\AccessInterop.dll.config");

ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();
builder.UpdateConfigurationWithReplace(fcs);

var coreExtension = new EnterpriseLibraryCoreExtension(fcs);

IUnityContainer container = new UnityContainer();
container.AddExtension(coreExtension);

EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
logger.Write("EL MSG: " + message);
Then you would need the following assemblies in the GAC (for logging only):

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
Microsoft.Practices.Unity.Interception.dll

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com