Starting a new project

If you’re one of the lucky few who never need to maintain existing applications, then you can leverage the project templates that ship out of the box with NTrace. As of the time this document was written, there are currently templates for C# Console applications, class libraries, and WinForms applications with plans for project templates for WCF, ASP.NET, and WPF applications soon. Starting a project using the project templates sets everything up for you: all you need to do is write your code and add tracing calls.

How to add NTrace to existing code

Declare an ETW Provider

In order to have your application publish events to ETW, you must add a reference to the NTrace library to your project (in the %ProgramFiles%\NTrace folder or in your machine’s GAC) and include a static field that is of type NTrace.ClassicProvider. The name you use for the variable isn’t really important, but the fact that it can be accessed by all of the classes in your assembly is. In practice, it is advised to use internal so that it is accessible from all of the classes in your assembly.

Tweak An Existing Project

In many cases, you’ll want to add the preprocessor to your existing code. In order to have the preprocessor kick in and do its magic, you’ll have to import the build instructions into your C# project. To do this, simply add properties registering your ETW provider and replace your existing reference to Microsoft.CSharp.targets with one to NTrace.CSharp.Targets from the $(MSBuildExtensionsPath)\NTrace\<<version>> folder like so:
<!—
  These values "register" your provider and tells the preprocessor how
  To hook things up when it’s time to build.
  -->
  <PropertyGroup>
    <EtwProviderVariable>
      <<fully-qualified provider variable name>>
    </EtwProviderVariable>
    <EtwProviderId>96B47F82-971F-4644-821F-B55FB2439DAD</EtwProviderId>
  </PropertyGroup>

  …

  <!—There will already be an Import at the bottom of your project, pointing to Microsoft.CSharp.targets. Change it to the following -->
  <Import Project="$(MSBuildExtensionsPath)\NTrace\v2.0\NTrace.CSharp.targets" />

How to capture the output of your application's logging:

Last edited Apr 29, 2011 at 7:18 PM by ahopper, version 4

Comments

FredStephens Sep 20, 2012 at 9:40 PM 
Also when tweaking an exisiting project the TraceHelper.cs file has to have a build action of Instrument to instantiate the TraceHelper.providor object.

FredStephens Sep 20, 2012 at 8:04 PM 
Previous comment applies to Tweak An Existing Project

FredStephens Sep 20, 2012 at 8:01 PM 
To get the NTRACE tab to appear under your project properties. You need to another line after <TargetFrameworkversion> to specify that there is a new project type for NTrace and a C# project.

<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<ProjectTypeGuids>{B5E33638-F656-4309-B8B1-11385E5F1346};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>