Home | Index | Examples | Guidance | FAQ | Listeners | Filters | Extensions
The simplest possible example using the ubiquitous "Hello World":
Hello.cs
#define TRACE
using System.Diagnostics;
class Program {
static TraceSource _trace = new TraceSource("Hello");
public static void Main() {
_trace.TraceInformation("Hello World!");
}
}
Hello.exe.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="Hello" switchValue="All">
<listeners>
<clear />
<add name="console"
type="System.Diagnostics.ConsoleTraceListener" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Compile with csc Hello.cs
(in .NET 4.0 you may need csc Hello.cs /r:System.dll
), and run, you should get the following output:
PS C:\Essential.Diagnostics\Examples> csc.exe Hello.cs
PS C:\Essential.Diagnostics\Examples> .\Hello.exe
Hello Information: 0 : Hello World!
Note that instead of #define TRACE
you would usually compile as csc Hello.cs /d:TRACE
, which is turned on by default in Visual Studio.
A simple "Hello World" isn’t however very useful for showing the different capabilities of logging, so the next page will walk you through a logging primer to introduce the range of features available in System.Diagnostics and the Essential.Diagnostics extensions.
Next: Logging Primer