Last active
December 9, 2016 21:46
-
-
Save JohnL4/71b7be703cacd3516adabea47f1f1d62 to your computer and use it in GitHub Desktop.
Playing around with .NET TraceSource, a nicer "primitive" way of debug/trace logging than just using {Debug,Trace}.WriteLine(). More "primitive" (and lighter weight) than Enterprise Library logging.
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; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Runtime.CompilerServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace TraceSourcePlay | |
{ | |
class Program | |
{ | |
static TraceSource ts = new TraceSource("TSPlay"); | |
static void Main(string[] args) | |
{ | |
//var ts = new TraceSource("TSPlay"); | |
#if TRACE | |
Console.WriteLine( "TRACE is on"); | |
#else | |
Console.WriteLine( "TRACE is off"); | |
#endif | |
Debug.WriteLine( "Debug.WriteLine()" ); | |
Trace.WriteLine( "Trace.WriteLine()" ); | |
ts.TraceInformation( "Trace info" ); | |
foo(); | |
ts.TraceInformation( "foo done" ); | |
Console.WriteLine( "(press any key to continue)"); | |
Console.ReadKey(); | |
Console.WriteLine( "Done."); | |
} | |
static void foo() | |
{ | |
Trace.Indent(); // This doesn't seem to work. | |
traceSourceIndent(); | |
var st = new StackTrace(); | |
ts.TraceData( TraceEventType.Warning, 0, new object[] {"text message", st} ); | |
Trace.Unindent(); | |
traceSourceUnindent(); | |
} | |
static void traceSourceIndent() | |
{ | |
//var traceListener = ts.Listeners["file"]; | |
//if (traceListener != null) traceListener.IndentLevel++; | |
foreach (var listener in ts.Listeners) | |
{ | |
((TraceListener) listener).IndentLevel++; | |
} | |
} | |
static void traceSourceUnindent() | |
{ | |
//var traceListener = ts.Listeners["file"]; | |
//if (traceListener != null) traceListener.IndentLevel--; | |
foreach (var listener in ts.Listeners) | |
{ | |
((TraceListener)listener).IndentLevel--; | |
} | |
} | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8" ?> | |
<!-- (This is App.config in your Visual Studio solution, I just named it with a leading "z" to make it sort properly | |
and not mess up the name of this gist. --> | |
<configuration> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> | |
</startup> | |
<system.diagnostics> | |
<sources> | |
<source name="TSPlay"> <!-- default switchName is same as source name. --> | |
<!-- Listeners here are for TraceSource calls, although indentation and autoflush are controlled by "trace" section, below. --> | |
<listeners> | |
<add name="consoleListener"/> | |
<add name="file"/> | |
</listeners> | |
</source> | |
<!-- There can be only one source with name "TSPlay". switchName attributes will overwrite, | |
in the order in which they are encountered in this config file. --> | |
<!--<source name="TSPlay" switchName="Main"> | |
<listeners> | |
<add name="consoleListener"/> | |
</listeners> | |
</source> | |
<source name="TSPlay" switchName="foo"> | |
<listeners> | |
<add name="consoleListener"/> | |
</listeners> | |
</source>--> | |
</sources> | |
<switches> | |
<add name="TSPlay" value="All"/> | |
<add name="Main" value="All"/> | |
<add name="foo" value="All"/> | |
</switches> | |
<sharedListeners> | |
<add name="consoleListener" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/> | |
<add name="file" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\\tsplay.log"/> | |
</sharedListeners> | |
<trace autoflush="true" indentsize="2"> | |
<!-- Listeners here are for Trace and Debug calls, not TraceSource. --> | |
<listeners> | |
<!--<add name="consoleListener"/>--> | |
</listeners> | |
</trace> | |
</system.diagnostics> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment