EL 4.1 Application Logging errors with file location variables

Topics: Enterprise Library Core, Logging Application Block
Nov 3, 2009 at 3:16 PM

I need some assistance with a logging issue I am having.

I am using Enterprise LIbrary 4.1 and the logging application block for multiple applications.  I have a separate logging configuration file that contains 4 listeners total so the logging configuration is externalized from each applications app.config file.

4 Applications

Client A - File Listener

Client B-  File Listener

Web A - Event Listener

Web B - Event Listener

The problem occurs when I use a user variable (%USERPROFILE%\AppData\Roaming\App\LogFiles\Tools.log) for the client file listeners log file location.  When placed on the web server I get an error when logging is initialized.  Here is the error.

The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: Access to the path 'C:\Windows\system32\config\systemprofile\AppData\Roaming\App\LogFiles' is denied. (Strategy type ConfiguredObjectStrategy, index 2)

If I remove the user variables from the file based listeners (remember client apps only) then everything works as expected. 

Does anyone know of any way to execute this without splitting the configuration into two separate config files?  I am trying to avoid doing this for maintenance and deployment reasons. 

 

Nov 4, 2009 at 2:01 AM

Since you don't want to have two config files, your option then would be to modify the config programmatically.  The modification will be based on the permission set on the filepath where you want to put your log file.  Here is a sample code on how to modify the config during runtime.

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourConfig.config";
Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
<font size="2" color="#a31515">
//Modify the filepath of the "MyTraceListener" FlatFileTraceListener 
</font>

FlatFileTraceListenerData flatFile = loggingSettings.TraceListeners.FirstOrDefault(f => f.Name == "MyTraceListener") as FlatFileTraceListenerData;@"C:\MyFlatFilePath.txt";

flatFile.FileName = @"C:\MyFlatFilePath.txt";

//Save the changes
entLibConfig.Save();
 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 4, 2009 at 2:36 AM

Note that while your solution would work it would be costly to implement in multiple applications (4-5 web applications and 8-10 client applications).  This would defeat the purpose as it would modify the file incorrectly for other applications.  This file maybe used by multiple applications at the same time.  Each needs to have it's configuration set properly.  Our solution has a fall back method so the applications look in a common location and if not found then the local bin folder to the application.

To resolve this I am going to do the following.

1.  Modify our common logging solution to look in the bin folder first (as an override) and then the common directory.  This will require two separate files (didn't want this) but may have too. 

2.  For the web applications a file without the variable paths will be placed in the common location and one with the variables in the local bin folder to the client applications. 

If anyone has any other solutions please post - it maybe helpful for others.