Filter by SourceLevels

Topics: Logging Application Block
Apr 28, 2010 at 3:49 PM

There appears to be no programmatic way to update the Level property of a LogWriter's TraceSources. Perhaps the sheer amount of "Enterpriseyness" is obscuring how one changes this property, regardless I'm unable to find any documentation associated with EntLib5 and updating the SourceLevels to toggle verbosity. I'd prefer to not have to make a category for each level I want.

 

Thanks.

Apr 28, 2010 at 4:18 PM
Edited Apr 28, 2010 at 4:21 PM

Additionally, the following code (hobbled together from incomplete discussion postings) does not appear to have any bearing on what is actually logged by the trace listeners:

 

Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
LoggingSettings loggingSettings = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);
foreach (var source in loggingSettings.TraceListeners)
{
    if (configuration.Verbosity <= 0)
    {
        source.Filter = SourceLevels.Information;
    }
    else if (configuration.Verbosity == 1)
    {
        source.Filter = SourceLevels.Verbose;
    }
    else
    {
        source.Filter = SourceLevels.All;
    }
}
xmlConfiguration.Save();

 

Apr 29, 2010 at 4:34 AM

Hi,

Have you already consider using Custom Filter? Or are there any other things you want to achive on this? Please let me know.

For cusstom filter see if this blog will help http://geekswithblogs.net/.NETonMyMind/archive/2006/05/15/78201.aspx

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Apr 29, 2010 at 5:39 AM

Sorry for my initial response, I think you may be able to make your code work just put some delay between after you've modified the config file and before executing the logging. See this thread for some more details http://entlib.codeplex.com/Thread/View.aspx?ThreadId=16623.

HTH, 

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 3, 2010 at 2:57 PM

Even after changing the delay there is no actual functioning filtering going on.

I can move the updating of the configuration to before or after the instantiation of the container and it still does nothing, zilch, nada.

What am I still missing? Is there an actual working piece of code in an example somewhere or is this an EntLib5 bug?

ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(100);

container = new UnityContainer();
EnterpriseLibraryContainer.ConfigureContainer(
    new UnityContainerConfigurator(container),
    new SystemConfigurationSource(true));

var locator = new UnityServiceLocator(container);
EnterpriseLibraryContainer.Current = locator;

Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
LoggingSettings loggingSettings = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);
foreach (var source in loggingSettings.TraceListeners)
{
    if (configuration.Verbosity <= 0)
    {
        source.Filter = SourceLevels.Information;
    }
    else if (configuration.Verbosity == 1)
    {
        source.Filter = SourceLevels.Verbose;
    }
    else
    {
        source.Filter = SourceLevels.All;
    }
}
xmlConfiguration.Save();
ConfigurationManager.RefreshSection(LoggingSettings.SectionName);

May 3, 2010 at 5:03 PM

It appears the problem stems from vshost.exe.config being used instead of .exe.config, thus changes were being ignored.

May 4, 2010 at 1:30 PM
Edited May 4, 2010 at 1:31 PM

Hmmm.. Unfortunately, I'm unable to reproduce your problem in my end, using the code below works well in a simple console application.

            ConfigurationChangeWatcher.SetDefaultPollDelayInMilliseconds(100);

            Configuration xmlConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            LoggingSettings setting = (LoggingSettings)xmlConfiguration.GetSection(LoggingSettings.SectionName);

            
            foreach (var source in setting.TraceListeners)
            {
                source.Filter = SourceLevels.Warning;

            }
            xmlConfiguration.Save();
            
            ConfigurationManager.RefreshSection(LoggingSettings.SectionName);

            Logger.Write("myMessage", "General", 0, 0, TraceEventType.Warning, "MyEvent");
            Logger.Write("myMessage", "General",0,0,TraceEventType.Verbose,"MyEvent");

By the way, just curios what is the actual SourceLevels/TraceEventType value of your LogEntry that you actually been logging?

Gino Terrado
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 4, 2010 at 3:08 PM

I was on VS2010 and somehow the VSHOST config file was stuck with a filter of SourceLevels.Information. Cleaning the solution and deleting the VSHOST config fixed the problem. However, the new issue is the XML configuration cannot be edited by users who lack access, which is a huge roadblock.