annotate Messaging/Common/Xaml/BindingErrorTraceListener.cs @ 31:7d9de5746f18

Working version
author adminsh@apollo
date Thu, 22 Mar 2012 08:09:41 +0000
parents 96fdf58e05b4
children
rev   line source
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
1 // http://www.switchonthecode.com/tutorials/wpf-snippet-detecting-binding-errors
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
2
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
3 using System.Diagnostics;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
4 using System.Text;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
5 using System.Windows;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
6
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
7 namespace Common.Xaml
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
8 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
9 public class BindingErrorTraceListener : DefaultTraceListener
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
10 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
11 private static BindingErrorTraceListener _listener;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
12
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
13
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
14 private readonly StringBuilder _message = new StringBuilder();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
15
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
16 private BindingErrorTraceListener()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
17 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
18 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
19
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
20 public override void Write(string message)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
21 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
22 _message.Append(message);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
23 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
24
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
25 public override void WriteLine(string message)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
26 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
27 _message.Append(message);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
28
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
29 var final = _message.ToString();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
30 _message.Length = 0;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
31
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
32 MessageBox.Show(final,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
33 "Binding Error",
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
34 MessageBoxButton.OK,
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
35 MessageBoxImage.Error);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
36 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
37
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
38 public static void CloseTrace()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
39 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
40 if (_listener == null)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
41 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
42 return;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
43 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
44
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
45 _listener.Flush();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
46 _listener.Close();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
47 PresentationTraceSources.DataBindingSource.Listeners.Remove(_listener);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
48 _listener = null;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
49 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
50
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
51 [Conditional("DEBUG")]
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
52 public static void SetTrace()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
53 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
54 SetTrace(SourceLevels.Error, TraceOptions.None);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
55 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
56
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
57 public static void SetTrace(SourceLevels level, TraceOptions options)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
58 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
59 if (_listener == null)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
60 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
61 _listener = new BindingErrorTraceListener();
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
62 PresentationTraceSources.DataBindingSource.Listeners.Add(_listener);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
63 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
64
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
65 _listener.TraceOutputOptions = options;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
66 PresentationTraceSources.DataBindingSource.Switch.Level = level;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
67 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
68 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
69 }