1 | // -----------------------------------------------------------------------
|
---|
2 | // <copyright file="RecentEventBufferTraceListener.cs" company="Microsoft">
|
---|
3 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
---|
4 | // </copyright>
|
---|
5 | // -----------------------------------------------------------------------
|
---|
6 |
|
---|
7 | namespace Microsoft.Samples.Kinect.WebserverBasics
|
---|
8 | {
|
---|
9 | using System;
|
---|
10 | using System.Collections.Generic;
|
---|
11 | using System.Diagnostics;
|
---|
12 | using System.Text;
|
---|
13 |
|
---|
14 | /// <summary>
|
---|
15 | /// Remembers the most recent trace events observed and exposes them as
|
---|
16 | /// a chunk of displayable text.
|
---|
17 | /// </summary>
|
---|
18 | public class RecentEventBufferTraceListener : TraceListener
|
---|
19 | {
|
---|
20 | private const int DefaultMaximumLines = 10;
|
---|
21 |
|
---|
22 | private readonly StringBuilder traceLineBuilder = new StringBuilder();
|
---|
23 |
|
---|
24 | private readonly Queue<string> entries;
|
---|
25 |
|
---|
26 | private readonly int maximumLines;
|
---|
27 |
|
---|
28 | /// <summary>
|
---|
29 | /// Initializes a new instance of the <see cref="RecentEventBufferTraceListener"/> class.
|
---|
30 | /// </summary>
|
---|
31 | /// <remarks>
|
---|
32 | /// Remembers the default number of trace lines.
|
---|
33 | /// </remarks>
|
---|
34 | public RecentEventBufferTraceListener()
|
---|
35 | : this(DefaultMaximumLines)
|
---|
36 | {
|
---|
37 | }
|
---|
38 |
|
---|
39 | /// <summary>
|
---|
40 | /// Initializes a new instance of the <see cref="RecentEventBufferTraceListener"/> class.
|
---|
41 | /// </summary>
|
---|
42 | /// <param name="maximumLines">
|
---|
43 | /// Maximum number of trace lines to remember.
|
---|
44 | /// </param>
|
---|
45 | public RecentEventBufferTraceListener(int maximumLines)
|
---|
46 | {
|
---|
47 | this.maximumLines = maximumLines;
|
---|
48 | this.entries = new Queue<string>(maximumLines);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public event EventHandler<EventArgs> RecentEventBufferChanged;
|
---|
52 |
|
---|
53 | /// <summary>
|
---|
54 | /// Displayable string representing buffer of recent trace events seen.
|
---|
55 | /// </summary>
|
---|
56 | public string RecentEventBuffer { get; private set; }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// Remembers the specified message as the latest message seen.
|
---|
60 | /// </summary>
|
---|
61 | /// <param name="message">
|
---|
62 | /// Message to remember.
|
---|
63 | /// </param>
|
---|
64 | public override void Write(string message)
|
---|
65 | {
|
---|
66 | this.traceLineBuilder.Append(message);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /// <summary>
|
---|
70 | /// Remembers the specified message, followed by a line terminator, as the latest
|
---|
71 | /// message seen.
|
---|
72 | /// </summary>
|
---|
73 | /// <param name="message">
|
---|
74 | /// Message to remember.
|
---|
75 | /// </param>
|
---|
76 | public override void WriteLine(string message)
|
---|
77 | {
|
---|
78 | this.traceLineBuilder.Append(message);
|
---|
79 | this.traceLineBuilder.Append("\n");
|
---|
80 | this.QueueMessage(this.traceLineBuilder.ToString());
|
---|
81 | this.traceLineBuilder.Clear();
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Remembers the specified message as the latest message seen.
|
---|
86 | /// </summary>
|
---|
87 | /// <param name="message">
|
---|
88 | /// Message to remember.
|
---|
89 | /// </param>
|
---|
90 | private void QueueMessage(string message)
|
---|
91 | {
|
---|
92 | this.entries.Enqueue(message);
|
---|
93 | if (this.entries.Count > this.maximumLines)
|
---|
94 | {
|
---|
95 | this.entries.Dequeue();
|
---|
96 | }
|
---|
97 |
|
---|
98 | var builder = new StringBuilder();
|
---|
99 | foreach (var entry in this.entries)
|
---|
100 | {
|
---|
101 | builder.Append(entry);
|
---|
102 | }
|
---|
103 |
|
---|
104 | this.RecentEventBuffer = builder.ToString();
|
---|
105 |
|
---|
106 | if (this.RecentEventBufferChanged != null)
|
---|
107 | {
|
---|
108 | this.RecentEventBufferChanged(this, EventArgs.Empty);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|