c# - 从 xml 文件加载 System.Diagnostics.TraceSource 的配置

标签 c# logging tracesource

在 log4net 中,可以选择从 app.config 或从任意 xml 文件加载配置。

是否可以从任意 xml 文件加载 System.Diagnostics.TraceSource 的配置?

最佳答案

System.Diagnostics 类只查看应用程序配置文件。例如。备注部分SourceSwitch说:

To configure a SourceSwitch, edit the configuration file that corresponds to the name of your application.

如果您查看代码,您会发现这些类在内部使用静态 DiagnosticConfiguration 类,它只是从当前 app.config< 中获取 system.diagonostics 配置部分

但是您可以将system.diagonostics 配置部分移动到单独的xml 文件中。只需指定定义部分的文件名:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics configSource="diagnostics.xml"/>
</configuration>

诊断.xml

<system.diagnostics>
  <sources>
    <source name="foo" switchName="bar"
            switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <add name="console"/>
      </listeners>
    </source>
  </sources>
  <switches>
    <add name="bar" value="Warning"/>
  </switches>
  <sharedListeners>
    <add name="console" 
         type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
  </sharedListeners>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <add name="console"/>
    </listeners>
  </trace>
</system.diagnostics>

关于c# - 从 xml 文件加载 System.Diagnostics.TraceSource 的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781510/

相关文章:

C# 无效的对象名称 ASP.NET

c# - 在后面的代码中将类添加到转发器内部的 anchor 标记

file - 如何将所有控制台输出保存到 R 中的文件中?

.net - 我如何告诉 -all- .NET Trace Sources 使用监听器?

c# - TraceSource 锁定文件

c# - Linq to SQL 扩展方法 : Method overloading, 错误

c# - 如何在 LDAP 服务器上查找用户

logging - kube-controller-manager 未出现在 Google Kubernetes Engine 上

python - 为特定请求/URL/端点禁用登录 gunicorn

.net - 在多线程环境中使用TraceSource