Last active
November 13, 2017 17:51
-
-
Save LSTANCZYK/a9e1035ac969f865d0d0359e21f6c10c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
// ReSharper disable once CheckNamespace | |
namespace System.Diagnostics | |
{ | |
/// <summary> A color console trace listener. </summary> | |
/// <seealso cref="T:System.Diagnostics.ConsoleTraceListener"/> | |
/// <remarks> | |
/// | |
/// | |
/// in your config file add the following: | |
/// <system.diagnostics> | |
/// <sources> | |
/// <source name = "log" switchValue="All"> | |
/// <listeners> | |
/// <add name = "Console" type="FlimFlan.Diagnostics.ColorConsoleTraceListener, ConsoleApplication1" /> | |
/// </listeners> | |
/// </source> | |
/// </sources> | |
/// </system.diagnostics> | |
/// | |
/// | |
/// </remarks> | |
public class ColorConsoleTraceListener : ConsoleTraceListener | |
{ | |
/// <summary> The event color. </summary> | |
private readonly Dictionary<TraceEventType, ConsoleColor> _eventColor = new Dictionary<TraceEventType, ConsoleColor>(); | |
/// <summary> Initializes a new instance of the System.Diagnostics.ColorConsoleTraceListener class. </summary> | |
public ColorConsoleTraceListener() | |
{ | |
_eventColor.Add(TraceEventType.Verbose, ConsoleColor.DarkGray); | |
_eventColor.Add(TraceEventType.Information, ConsoleColor.Gray); | |
_eventColor.Add(TraceEventType.Warning, ConsoleColor.Yellow); | |
_eventColor.Add(TraceEventType.Error, ConsoleColor.DarkRed); | |
_eventColor.Add(TraceEventType.Critical, ConsoleColor.Red); | |
_eventColor.Add(TraceEventType.Start, ConsoleColor.DarkCyan); | |
_eventColor.Add(TraceEventType.Stop, ConsoleColor.DarkCyan); | |
} | |
/// <summary> Writes trace information, a message, and event information to the listener specific output. </summary> | |
/// <param name="eventCache"> A <see cref="T:System.Diagnostics.TraceEventCache" /> object that contains the current process ID, thread ID, and stack trace information. </param> | |
/// <param name="source"> A name used to identify the output, typically the name of the application that generated the trace event. </param> | |
/// <param name="eventType"> One of the <see cref="T:System.Diagnostics.TraceEventType" /> values specifying the type of event that has caused the trace. </param> | |
/// <param name="id"> A numeric identifier for the event. </param> | |
/// <param name="message"> A message to write. </param> | |
/// <seealso cref="M:System.Diagnostics.TraceListener.TraceEvent(TraceEventCache,string,TraceEventType,int,string)"/> | |
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) | |
{ | |
TraceEvent(eventCache, source, eventType, id, "{0}", message); | |
} | |
/// <summary> Writes trace information, a formatted array of objects and event information to the listener specific output. </summary> | |
/// <param name="eventCache"> A <see cref="T:System.Diagnostics.TraceEventCache" /> object that contains the current process ID, thread ID, and stack trace information. </param> | |
/// <param name="source"> A name used to identify the output, typically the name of the application that generated the trace event. </param> | |
/// <param name="eventType"> One of the <see cref="T:System.Diagnostics.TraceEventType" /> values specifying the type of event that has caused the trace. </param> | |
/// <param name="id"> A numeric identifier for the event. </param> | |
/// <param name="format"> A format string that contains zero or more format items, which correspond to objects in the <paramref name="args" /> array. </param> | |
/// <param name="args"> An object array containing zero or more objects to format. </param> | |
/// <seealso cref="M:System.Diagnostics.TraceListener.TraceEvent(TraceEventCache,string,TraceEventType,int,string,params object[])"/> | |
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args) | |
{ | |
var originalColor = Console.ForegroundColor; | |
Console.ForegroundColor = GetEventColor(eventType, originalColor); | |
base.TraceEvent(eventCache, source, eventType, id, format, args); | |
Console.ForegroundColor = originalColor; | |
} | |
/// <summary> Gets event color. </summary> | |
/// <param name="eventType"> Type of the event. </param> | |
/// <param name="defaultColor"> The default color. </param> | |
/// <returns> The event color. </returns> | |
private ConsoleColor GetEventColor(TraceEventType eventType, ConsoleColor defaultColor) | |
{ | |
if (!_eventColor.ContainsKey(eventType)) | |
{ | |
return defaultColor; | |
} | |
return _eventColor[eventType]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment