Share via


DTM Logger Example Using C# (Legacy)

Note  This content applies to the Windows Logo Kit (WLK). For the latest information using the new Windows Hardware Certification Kit (HCK), see Windows HCK User's Guide on the Windows Hardware Dev Center.

Note   Deprecated This code example uses a legacy namespace. For new applications, use the Microsoft.WTT.Log namespace.

The following code example demonstrates how to implement logging for a simple test case with only one variation by using the WTTLogger .NET classes.

using System;
using Microsoft.Wtt.Log;

/// <summary>
/// Test case.
/// <summary>
class TestCase
{
  public void Run ()
  {
    // Using Trace makes it easier to do logging.
    Trace.Write( Level.Info, "Test case 1 is executing." );
    // Intentionally cause exception to demonstrate exception handling.
    ((string)null).ToString();
  }
}

/// <summary>
/// Sample with one test case without variations.
/// <summary>
class MainClass
{
    /// <summary>
    /// The main entry point for the application.
    /// <summary>
    [STAThread]
    static void Main(string[] args)
    {
        // Create new logging object.
        // Using 'using' guarantees that log.Dispose() will be called at the end.
        using ( WttLog log = new WttLog( "$console" ) )
        {
          IContext testContext = log.StartTest( "Test case 1" );

          try
          {
            // Create test case object.
            TestCase test = new TestCase();
            // Execute test case.
            test.Run();
          }
          catch ( Exception exception )
          {
            // Using CheckResult(...) is equivalent to Serialize( Level.Fail, ... )
            // but it makes code more readable since all check points are clearly
            // marked.
            log.CheckResult( "Unexpected exception: "+exception.Message, exception );
          }

          log.EndTest( testContext );
        }
    }
}

See Also

Code Samples for DTM Logger

 

 

Build date: 9/14/2012