markus / ConvertService / ServiceBase / Markus.Service.StationController / Controls / TraceDocument.xaml.cs @ c8183702
이력 | 보기 | 이력해설 | 다운로드 (2.5 KB)
1 | 1ae729e4 | taeseongkim | using System.Windows.Controls; |
---|---|---|---|
2 | using System.Windows; |
||
3 | using System.Windows.Media; |
||
4 | using System.Windows.Documents; |
||
5 | using System.Diagnostics; |
||
6 | using System.Windows.Controls.Primitives; |
||
7 | |||
8 | a34f58f6 | taeseongkim | namespace Markus.Service.StationController.Controls |
9 | 1ae729e4 | taeseongkim | { |
10 | public partial class TraceDocument : FlowDocument, ITraceTextSink |
||
11 | { |
||
12 | private delegate void AppendTextDelegate(string msg, string style); |
||
13 | |||
14 | private TraceListener _listener; |
||
15 | |||
16 | public TraceDocument() |
||
17 | { |
||
18 | AutoAttach = true; |
||
19 | InitializeComponent(); |
||
20 | } |
||
21 | |||
22 | public bool AutoAttach { get; set; } |
||
23 | |||
24 | public void Event(string msg, TraceEventType eventType) |
||
25 | { |
||
26 | Append(msg, eventType.ToString()); |
||
27 | } |
||
28 | |||
29 | public void Fail(string msg) |
||
30 | { |
||
31 | Append(msg, "Fail"); |
||
32 | } |
||
33 | |||
34 | private void Append(string msg, string style) |
||
35 | { |
||
36 | if (Dispatcher.CheckAccess()) |
||
37 | { |
||
38 | Debug.Assert(Blocks.LastBlock != null); |
||
39 | Debug.Assert(Blocks.LastBlock is Paragraph); |
||
40 | var run = new Run(msg); |
||
41 | |||
42 | run.Style = (Style)(Resources[style]); |
||
43 | if (run.Style == null) |
||
44 | run.Style = (Style)(Resources["Information"]); |
||
45 | |||
46 | ((Paragraph)Blocks.LastBlock).Inlines.Add(run); |
||
47 | |||
48 | ScrollParent(this); |
||
49 | } |
||
50 | else |
||
51 | { |
||
52 | Dispatcher.Invoke(new AppendTextDelegate(Append), msg, style); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | private static void ScrollParent(FrameworkContentElement element) |
||
57 | { |
||
58 | if (element != null) |
||
59 | { |
||
60 | if (element.Parent is TextBoxBase) |
||
61 | ((TextBoxBase)element.Parent).ScrollToEnd(); |
||
62 | |||
63 | else if (element.Parent is ScrollViewer) |
||
64 | ((ScrollViewer)element.Parent).ScrollToEnd(); |
||
65 | |||
66 | else |
||
67 | ScrollParent(element.Parent as FrameworkContentElement); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | private void Document_Loaded(object sender, RoutedEventArgs e) |
||
72 | { |
||
73 | if (AutoAttach && _listener == null) |
||
74 | { |
||
75 | _listener = new TraceTextSource(this); |
||
76 | Trace.Listeners.Add(_listener); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | private void Document_Unloaded(object sender, RoutedEventArgs e) |
||
81 | { |
||
82 | if (_listener != null) |
||
83 | { |
||
84 | Trace.Listeners.Remove(_listener); |
||
85 | _listener.Dispose(); |
||
86 | _listener = null; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | } |