Getting Started - The Basics

The simplest possible example using the ubiquitous "Hello World":

#define TRACE
using System.Diagnostics;

class Program {
  static TraceSource _trace = new TraceSource("Hello");

  public static void Main() {
    _trace.TraceInformation("Hello World!");

<?xml version="1.0" encoding="utf-8" ?>
      <source name="Hello" switchValue="All">
          <clear />
          <add name="console" 
            type="System.Diagnostics.ConsoleTraceListener" />

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.


Last edited Nov 2, 2011 at 6:07 AM by sgryphon, version 12


No comments yet.