【问题标题】:Custom / Nested XML settings in ServiceConfiguration.cscfgServiceConfiguration.cscfg 中的自定义/嵌套 XML 设置
【发布时间】:2011-05-04 06:12:44
【问题描述】:

我们正在尝试在我们的 Azure 应用程序中实现一些可动态配置的日志记录,并且我们正在使用企业库来执行此操作,效果很好,但是实现这一点所需的 xml 比简单的“appSetting”样式设置更复杂ServiceConfiguration.cscfg 文件似乎可以接受,因为它需要嵌套的 xml 节点,

例如

<configSections>
    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>

通过以下方式解决(请原谅此窗口中的格式):

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
        <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            type="OurSolution.Common.AzureDiagnosticTraceListener, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
            name="AzureDiagnosticTraceListener" />
    </listeners>
    <formatters>
        <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
            name="Text Formatter" />
    </formatters>
    <categorySources>
        <add switchValue="All" name="General">
            <listeners>
                <add name="AzureDiagnosticTraceListener" />
            </listeners>
        </add>
    </categorySources>
</loggingConfiguration>

我看不出有什么方法可以欺骗 ServiceDefinition 或 ServiceConfiguration 文件来接受这一点,尽管如果可以的话,我可以看到一种方法告诉企业库使用我可以在 app.config 中执行的 ServiceConfiguration 文件。

为什么我们试图解决这个问题是为了允许我们动态调整日志记录的设置,即从无日志记录更改为详细而无需重新部署,这在我们的实时应用程序中证明是耗时且不切实际的只是最近才上线,所以可能仍然存在奇怪的错误;-)

任何想法都将不胜感激 问候 Kindo马来语

【问题讨论】:

    标签: .net logging azure enterprise-library


    【解决方案1】:

    其他选项:

    • Base64 编码用于任意配置并将它们放入 cscfg 的值中
    • (我的最爱)只需在 blob 存储或数据库中保留额外的配置(让管理部署的工作变得更简单)

    【讨论】:

    • 我喜欢将设置放入 blob 存储的想法,但您会失去检测配置更改的内置功能。这并不是说你不能自己想出办法来做到这一点。
    • 基本上,您只能在启动时从 BLOB 加载设置,并且有一个几乎一直处于休眠状态的侧线程。几分钟后,它会检查 BLOB 中的变化,如果检测到,只需要求 Management API 重新启动 worker。
    • 我们最终采取了不同的方式,但非常感谢您提供的信息
    【解决方案2】:

    目前,ServiceConfiguration 文件仅限于基本的&lt;Setting name="foo" value="bar" /&gt; 结构。

    但该值只是一个字符串值。当需要更具表现力的东西时,我所做的是将 XML 作为字符串放入设置的值中,然后在我读出它时将其反序列化为 XML。当您直接编辑它时,这确实会使配置文件看起来很丑,因为它会编码您设置中的所有 XML,但它确实有效。

    如果我们只查看您的侦听器部分以保持示例较小:

    <listeners>
        <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        type="OurSolution.Common.AzureDiagnosticTraceListener, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="AzureDiagnosticTraceListener" />
    </listeners>
    

    这可以放在 ServiceConfiguration 文件中,如下所示:

    <Setting name="Logging" value="&lt;listeners&gt;&lt;add listenerDataType=&quot;Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot; type=&quot;OurSolution.Common.AzureDiagnosticTraceListener, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot; name=&quot;AzureDiagnosticTraceListener&quot; /&gt;&lt;/listeners&gt;" />
    

    执行此操作的最简单方法是获取源 XML,替换所有选项卡和回车,并通过云项目属性设置部分更新设置。

    部署后对其进行更改并非完全直截了当,而是可行的。

    【讨论】:

    • 非常感谢,我试试看,你知道这个“字符串”的长度是否有限制吗?
    • 不,我不知道。我只使用了比您看到的要小得多的设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多