【问题标题】:how can override the ap.config values programmatically for a C# wcf application如何以编程方式覆盖 C# wcf 应用程序的 ap.config 值
【发布时间】:2011-02-04 05:06:32
【问题描述】:

使用 C#、VS2010 和 .NET 4.0 开发 WCF Web 服务

我想以编程方式更改存储在我的 app.config 文件中的一些设置。

不想想要编辑或更新实际的 app.config 文件,我想做的是在应用程序运行之后覆盖一些属性。

我想要更改的是我的 WCF 客户端应用程序中的跟踪级别。 如果 app.config 文件的跟踪级别通常为无,为了客户端支持,我想让客户端将其更改为警告、关键、详细或任何适当的。

我不想更改 app.confg 文件,none 的跟踪级别对于正常操作是正确的。只有当客户提交支持任务并了解正在发生的事情时,才会让应用程序生成一个级别设置为某个级别的跟踪。

一旦应用程序启动,如何以编程方式更改这类事情。

我看过许多关于如何以编程方式从头开始设置端点的文章。但是,我还没有在 systems.diagnostics 部分看到这一点。 我看过有关如何使用 wcf 编辑器或手动修改 app.config 文件的文章。

没有看到的是如何保留 app.config 定义的内容,但如何在需要时以编程方式更改或覆盖 ap.config 文件中包含的设置。

在我的 app.config 文件中的以下内容中

<system.diagnostics>
    <sources>
        <source name="NewSource" switchValue="Error,ActivityTracing">
            <listeners>
                <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                    <filter type="" />
                </add>
            </listeners>
        </source>
        <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
            <listeners>
                <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                    <filter type="" />
                </add>
                <add name="ServiceModelMessageLoggingListener">
                    <filter type="" />
                </add>
            </listeners>
        </source>
        <source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
            propagateActivity="true">
            <listeners>
                <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                    <filter type="" />
                </add>
                <add name="ServiceModelTraceListener">
                    <filter type="" />
                </add>
            </listeners>
        </source>
    </sources>
    <sharedListeners>
        <add initializeData="d:\sandbox\wcf\helloworld\helloworldservice\helloclient\app_messages.svclog"
            type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            name="ServiceModelMessageLoggingListener" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId">
            <filter type="" />
        </add>
        <add initializeData="d:\sandbox\wcf\helloworld\helloworldservice\helloclient\app_tracelog.svclog"
            type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            name="ServiceModelTraceListener" traceOutputOptions="DateTime, Timestamp, ProcessId, ThreadId">
            <filter type="" />
        </add>
    </sharedListeners>
</system.diagnostics>

我希望用户可以选择将 switchvalue 属性调整为默认警告以外的其他属性。我可能想通过将其更改为 none 或通过将其更改为详细等来增加内容来禁用它 我可能还想更改上述 initializeData 属性值中写入跟踪的位置。

这可能吗?必须有很多时候,当 app.config 中的默认值通常没问题时,需要以编程方式覆盖各种元素的属性,但在某些情况下,需要以编程方式覆盖这些值,而不会对 app.config 进行持久更改。

非常感谢任何帮助。

【问题讨论】:

    标签: c# .net windows wcf


    【解决方案1】:

    你帖子的最后一句话回答了这个问题: “当 app.config 中的默认值通常没问题时,必须有很多时候需要以编程方式覆盖各种元素的属性,但在某些情况下,需要以编程方式覆盖这些值而不进行持久更改到 app.config。”

    您需要命令式编程:) 换句话说,编写一个实用程序类来帮助您进行检查。

    试试这个课程:SourceSwitch

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2015-05-18
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多