|
6 | 6 | * GitHub: https://github.com/ivanpointer/NuLog |
7 | 7 | */ |
8 | 8 |
|
| 9 | +using NuLog.Configuration; |
| 10 | +using NuLog.Configuration.Extenders; |
| 11 | +using NuLog.Dispatch; |
9 | 12 | using System; |
10 | | -using System.Collections.Generic; |
11 | | -using System.Linq; |
| 13 | +using System.Diagnostics; |
12 | 14 | using System.Text; |
13 | | -using System.Threading.Tasks; |
14 | 15 |
|
15 | 16 | namespace NuLog.Extenders |
16 | 17 | { |
| 18 | + /// <summary> |
| 19 | + /// An extender that listens to trace/debug messages and sends them through |
| 20 | + /// the framework as log events. |
| 21 | + /// </summary> |
17 | 22 | public class TraceListenerExtender : ExtenderBase |
18 | 23 | { |
19 | | - public override void Startup(Dispatch.LogEventDispatcher dispatcher) |
| 24 | + /// <summary> |
| 25 | + /// This is an internal class that extends the basic TraceListener |
| 26 | + /// This is what will actually do the listening for trace |
| 27 | + /// </summary> |
| 28 | + internal class InternalTraceListener : TraceListener |
20 | 29 | { |
21 | | - throw new NotImplementedException(); |
| 30 | + #region Constants/Members/Messages |
| 31 | + |
| 32 | + // Our constants, including a write lock to help keep our writing clean |
| 33 | + private static readonly object WriteLock = new object(); |
| 34 | + private static readonly char[] Split = Environment.NewLine.ToCharArray(); |
| 35 | + |
| 36 | + // Our members, including a logger |
| 37 | + private static readonly LoggerBase _logger = LoggerFactory.GetLogger(); |
| 38 | + private string[] _tags; |
| 39 | + private StringBuilder _stringBuilder; |
| 40 | + |
| 41 | + #endregion |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The only constructor. Configures this trace listener based on |
| 45 | + /// the configuration in the provided TraceListenerExtender |
| 46 | + /// </summary> |
| 47 | + /// <param name="extender">The TraceListenerExtender that this TraceListener belongs to, and from which to load the configuration</param> |
| 48 | + public InternalTraceListener(TraceListenerExtender extender) |
| 49 | + : base() |
| 50 | + { |
| 51 | + // Prepare the String Builder |
| 52 | + _stringBuilder = new StringBuilder(); |
| 53 | + |
| 54 | + // We want to get the tags from the config |
| 55 | + if (extender != null && extender.TraceListenerConfig != null) |
| 56 | + _tags = extender.TraceListenerConfig.TraceTags; |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Writes a single message. If it has no newline characters, the contents are stored |
| 61 | + /// in a string buffer until a WriteLine is called, or a Write with a message that |
| 62 | + /// contains newlines, at which point, the message will be flushed to log |
| 63 | + /// </summary> |
| 64 | + /// <param name="message">The message to flush to log</param> |
| 65 | + public override void Write(string message) |
| 66 | + { |
| 67 | + // Make sure that we have a message |
| 68 | + if (String.IsNullOrEmpty(message) == false) |
| 69 | + lock (WriteLock) |
| 70 | + { |
| 71 | + // If the message has no newline, just queue it up |
| 72 | + if (!message.Contains(Environment.NewLine)) |
| 73 | + { |
| 74 | + _stringBuilder.Append(message); |
| 75 | + } |
| 76 | + // The message contains at least one newline, split it up |
| 77 | + else |
| 78 | + { |
| 79 | + // Setup |
| 80 | + bool endsNewline = message.EndsWith(Environment.NewLine); |
| 81 | + var parts = message.Split(Split); |
| 82 | + |
| 83 | + // Append, and write the first part |
| 84 | + // unless it is the only part and we have no newline at the end |
| 85 | + _stringBuilder.Append(parts[0]); |
| 86 | + if (parts.Length > 1 || endsNewline) |
| 87 | + { |
| 88 | + WriteLine(_stringBuilder.ToString()); |
| 89 | + _stringBuilder.Clear(); |
| 90 | + } |
| 91 | + |
| 92 | + // Write out each of the child parts if there are more, queuing the last bit if |
| 93 | + // it isn't followed by a newline |
| 94 | + if (parts.Length > 1) |
| 95 | + { |
| 96 | + for (int index = 1; index < (endsNewline ? parts.Length : parts.Length - 1); index++) |
| 97 | + WriteLine(parts[index]); |
| 98 | + |
| 99 | + if (endsNewline == false) |
| 100 | + _stringBuilder.Append(parts[parts.Length - 1]); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Flushes the buffer and writes the message to log |
| 108 | + /// </summary> |
| 109 | + /// <param name="message">The message to write to log</param> |
| 110 | + public override void WriteLine(string message) |
| 111 | + { |
| 112 | + // Ignore this request if the message is empty |
| 113 | + if (String.IsNullOrEmpty(message) == false) |
| 114 | + lock (WriteLock) |
| 115 | + { |
| 116 | + // Write the contents of the buffer out, followed by the message |
| 117 | + _logger.Log(new LogEvent |
| 118 | + { |
| 119 | + Message = String.Format("{0}{1}", _stringBuilder.ToString(), message), |
| 120 | + Tags = _tags, |
| 121 | + // Help prevent feedback loops: |
| 122 | + Silent = true |
| 123 | + }); |
| 124 | + _stringBuilder.Clear(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Flushes the contents of the buffer out to log |
| 130 | + /// </summary> |
| 131 | + public override void Flush() |
| 132 | + { |
| 133 | + lock (WriteLock) |
| 134 | + { |
| 135 | + // Write out the contents of the buffer |
| 136 | + WriteLine(_stringBuilder.ToString()); |
| 137 | + _stringBuilder.Clear(); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // This TraceListenerExtender's TraceListener |
| 143 | + private TraceListener _traceListener; |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// The configuration for this TraceListenerExtender |
| 147 | + /// </summary> |
| 148 | + public TraceListenerConfig TraceListenerConfig { get; set; } |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Initializes this extender with the provided configuration |
| 152 | + /// </summary> |
| 153 | + /// <param name="extenderConfig">The configuration for this specific extender</param> |
| 154 | + /// <param name="loggingConfig">The whole configuration for the framework</param> |
| 155 | + public override void Initialize(ExtenderConfig extenderConfig, LoggingConfig loggingConfig) |
| 156 | + { |
| 157 | + // Let the base configure itself out |
| 158 | + base.Initialize(extenderConfig, loggingConfig); |
| 159 | + |
| 160 | + // Configure this extender, using the TraceListenerConfig |
| 161 | + if (extenderConfig != null) |
| 162 | + { |
| 163 | + TraceListenerConfig = extenderConfig is TraceListenerConfig |
| 164 | + ? (TraceListenerConfig)extenderConfig |
| 165 | + : new TraceListenerConfig(extenderConfig.Config); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /// <summary> |
| 170 | + /// Starts this trace listener extender. Starts a trace listener that will |
| 171 | + /// route trace messages through the framework as log events |
| 172 | + /// </summary> |
| 173 | + /// <param name="dispatcher">The dispatcher that has been initialized for the framework. This TraceListenerExtender doesn't use the dispatcher.</param> |
| 174 | + public override void Startup(LogEventDispatcher dispatcher) |
| 175 | + { |
| 176 | + _traceListener = new InternalTraceListener(this); |
| 177 | + Trace.Listeners.Add(_traceListener); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Shuts down this instance. Will flush the buffer of the trace listener and remove it from the trace listeners list. |
| 182 | + /// </summary> |
| 183 | + /// <param name="timeout">The amount of time for this to shutdown. Ignored.</param> |
| 184 | + /// <param name="stopwatch">A stopwatch. Ignored.</param> |
| 185 | + /// <returns>A boolean indicating whether or not this shutdown successfully. This always returns true.</returns> |
| 186 | + public override bool Shutdown(int timeout = DefaultShutdownTimeout, Stopwatch stopwatch = null) |
| 187 | + { |
| 188 | + // If we have a configured trace listener, flush it and remove it from the list |
| 189 | + // of listeners. |
| 190 | + if (_traceListener != null) |
| 191 | + { |
| 192 | + _traceListener.Flush(); |
| 193 | + _traceListener.Close(); |
| 194 | + _traceListener.Dispose(); |
| 195 | + Trace.Listeners.Remove(_traceListener); |
| 196 | + _traceListener = null; |
| 197 | + } |
| 198 | + return base.Shutdown(); |
22 | 199 | } |
| 200 | + |
23 | 201 | } |
24 | 202 | } |
0 commit comments