【问题标题】:Change Enterprise Library configuration midway in a program在程序中途更改企业库配置
【发布时间】:2014-11-11 17:03:11
【问题描述】:

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

如果我注释掉FirstConfig(); 和下一个Log 语句,那么SecondConfig() 工作正常。否则,似乎只有FirstConfig() 有效果。

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

【问题讨论】:

    标签: c# enterprise-library enterprise-library-5


    【解决方案1】:

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

    我认为企业库允许检测现有的日志记录配置更改而无需重新启动应用程序。您可能只想修改现有配置,然后处理更改。

    http://msdn.microsoft.com/en-us/library/ff664363%28PandP.50%29.aspx

    所有配置源类都实现了 IConfigurationSource 接口。此接口允许您的应用程序代码订阅配置更改通知。有关详细信息,请参阅Updating Configuration Settings at Run Time。默认情况下,在 Enterprise Library 中,只有 Logging Application Block 注册以接收配置更改通知。

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

    “一个例外是记录应用程序块,它能够检测配置更改并重新加载配置而无需重新启动应用程序。这适用于 Web 窗体和 Windows 窗体应用程序,但它仍会自动触发应用程序重新启动 Web 窗体应用程序。这意味着当您更改配置文件时,您不能依赖维护 ASP.NET 应用程序中的进程内会话状态。"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      相关资源
      最近更新 更多