Reload or refresh the configuration

Topics: Logging Application Block
Nov 12, 2014 at 3:25 AM
I want to use the Logging block to log to a particular file for some time in the program, and then change the filename after sometime. I'm using the fluent interface, but when I configure it the second time, it is not working. Is there a way to refresh or reload the config after having done it once already?
static void Main(string[] args)
{

    FirstConfig();
    Logger.LogInfoMessage("Before processing"); //Some wrapper around EntLib logger methods

    //Do some processing for some time

    SecondConfig();
    Logger.LogInfoMessage("After processing");
}

private static void FirstConfig()
{
    var textFormatter = new FormatterBuilder()
        .TextFormatterNamed("First Text Formatter")
        .UsingTemplate("{message}");

    var builder = new ConfigurationSourceBuilder();
    builder.ConfigureLogging()
        .WithOptions.DoNotRevertImpersonation()
        .LogToCategoryNamed("General").WithOptions.SetAsDefaultCategory()
        .SendTo.FlatFile("First Listener")
        .FormatWith(textFormatter).WithHeader("").WithFooter("")
        .ToFile("Logs\\BeforeChange.log");

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

private static void SecondConfig()
{
    var textFormatter = new FormatterBuilder()
        .TextFormatterNamed("Second Text Formatter")
        .UsingTemplate("{message}");

    var builder = new ConfigurationSourceBuilder();
    builder.ConfigureLogging()
        .WithOptions.DoNotRevertImpersonation()
        .LogToCategoryNamed("General").WithOptions.SetAsDefaultCategory()
        .SendTo.FlatFile("Second Listener")
        .FormatWith(textFormatter).WithHeader("").WithFooter("")
        .ToFile("Logs\\AfterChange.log");

    var configSource = new DictionaryConfigurationSource();
    builder.UpdateConfigurationWithReplace(configSource);
    EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}
Nov 12, 2014 at 3:34 AM
You should dispose all trace listeners before performing a reconfiguration (this will release all file locks).

You can use the static Logger.Reset() to perform the dispose.
static void Main(string[] args)
{

    FirstConfig();
    Logger.LogInfoMessage("Before processing"); //Some wrapper around EntLib logger methods

    //Do some processing for some time

    Logger.Reset();

    SecondConfig();
    Logger.LogInfoMessage("After processing");
}
Also, while disposing, you should ensure that there are no write operations performed or they will fail.

If that doesn't resolve the issue, can you elaborate on what is not working?

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by Naru on 11/11/2014 at 9:45 PM
Nov 12, 2014 at 5:47 AM
Oh yes, that solved it. It was not intuitive for me to find the Reset method on Logger, I was searching for similar methods in the ELContainer.
Also, while disposing, you should ensure that there are no write operations performed or they will fail.
This would not happen unless it is in a multi-threaded environment, correct?
Nov 12, 2014 at 5:20 PM
This would not happen unless it is in a multi-threaded environment, correct?
Exactly.