Disable trace listeners at runtime

Topics: Logging Application Block
Jun 15, 2010 at 8:03 AM

Hi All

I have found this thread http://entlib.codeplex.com/Thread/View.aspx?ThreadId=63622 using an version prior to version 5.0

I was wondering is it possible to disable at run time any of the trace listeners?

Many thanks

Paul

Jun 15, 2010 at 3:23 PM

Yes it is possible. You can set the Severity Filter to Off during runtime to disable the logging of the specified trace listener. And yes this can also be done same with the thread you have found. It should look something like this;

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

            FlatFileTraceListenerData flat1 = setting.TraceListeners.Get("Flat File Trace Listener 2") as FlatFileTraceListenerData;
            flat1.Filter = SourceLevels.Off;

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

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

Jun 16, 2010 at 1:43 PM

Hi Gino

Thats great thanks, if a logger is running at the time will it shut it down ? I would really like to shut a listener down. So I can run NUNit test again each one ( I have rolled my own listeners )

( the reason I would like to shut it down is the xmllistener keeps the file locked and I would like to delete or truncate the file before running the tests.

Regards

Paul

Jun 17, 2010 at 7:59 AM

Hi Paul,

It is not the Trace Listener that keeps the file locked, it is actually the Logger/LogWriter. So if you want to delete the log file during runtime, one thing I can think of on how you can do this is to reset or disposed the Logger/LogWriter before you do the actual deletion of the file during runtime (Logger - Logger.Reset(); , LogWriter - writer.Dispose();). Then you can proceed with the next logging execution (if you're using a LogWriter object you need to instantiate its value again before proceeding into the next logging execution). 

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

 

 

 

Sep 1, 2010 at 12:56 PM

Hi

This all worked fine on developer PC's updating the app.config file. But when we deploy the app into C:\Program Files\My App\ we don't have righs to update a file in this directory. Any thoughts either to access the app.config from a different directory. Or some other means.

Many thanks

Paul

 

Sep 2, 2010 at 7:35 AM

What I really want to be able todo is, change the file path when the appliction loads, to save the log file in different directory for each use that logins onto the computer.

Sep 2, 2010 at 7:44 AM

All I can think of is either provide user of your App a write permission to update the app.config in its deployment folder or try if the option of using Entlib5 Fluent Configuration API will work for you.

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

 

Sep 3, 2010 at 10:04 AM
Edited Sep 6, 2010 at 12:06 PM

Hi

      builder.ConfigureLogging()
             .LogToCategoryNamed("General").WithOptions.ToSourceLevels(Status).SetAsDefaultCategory()
             .SendTo.MySpecialXmlFile("Log File")
             .ToFile(@"c:\log.xml").WithTraceOptions(System.Diagnostics.TraceOptions.Callstack);

      var configSource = new DictionaryConfigurationSource();
      builder.UpdateConfigurationWithReplace(configSource);
      EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

 

Got this working, and it works great. I don't want to use a APP.config to store the config information. So once I have setup the logger, how can I change the "MySpecialXmlFile" parameters. ( like the filename ). Also how do I change the "General" category filter etc. Tried below only, but only shows the listeners, doesn't seem to be awat of getting the configuration data.

 

      var _writer = (LogWriterImpl)EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

      foreach (var w in _writer.TraceSources)
      {
        if (w.Value.Level != System.Diagnostics.SourceLevels.Off)
        {
          foreach (var l in w.Value.Listeners)
          {
            var aListener = ((ReconfigurableTraceListenerWrapper)l).InnerTraceListener;
            if (aListener != null)
            {

            }
          }
        }
      }