问题
I want to log to a particular set of files/folders for some time, and then switch and start logging to a different set of files. For this, I'm using the fluent API to set my filename as I want (not shown here). But I'm not able to change the configuration midway in a program. It is not working the way I expected it. Are there any commands to reload or clear the configuration that I need to do if I'm setting up the config a second time?
If I comment out FirstConfig();
and the next Log
statement, then the SecondConfig()
works fine. Else, only FirstConfig()
seems to have an effect.
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 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);
}
回答1:
The last line is creating a new container and assigning the configuration to it, you're creating two different containers.
I think enterprise library allows detection of an existing logging configuration change to be recognized without an application restart. It's possible you want to just modify the existing configuration and then handle the change.
http://msdn.microsoft.com/en-us/library/ff664363%28PandP.50%29.aspx
All configuration source classes implement the IConfigurationSource interface. This interface allows your application code to subscribe to notifications of configuration changes. For more information, see Updating Configuration Settings at Run Time. By default, in Enterprise Library, only the Logging Application Block registers to receive notifications of configuration changes.
For Enterprise Library 5.0 http://msdn.microsoft.com/en-us/library/ff664640%28v=pandp.50%29.aspx
"The one exception to this is the Logging Application Block, which is able to detect configuration changes and reload the configuration without restarting the application. This works for Web Forms and Windows Forms applications, though it will still automatically trigger the application to restart for Web Forms applications. This means that you cannot rely on maintenance of in-process session state in ASP.NET applications when you change the configuration file. "
You can detect changes to the configuration by registering for the SourceChanged event that is defined in the IConfigurationSource interface and implemented by all Enterprise Library configuration sources. You can register and unregister for this event using the standard event handling approach, as shown in the following example that detects changes to configuration stored in a custom file named MyConfig.config.
来源:https://stackoverflow.com/questions/26870330/change-enterprise-library-configuration-midway-in-a-program