This post has been migrated from www.experimentsincode.com, we apologise if some of the images or content is missing

This is second part in using the Sitecore Debug tool. The first post discussed using the Sitecore Profiler to output data to the Sitecore Debug screen. This post will discuss how we can use the Sitecore Tracer to output information to the debug screen. The Tracer lives at Sitecore.Diagnostics.Tracer and has a different setup to the Profiler. The Tracer allows you to output data to the Sitecore Trace section of the debug page:
Notice that Trace outputs time as well but this is calculated in a different way to the Profiler. First lets look at how we write a message to the screen:
  Tracer.Info("I am here");
This renders the following in the trace:
Notice that Sitecore has added a time against the output, this the time since the last trace message was written. We can see this is if we update our code to have two traces and a delay:
Tracer.Info("I am here");

Thread.Sleep(1000);

Tracer.Info("I am also");
From the output we can see the 1000ms delay has been noted against the "I am also" message:
So now we can output different messages, but you may want to indent messages to create a hierarchy, this is achieved using the indent property and increasing and decreasing it:
            Tracer.Info("I am here");
            Tracer.Indent++;

            Thread.Sleep(1000);
            Tracer.Info("I am in the middle");
            Thread.Sleep(1000);

            Tracer.Indent--;
            Tracer.Info("I am also");
Notice that I both increment and decrement the indent counter, you should ensure you do both otherwise you might have some weird output. Looking at the output we should see the "I am in the middle" message has been indented:
The Tracer is useful when you want to record the time a series on individuals tasks take to find out which task was the slowest:
Tracer.Info("Getting data started");

var tweets = twitter.GetTweets("mikeedwards83");
Tracer.Info("Retrieved Tweets");

var facebook = facebook.GetPosts("mikeedwards");
Tracer.Info("Retrieved Facebook");

We could then use the Tracer to see if Twitter or Facebook were slower.

Warning Levels

The Tracer also allows you to indicate message severity and has the following levels: * Debug * Info * Warning * Error * Fatal Let see what we get if we output each one:
            Tracer.Debug("Debug me");

            Tracer.Info("Info me");

            Tracer.Warning("Warn me");

            Tracer.Error("Error me");

            Tracer.Fatal("Died");
On the trace screen we can see the following, notice that Warning and Error are highlighted (I am not sure why fatal isn't):

Debug Information

The DebugInfo method allows you to create output that contains additional data via the Packet class:
DebugInfo(string debugInfoID, Packet packet)
The Packet class essentially represents an XML data structure, lets see this in use:
            var packet = new Packet();
            packet.AddElement("service", "twitter");
            packet.AddElement("username", "mikeedwards83");

            Tracer.DebugInfo("Debug with packet", packet);
If you look at the trace screen you will see that the message and data is missing. Instead to get access to this info you need to download the trace file by clicking the Download link:
When you open the file you should get an XML output that contains the full trace and within that you should find the debug info:
<debuginfo indent="0" sincestart="953888" elapsed="3237" time="20130704T112427" id="Debug with packet">
  <message>Collected debug information</message>
  <sitecore>
    <service>twitter</service>
    <username>mikeedwards83</username>
  </sitecore>
</debuginfo>

Syntactic Sugar

I would like a simpler way to write a message, add an indent and then remove the indent, this is where a using statement comes to the rescue. I have created the following wrapper class for the Tracer:
    public class SugaryTracer : IDisposable
    {
        private readonly string _name;
        private readonly bool _endMessage;

        public SugaryTracer(string name, bool endMessage = false)
        {
            _name = name;
            _endMessage = endMessage;
            Tracer.Info(_name+" start");
            Tracer.Indent++;
        }

        public void Info(string message)
        {
            Tracer.Info(message);
        }
        public void Warning(string message)
        {
            Tracer.Warning(message);
        }
        public void Error(string message)
        {
            Tracer.Error(message);
        }
        public void Fatal(string message)
        {
            Tracer.Fatal(message);
        }

        public void Debug(string message)
        {
            Tracer.Debug(message);
        }
        public void DebugInfo(string message, Packet packet)
        {
            Tracer.DebugInfo(message, packet);
        }

        public void Dispose()
        {
            Tracer.Indent--;

            if(_endMessage)
                Tracer.Info(_name+" end ");
        }
I can then update my code to use a using statement and make indented messages like so:
            using (var tracer = new SugaryTracer("I have stated to do something"))
            {
               tracer.Info("This is an indented message");
            }
If you haven't yet, check out Using the Sitecore Debug Tool Part 1.