Skip to content

Tracing and Diagnostics

Daniel Cazzulino edited this page Jan 9, 2014 · 3 revisions

Clide provides built-in tracing with support for writing to the Visual Studio output window automatically. It's based on the Tracer package, which is embedded in Clide and configured to run with the System.Diagnostics implementation.

To set up tracing, you only need to add your TraceListener implementation to Clide's tracer manager:

var fileListener = new TextWriterTraceListener(@"C:\Temp\log.txt");

Tracer.Manager.AddListener("MyExtension", fileListener);

This will cause all traces from all classes under the MyExtension namespace to trace to the given log file, provided the right tracing level is met. You can set the tracing level for arbitrary traces using the following code:

Tracer.Manager.SetTracingLevel("MyExtension", SourceLevels.All);

Your components can now trace by retrieving their tracer as follows:

[Command(Constants.CommandSet, Constants.cmdMyCommand)]
public class MyCommand : ICommandExtension
{
        private static readonly ITracer tracer = Tracer.Get<MyCommand>();

        public string Text
        {
            get { return "Sample"; }
        }

        public void Execute(IMenuCommand command)
        {
			tracer.Info("Executing MyCommand");
        }

        public void QueryStatus(IMenuCommand command)
        {
            command.Enabled = command.Visible = true;
			tracer.Verbose("Command is enabled: {0}", command.Enabled);
        }
}

Tracer supports hierarchical tracing and configuration.

Output Window Tracing

Another common setup for extensions is to trace certain messages to an output window pane. Clide provides access to output window traces in the form of a TextWriter that can be retrieved directly from the IDevEnv interface:

IDevEnv devEnv = Host.Initialize(this);
TextWriter outputWindow = devEnv.OutputWindow.GetPane(this, "My Extension");

The returned text writer can be used to construct a regular TextWriterTraceListener:

Tracer.Manager.AddListener("MyExtension", new TextWriterTraceListener(outputWindow));

This writer is a bit verbose, however, and traces don't look that great considering they are intended for the end user:

MyPackage.ShellPackage Information: 0 : Shell package initialized 

It gets a lot more verbose if you use Activity Tracing. To get a cleaner output, you can use Clide's provided TextTraceListener instead:

Tracer.Manager.AddListener("MyExtension", new TextTraceListener(outputWindow));

This listener only writes simple strings, with just the trace level (Error, Information, etc.), properly padded so that it's easy to read, and the message string. All non-message traces (i.e. Transfer, Suspend, Resume, Stop, data traces, etc.) are not rendered at all. The above trace becomes:

Information: Shell package initialized

You can learn more about tracing by reading the Tracer wiki.

Table of Contents


- [How To Code Samples][1]
Clone this wiki locally