Click here to Skip to main content
14,428,725 members

Tracing for Compact Framework apps

Rate this:
4.32 (12 votes)
Please Sign up or sign in to vote.
4.32 (12 votes)
8 Jun 2004CPOL
This article proposes a solution that mitigates the lack of debug console, when building Compact Framework-based Pocket PC apps.

Tcptrace demo


One of the features of the Visual Studio IDE I miss the most when developing Pocket PC applications is the ability to see in the Output pane, the trace messages I usually spread throughout my code. If you have developed code for a while, you know the second best option is to have a console window where you can see these messages.

Not having the knowledge, nor the time to build a solution integrated with the IDE, in this article I show a console application that displays in a host PC, the strings passed to System.Diagnostics.Debug.Write or System.Diagnostics.Debug.WriteLine calls in Compact Framework code running on a Pocket PC.


  1. Sockets

    Sockets are amongst the simplest ways of communicating two apps in almost every system. But in the .NET framework you can find a pair of complementary classes that simplify even further the sockets API: System.Net.Sockets.TcpClient and System.Net.Sockets.TcpListener. Of course, their underlying implementation utilizes sockets and their use patterns shouldn't come as a surprise for anyone who has ever programmed against the sockets API.

    TcpListener and TcpClient synchronous blocking communications doesn't provide all the flexibility sockets do, but in this case, they fulfill my modest requirements.

    See the MSDN Library for more information on sockets and the System.Net.Sockets namespace

  2. Trace listeners

    The Systems.Diagnostics namespace provides a mechanism that allows multiple output for the trace messages. When you call the Debug or Trace classes' Write and WriteLine family of methods, they invoke the corresponding methods on every listener that has been registered for that purpose. The system provides some implementations of the TraceListener abstract class that direct messages to the output window of a debugger, an output stream or the event log. If your needs differ from the system provided options, you need to write your own implementation of System.Diagnostic.TraceListener and add it to the listeners collection.

    You can find the documentation about trace listeners here.

Using the code

The debug console

TcpTraceConsole is a console application that acts as the server. It waits for connections to arrive to the IP address and port specified in its command line. When a connection is made, it just uses System.Console.WriteLine to display each message received.

Obviously, it must be launched in the host PC before running the Pocket PC app. For example, if the IP address of your PC is and its port 14001 is not used yet:

C:>TcpTraceConsole 14001

The trace listener

I derived TcpTrace.TcpTraceListener directly from TraceListener and overrode the Write and WriteLine methods to redirect their output to the console app via a TcpClient instance. This class is packaged in its own assembly, so it can be used in any application:

  1. Add a reference to TcpTrace.dll in your project
  2. Before any call to Debug.Trace in your code, call the static method TcpTrace.InstallTcpTraceListener. If you use the version with parameters, you need to supply the same address and port you used to launch the TcpTrace console app. If you use the parameter-less version, your working directory must contain an application configuration file specifying the address and port in the following way:

        <add key="TcpTraceServer" value="<SERVER_NAME>" />
        <add key="TcpTracePort" value= "<PORT_NUMBER>" />

Included in the source code and in the demo .zip files, there is a Pocket PC WinForms app that shows the use of TcpTrace.TcpTraceListener.

To run the PpcTraceClient.exe app, you must:

  1. Copy the PpcTraceClient.exe, PpcTraceClient.exe.config, TcpTrace.dll and NetTech.Configuration.dll files into a folder on your device
  2. Edit the PpcTraceClient.exe.config and update the TcpTraceServer and TcpTracePort entries to match the IP address and port that TcpTraceConsole is listening to.

A little annoyance I've found when using the listener is that in my actual device, when connected to my desktop PC through the USB connection, I have to pass the PC's DNS name instead of its IP address, while in the emulator I have to pass the IP address. You may find a different behavior in your configuration.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jose Luis Balsera
Software Developer
United States United States
No Biography provided

Comments and Discussions

General[Message Deleted] Pin
it.ragester2-Apr-09 22:47
Memberit.ragester2-Apr-09 22:47 
GeneralTracing on actual device via USB Pin
Mark Pantus30-Dec-05 2:39
MemberMark Pantus30-Dec-05 2:39 
Generaland some code to handle Hostname or IP Pin
cyrille3722-Jun-05 15:29
Membercyrille3722-Jun-05 15:29 
Questionwhere is file NetTech.Configuration.dll? Pin
SanDiegoPhil17-Sep-04 5:19
MemberSanDiegoPhil17-Sep-04 5:19 
GeneralLogging Pin
pat2708815-Aug-04 13:20
Memberpat2708815-Aug-04 13:20 
GeneralRe: Logging Pin
pat2708816-Aug-04 5:13
Memberpat2708816-Aug-04 5:13 
GeneralRe: Logging Pin
Jose Luis Balsera7-Aug-04 0:16
MemberJose Luis Balsera7-Aug-04 0:16 
GeneralRe: Logging Pin
pat2708817-Aug-04 3:07
Memberpat2708817-Aug-04 3:07 
GeneralTcpTraceConsole uses 100% CPU Pin
Jol3-May-04 18:13
professionalJol3-May-04 18:13 
GeneralRe: TcpTraceConsole uses 100% CPU Pin
Jose Luis Balsera10-Jun-04 10:08
MemberJose Luis Balsera10-Jun-04 10:08 
GeneralLog4Net Pin
Uwe Keim9-Oct-03 5:36
sitebuilderUwe Keim9-Oct-03 5:36 
GeneralRe: Log4Net Pin
Jose Luis Balsera10-Oct-03 0:29
MemberJose Luis Balsera10-Oct-03 0:29 
GeneralRe: Log4Net Pin
emeraldtea25-Nov-03 8:13
Memberemeraldtea25-Nov-03 8:13 
GeneralRe: Log4Net Pin
pat2708815-Aug-04 13:33
Memberpat2708815-Aug-04 13:33 
GeneralRe: Log4Net Pin
Jose Luis Balsera7-Aug-04 0:07
MemberJose Luis Balsera7-Aug-04 0:07 
GeneralRe: Log4Net Pin
converdb4-May-06 3:57
Memberconverdb4-May-06 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 8 Oct 2003


24 bookmarked