I have created a Custom Trace Listener inheriting from TraceListner class. Actually the idea is to use the Trace.Write method in the application which should update an Observable collection in a custom trace listener class which can be bound to a framework element.
我创建了一个继承自TraceListner类的自定义跟踪侦听器。实际上,我们的想法是在应用程序中使用Trace.Write方法,该方法应该更新可以绑定到框架元素的自定义跟踪侦听器类中的Observable集合。
Here is my Custom Trace Listener :
这是我的自定义跟踪侦听器:
public class CustomTraceListener : TraceListener, INotifyPropertyChanged
{
private ObservableCollection<TraceMessageInfo> _traceLogs;
public ObservableCollection<TraceMessageInfo> TraceLogs
{
get
{
return _traceLogs;
}
set
{
_traceLogs = value;
OnPropertyChanged("TraceLogs");
}
}
public CustomTraceListener()
{
if (TraceLogs == null)
{
TraceLogs = new ObservableCollection<TraceMessageInfo>();
TraceLogs.Add(new TraceMessageInfo("Message", Enums.TraceCategory.Information.ToString()));
}
}
public override void Write(object traceMessageInfo)
{
TraceLogs.Add(new TraceMessageInfo("New message", Enums.TraceCategory.Information.ToString()));
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Here is the xaml i am using to bind the observable collection to a ListBox (WPF).
这是我用来将可观察集合绑定到ListBox(WPF)的xaml。
<StackPanel Grid.Row="1" Name="TraceLogStackPanel">
<ListBox Name="lbTraceViewer" ItemsSource="{Binding TraceLogs, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="700" Background="{Binding Category,Converter={StaticResource TraceCategoryToColorConverter}, Mode=TwoWay}">
<Label Content="{Binding Message, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
In the constructor i am setting the data context of the Stack Panel to this new Trace Listener.
在构造函数中,我将Stack Panel的数据上下文设置为这个新的Trace Listener。
this.TraceLogStackPanel.DataContext = new CustomTraceListener();
And finally on the click of execute button i am setting a dummy message to the write method of my custom trace listener.
最后点击执行按钮我正在为我的自定义跟踪监听器的write方法设置一个虚拟消息。
private void Button_Click(object sender, RoutedEventArgs e)
{
Trace.Write(new TraceMessageInfo("Message",Enums.TraceCategory.Error.ToString()));
}
In the configuration file i have set the custom trace listener and removed the default one.
在配置文件中,我设置了自定义跟踪侦听器并删除了默认侦听器。
My problem is that when i click on the execute there is no change in the collection. Sometimes when i try to debug it shows me the collection as null and sometimes it shows me the collection changed but no notifications to the UI. The listbox is showing only one message which i have set in the constructor of custom listener.
我的问题是,当我点击执行时,集合中没有任何变化。有时当我尝试调试时,它会将集合显示为null,有时它会显示集合已更改但未向UI发送通知。列表框只显示我在自定义侦听器的构造函数中设置的一条消息。
Any help will be appreciated.
任何帮助将不胜感激。
1 个解决方案
#1
1
Well, since you're creating an instance of the CustomTraceListener
, your view is bound to a different instance than the .NET tracing system (it will create its own instance). I would change the collection to be a static instance, so you will see all messages:
好吧,既然您正在创建CustomTraceListener的实例,那么您的视图将绑定到与.NET跟踪系统不同的实例(它将创建自己的实例)。我会将集合更改为静态实例,因此您将看到所有消息:
private static readonly ObservableCollection<TraceMessageInfo> _traceLogs;
public ObservableCollection<TraceMessageInfo> TraceLogs
{
get
{
return _traceLogs;
}
}
static CustomTraceListener()
{
_traceLogs = new ObservableCollection<TraceMessageInfo>();
_traceLogs.Add(new TraceMessageInfo("Message", Enums.TraceCategory.Information.ToString()));
}
#1
1
Well, since you're creating an instance of the CustomTraceListener
, your view is bound to a different instance than the .NET tracing system (it will create its own instance). I would change the collection to be a static instance, so you will see all messages:
好吧,既然您正在创建CustomTraceListener的实例,那么您的视图将绑定到与.NET跟踪系统不同的实例(它将创建自己的实例)。我会将集合更改为静态实例,因此您将看到所有消息:
private static readonly ObservableCollection<TraceMessageInfo> _traceLogs;
public ObservableCollection<TraceMessageInfo> TraceLogs
{
get
{
return _traceLogs;
}
}
static CustomTraceListener()
{
_traceLogs = new ObservableCollection<TraceMessageInfo>();
_traceLogs.Add(new TraceMessageInfo("Message", Enums.TraceCategory.Information.ToString()));
}