Changing TraceListeners at RunTime

Topics: Logging Application Block
Oct 6, 2010 at 2:11 AM

Hi there,

I'm trying to use the Enterprise Library 5.0 logging block for logging in my application. The problem I'm finding is how to modify the settings of my TraceListeners programmatically. I've seen the thread at http://entlib.codeplex.com/Thread/View.aspx?ThreadId=30391 and the environment variable works for the log file name to be set by the user, but I need more flexibility, for example:

  • Not logging at all if the user does not set a log file name
  • Being able to modify the severity filter, so the user can decide what to get in the log

From reading the thread above I think my bets option could be to create a custom TraceListener class inheriting from the one I need in order to do this. Can someone confirm this, or if there would be an easier way modifying the configuration of an existing trace listener from code? If the new class is the way to go, are there any sample code to look at?

Thanks,
Dioni

 

 

 

Oct 6, 2010 at 2:40 AM

Yes, you would create a custom trace listener for that.  I don't have a sample custom trace listener that is related to your requirement, I only have  a sample custom database trace listener.

On modifying settings programmatically, you may also need something like what I suggested in this thread.

 

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

Oct 7, 2010 at 4:43 PM
Edited Oct 7, 2010 at 9:55 PM

Thanks for the help, Sarah,

using the Fluent Configuration seemed to be a good way to go but it seems that it creates new Listeners, Formatters, etc. Is there a way to access from code the Categories, Listeners and Formatters already in config file to modify them as necessary with the options set by the user, rather than having to set everything in the code

Thanks,
Dioni

Oct 8, 2010 at 12:38 AM

The Fluent Configuration API currently doesn't provide you that option.  The other option you have is also mentioned in the thread I referred you to which is accessing the LoggingSettings object from your config.  Here's the link to that other thread.

 

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

Oct 8, 2010 at 1:56 AM

Yes, I saw that one too, but was hoping there could be some simpler way of deserializing, modifying, and using without having to save to disk each time. My app is invoked through command line and there're parallel access scenarios that I'm not very sure would work correctly.

I hope there's an easier solution for this in future versions of the library, since I believe this is a very standard scenario for logging.

Again, thanks a lot for your help. I'll try loading the configuration using your code sample and modifying it on the fly.

Thanks,
Dioni

Oct 8, 2010 at 11:57 PM

Hi again,

when using the code in the other thread it throws an exception of type TypeInitializationException in line:

            LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

with inner exception:

An error occurred creating the configuration section handler for loggingConfiguration: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

This seems to be originated in the line 18 of the config file, which contains:

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

Any ideas of why this might be?

Thanks,
Dioni

Oct 9, 2010 at 1:55 AM

Ok, got to fix it. It seems there was a problem with the references from VS.

BTW, your code snipet at http://entlib.codeplex.com/Thread/View.aspx?ThreadId=63622 works much better than the one you pointed in this thread.

Thanks,
Dioni