在程序中途更改企业库配置 [英] Change Enterprise Library configuration midway in a program

查看:65
本文介绍了在程序中途更改企业库配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想登录到一组特定的文件/文件夹一段时间,然后切换并开始记录到另一组文件。为此,我使用fluent API根据需要设置文件名(此处未显示)。但是我无法在程序中途更改配置。它没有按我预期的方式工作。如果第二次设置配置,是否有任何命令需要重新加载或清除配置?

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?

如果我注释掉 FirstConfig(); 和下一个 Log 语句,然后是 SecondConfig()工作良好。否则,只有 FirstConfig()似乎起作用。

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);
}


推荐答案

最后一行是创建一个新容器并为其分配配置,您将创建两个不同的容器。

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/zh-cn/library/ff664363%28PandP.50%29.aspx

所有配置源类实现IConfigurationSource接口。此接口允许您的应用程序代码订阅配置更改的通知。有关更多信息,请参见在运行时更新配置设置。默认情况下,在企业库中,只有Logging Application Block注册才能接收配置更改的通知。

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.

对于Enterprise Library 5.0
http://msdn.microsoft.com/en-us/library/ff664640%28v=pandp.50%29.aspx

For Enterprise Library 5.0 http://msdn.microsoft.com/en-us/library/ff664640%28v=pandp.50%29.aspx

这是一个例外,它是Logging Application Block,它能够检测配置更改并重新加载配置,而无需重新启动应用程序。该方法适用于Web Forms和Windows Forms应用程序,尽管它仍然可以自动触发该应用程序以重新启动Web Forms应用程序。这意味着在更改配置文件时,您不能依赖于ASP.NET应用程序中进程内会话状态的维护。

"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. "

您可以通过注册在IConfigurationSource界面中定义并由所有企业库配置源实现的SourceChanged事件来检测配置更改。您可以使用标准事件处理方法为该事件注册和注销,如以下示例所示,该示例检测存储在名为MyConfig.config的自定义文件中的配置更改。

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.

这篇关于在程序中途更改企业库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆