C# 如何找到在app.config中声明的跟踪侦听器(在代码中)?
我有一个自定义跟踪侦听器,它记录到我试图在ViewModelLocator中找到的字符串(我将绑定到wpf文本框),它(或我定义的所有其他侦听器)似乎不在System.Diagnostics.trace.listeners中 App.config代码段C# 如何找到在app.config中声明的跟踪侦听器(在代码中)?,c#,trace,C#,Trace,我有一个自定义跟踪侦听器,它记录到我试图在ViewModelLocator中找到的字符串(我将绑定到wpf文本框),它(或我定义的所有其他侦听器)似乎不在System.Diagnostics.trace.listeners中 App.config代码段 <?xml version="1.0" encoding="utf-8" ?> <configuration> <system.diagnostics> <switches>
<?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>
如果在Trace.Listeners集合中找不到它,则可以假定从未添加该侦听器。两个基本原因:
- 您可能正在启用Visual Studio宿主进程选项的情况下进行调试。它使用不同的配置文件app.vshost.exe.config。项目+属性,调试选项卡将其关闭
- .config文件中的条目可能格式不正确。从VisualStudio输出窗口可以看出,您将看到“首次机会异常”通知。“调试+异常”,单击“抛出”复选框以在引发异常时强制调试器停止。您可以从堆栈跟踪中收集信息。此异常不会阻止你的应用程序运行。我们无法猜测“type”值是否准确
TraceSource source = new TraceSource("RomanExampleWPFApp");
source.TraceInformation("hellow world");
TraceSource source = new TraceSource("RomanExampleWPFApp");
foreach (var listener in source.Listeners)
{
...
}
如果你想得到一个监听器列表,你可以这样做:
TraceSource source = new TraceSource("RomanExampleWPFApp");
source.TraceInformation("hellow world");
TraceSource source = new TraceSource("RomanExampleWPFApp");
foreach (var listener in source.Listeners)
{
...
}
它们从未被实例化,因此不要期望在任何TraceSource集合中找到它们。 如果您不介意对diagnostics配置进行一些反思,您可以解释以下代码输出:
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
//}
}
类程序{
静态无效转储(ConfigurationElementCollection集合){
foreach(集合中的ConfigurationElement elm){
Console.WriteLine();
Console.WriteLine(elm.ToString());
Console.WriteLine();
foreach(elm.ElementInformation.Properties中的PropertyInformation属性){
Console.Write(道具名称+“:”);
如果(属性值==null){
控制台。写入线(“空”);
}否则{
ConfigurationElementCollection子项=作为ConfigurationElementCollection的属性值;
如果(子项!=null){
Console.WriteLine(“儿童”);
Console.WriteLine();
}否则{
控制台写入线(属性值);
}
}
}
}
}
静态void Main(字符串[]参数){
类型tp=typeof(Debug.Assembly.GetType(“System.Diagnostics.DiagnosticsConfiguration”);
PropertyInfo-propSources=tp.GetProperty(“源”,BindingFlags.NonPublic | BindingFlags.Static);
ConfigurationElementCollection源=(ConfigurationElementCollection)propSources.GetValue(null,null);
垃圾场(来源);
//for(int i=0;i
基本上每个源元素都有一个名为“侦听器”的属性它是侦听器元素的集合。静态属性System.Diagnostics.Trace.Listeners
包含在System.Diagnostics.Trace
上使用静态方法时通知的侦听器集合。这与与TraceSo实例关联的侦听器集合不同urce
。