DEV Community

Cover image for "Use System.Diagnostics.Trace and DbgView within a WPF-Application"

"Use System.Diagnostics.Trace and DbgView within a WPF-Application"

kimkulling profile image Kim Kulling ・2 min read

This blogpost was first posted here

TRACE using C/C++ with Win32-API

One of my favorite tools to debug Win32/MFC-Applications with UI was the Win32-call:

::OutputDebugString( "Test\n" );

You can use it to trace debug information withing your application during runtime. Special if you have to analyze some runtime-specific error and you do not have any kind of console / log to do printf-debugging this Win32-Call case save your ass. You only have to run the tool DbgView to monitor these log-entries ( if you want to try it out you can download DbgView here.

TRACE using C# with WPF

Because I am currently working with C# in my job I wanted to use this tool as well for WVF-applications. And of course there is a corresponding API-method in the .NET-framework called:

Trace.WriteLine( string msg );

You can find the documentation here.

It is easy to use. Just insert your Trace-log-entries in your code. When you want to take a look into your application you can start DbgView to see, what is ongoing. Just make sure, that you have defined:

#define TRACE

at the beginning of your source-file. Here is a small example:

#define TRACE

using System.Diagnosics;

class Test {
    static int main() {
        Trace.WriteLine("Hello, world!" );

To check if this works fine just run DbgView and run your application. You should be able to see your post Hello, world! there.


Editor guide