Last active
December 24, 2015 12:29
-
-
Save ridomin/6798405 to your computer and use it in GitHub Desktop.
Basic Logger to use TraceSources and TraceEventTypes from .Net based Apps
This file contains 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
<system.diagnostics> | |
<sharedListeners> | |
<add name="DefaultListener" type="System.Diagnostics.TextWriterTraceListener" | |
initializeData="LogFiles\Traces\Traces.log" /> | |
</sharedListeners> | |
<sources> | |
<source name="General"> | |
<listeners> | |
<add name="DefaultListener" /> | |
</listeners> | |
</source> | |
<source name="Web"> | |
<listeners> | |
<add name="DefaultListener" /> | |
</listeners> | |
</source> | |
</sources> | |
<switches> | |
<add name="General" value="Verbose" /> | |
<add name="Web" value="Verbose" /> | |
<add name="Runtime" value="Verbose" /> | |
<add name="Security" value="Verbose" /> | |
</switches> | |
<trace autoflush="true" indentsize="4"> | |
<listeners> | |
<add name="DefaultListener" /> | |
</listeners> | |
</trace> | |
</system.diagnostics> |
This file contains 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
class FaultyClass | |
{ | |
TraceLogger log = TraceLogger.Create(TraceCategory.General); | |
FaultyClass() | |
{ | |
log.TraceVerbose("Ctor {0}", "FaultyClass"); | |
} | |
internal void ThrowWhenCalled() | |
{ | |
var ex = new ApplicationException("Forced Exception " + DateTime.Now.ToShortTimeString()); | |
log.TraceErrorFromException(ex, true, true); | |
throw ex; | |
} | |
} |
This file contains 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
namespace Rido.Core.Tracing | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
public enum TraceCategory | |
{ | |
General, Runtime, Web, Worker, Security | |
} | |
public interface ITraceLogger | |
{ | |
void Trace(TraceEventType level, string message, params object[] mens); | |
void TraceError(string message, params object[] mens); | |
void TraceInformation(string message, params object[] mens); | |
void TraceErrorFromException(Exception exception, bool showStackTrace, bool showDetail); | |
void TraceVerbose(string message, params object[] mens); | |
void TraceWarning(string message, params object[] mens); | |
void TraceCritical(string message, params object[] mens); | |
} | |
public class TraceLogger : ITraceLogger | |
{ | |
private static Dictionary<TraceCategory, TraceSource> _traceSources = new Dictionary<TraceCategory, TraceSource>(); | |
TraceSource _ts; | |
private TraceCategory Source { get; set; } | |
internal static Dictionary<TraceCategory, TraceSource> TraceSources | |
{ | |
get { return _traceSources; } | |
} | |
static TraceLogger() | |
{ | |
Initialize(); | |
} | |
private static void Initialize() | |
{ | |
Array items = Enum.GetValues(typeof(TraceCategory)); | |
foreach (var item in items) | |
{ | |
_traceSources.Add((TraceCategory)item, new TraceSource(item.ToString())); | |
} | |
} | |
public static TraceLogger Create(TraceCategory source) | |
{ | |
TraceLogger logger = new TraceLogger(source); | |
return logger; | |
} | |
protected TraceLogger(TraceCategory source) | |
{ | |
Source = source; | |
_ts = _traceSources[this.Source]; | |
} | |
public void TraceVerbose(string message, params object[] mens) | |
{ | |
Trace(TraceEventType.Verbose, message, mens); | |
} | |
public void TraceInformation(string message, params object[] mens) | |
{ | |
Trace(TraceEventType.Information, message, mens); | |
} | |
public void TraceWarning(string message, params object[] mens) | |
{ | |
Trace(TraceEventType.Warning, message, mens); | |
} | |
public void TraceCritical(string message, params object[] mens) | |
{ | |
Trace(TraceEventType.Critical, message, mens); | |
} | |
public void TraceError(string message, params object[] mens) | |
{ | |
Trace(TraceEventType.Error, message, mens); | |
} | |
public void Trace(TraceEventType level, string message, params object[] mens) | |
{ | |
string formmatedMessage = string.Concat(string.Format(message, mens)); | |
_ts.TraceEvent(level, 0, formmatedMessage); | |
} | |
public void TraceErrorFromException(Exception exception, bool showStackTrace, bool showDetail) | |
{ | |
if (showDetail) | |
{ | |
TraceError(exception.ToString()); | |
return; | |
} | |
if (showStackTrace) | |
{ | |
TraceError("{0} {1}", exception.Message, exception.StackTrace.ToString()); | |
return; | |
} | |
TraceError(exception.Message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment