简体   繁体   English

如何找到我在app.config中声明的跟踪侦听器(在代码中)?

[英]How do I find a trace listener (in code) that I declare in app.config?

I have custom trace listener that logs to a string(which I'll bind to a wpf textbox) which I I'm trying to find in my ViewModelLocator, it (or all of the other listeners I defined) don't seem to be in System.Diagnostics.Trace.Listeners) 我有自定义跟踪侦听器,它记录到一个字符串(我将绑定到一个wpf文本框),我试图在我的ViewModelLocator中找到它,它(或我定义的所有其他侦听器)似乎不是在System.Diagnostics.Trace.Listeners)

App.config snippet App.config片段

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <switches>
      <add name="RomanExampleWPFAppSwitch" value="Verbose" />
    </switches>
    <sources>
      <source name="RomanExampleWPFApp" switchName="RomanExampleWPFAppSwitch">
        <listeners>
          <remove name="Default" />
          <add name="RollingLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" initializeData="RollingLogWriter" append="true" autoFlush="true" baseFileName="RomanExampleWPFAppLog" location="LocalUserApplicationDirectory" logFileCreationSchedule="Daily" reserveDiskSpace="1073741824" traceOutputOptions="DateTime,LogicalOperationStack" />
          <add name="StringLog" type="RomanExampleWPFApp.Other.StringLogTraceListener, RomanExampleWPFApp" />
          <add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
</configuration>

If you can't find it back in the Trace.Listeners collection then you can assume that the listener never got added. 如果您无法在Trace.Listeners集合中找到它,那么您可以假设从未添加过侦听器。 Two basic reasons: 两个基本原因:

  • You might be debugging with the Visual Studio Hosting Process option enabled. 您可能正在调试启用Visual Studio Hosting Process选项。 Which uses a different config file, app.vshost.exe.config. 其中使用了不同的配置文件app.vshost.exe.config。 Project + Properties, Debug tab to turn it off. 项目+属性,调试选项卡将其关闭。

  • The entry in the .config file might be malformed. .config文件中的条目可能格式错误。 You can tell from the Visual Studio Output window, you'll see a "first chance exception" notification. 您可以从Visual Studio输出窗口中看到,您将看到“第一次机会异常”通知。 Debug + Exceptions, click the Thrown checkbox to force the debugger to stop when the exception is raised. Debug + Exceptions,单击Thrown复选框以强制调试器在引发异常时停止。 You can glean info from the stack trace. 您可以从堆栈跟踪中收集信息。 This exception doesn't otherwise prevent your app from running. 此异常不会阻止您的应用运行。 We can't guess if the "type" value is accurate. 我们无法猜测“类型”值是否准确。

You are defining a specific TraceSource . 您正在定义特定的TraceSource If you want to trace something in this case, this is how you would do like this: 如果你想在这种情况下追踪某些东西,你可以这样做:

TraceSource source = new TraceSource("RomanExampleWPFApp");
source.TraceInformation("hellow world");

And if you want to get a list of listeners, then you can do it like this: 如果你想获得一个监听器列表,那么你可以这样做:

TraceSource source = new TraceSource("RomanExampleWPFApp");
foreach (var listener in source.Listeners)
{
   ...
}

They never get instantiated, so don't expect to find them in any TraceSource collection. 它们永远不会被实例化,所以不要指望在任何TraceSource集合中找到它们。 If you don't mind a little reflection on diagnostics configuration, you can interpet the code output of this : 如果您不介意对诊断配置进行一点反思,可以插入以下代码输出:

class Program {
    static void Dump( ConfigurationElementCollection collection ) {
        foreach ( ConfigurationElement elm in collection ) {
            Console.WriteLine();
            Console.WriteLine(elm.ToString());
            Console.WriteLine();
            foreach ( PropertyInformation prop in elm.ElementInformation.Properties ) {
                Console.Write( prop.Name + ": " );
                if ( prop.Value == null ) {
                    Console.WriteLine( "null" );
                } else {
                    ConfigurationElementCollection children = prop.Value as ConfigurationElementCollection;
                    if ( children != null ) {
                        Console.WriteLine( "children<" );
                        Console.WriteLine();
                        Dump( children );
                        Console.WriteLine( ">children" );
                        Console.WriteLine( );
                    } else {
                        Console.WriteLine( prop.Value );
                    }
                }
            }
        }
    }
    static void Main( string[] args ) {
        Type tp = typeof( Debug ).Assembly.GetType( "System.Diagnostics.DiagnosticsConfiguration" );
        PropertyInfo propSources = tp.GetProperty( "Sources", BindingFlags.NonPublic | BindingFlags.Static );
        ConfigurationElementCollection sources = (ConfigurationElementCollection)propSources.GetValue( null, null );
        Dump( sources );
        //for ( int i = 0; i < sources.Count; i++ ) {
        //  sources.g
        //}
    }

Basically each Source element has a property named "listeners" which is a collection of Listeners elements. 基本上每个Source元素都有一个名为“listeners”的属性,它是Listeners元素的集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM