21

I'm running code on a microcontroller with .NET Micro Framework, and I want my debug output to write to a text file. How does this work?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
755
  • 2,991
  • 3
  • 20
  • 34
  • I recommend you just redirect the output to a file (e.g. your_app > log.txt). It's more flexible. – m0skit0 Oct 28 '11 at 08:00
  • 1
    If we're talking flexible, I'd say use NLog. Only, I have no idea how or in what ways it would work on a constrained system... @m0skit0: Is there a shell with which to do the redirection when using .Net MF? – Merlyn Morgan-Graham Oct 28 '11 at 08:01
  • nlog's good also log4net [Edit] oops not sure those are options on .NET Micro... – kenny Nov 16 '11 at 08:51

4 Answers4

30

Use Trace. It is designed to do what you need.

using System;
using System.Diagnostics;

class Test
{
    static void Main()
    {
       Trace.Listeners.Add(new TextWriterTraceListener("yourlog.log"));
       Trace.AutoFlush = true;
       Trace.Indent();
       Trace.WriteLine("Entering Main");
       Console.WriteLine("Hello World.");
       Trace.WriteLine("Exiting Main");
       Trace.Unindent();
       Trace.Flush();
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ekk
  • 5,627
  • 19
  • 27
  • 2
    If you only want to trace to Console, then ConsoleTraceListener seems to be a clearer and more straight forward solution. But he asked to write into a file. – Ucodia Oct 28 '11 at 08:11
  • I just update my answer, thanks to point me out the problem. :) – Ekk Oct 28 '11 at 08:13
16

The most flexible solution for using a out-of-the-box tracing is to make an application configuration file that will define trace listeners.

<configuration>
  <system.diagnostics>
    <trace autoflush="true">
      <listeners>
        <add name="textListener" 
             type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="trace.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

Then, in your application, whenever you want to log something, just do:

Trace.WriteLine("Hello, this is a trace");

But the power of the TraceListener class lies into its granularity. You can chose between Error, Info and Warning levels and define different log file for whatever level you need to trace. Using configuration files makes it also easier to disable tracing in your application because you don't need to recompile your application.

For more informations on tracing system, check this MSDN article.

Ucodia
  • 7,410
  • 11
  • 47
  • 81
  • 1
    +1 I think you answer my answer/question you solution should work in .NET Micro Framework. I'am still using debugview.exe. – JPBlanc Oct 28 '11 at 08:37
13

Ekk is right about Trace being a better design, however that doesn't answer the question, which would be fine in the absence of a direct solution. The OP or someone may have inherited a code base which uses Debug throughout, and Trace may not be desirable at the time.

I found this solution http://bytes.com/topic/c-sharp/answers/273066-redirect-output-debug-writeline:

TextWriterTraceListener[] listeners = new TextWriterTraceListener[] {
new TextWriterTraceListener("C:\\debug.txt"),
new TextWriterTraceListener(Console.Out)
};

Debug.Listeners.AddRange(listeners);

Debug.WriteLine("Some Value", "Some Category");
Debug.WriteLine("Some Other Value");
hultqvist
  • 17,451
  • 15
  • 64
  • 101
Kind Contributor
  • 17,547
  • 6
  • 53
  • 70
  • Will this flush to and release the file automatically and all that jazz? – Sinaesthetic Feb 26 '15 at 20:31
  • 1
    I believe autoflush can be configured in the .config file - `` - see http://stackoverflow.com/questions/4345952/textwritertracelistener-does-not-work. There may also be a static `AutoFlush property` on the `System.Diagnostics.Trace` class. I personally wouldn't worry about autoflushing. Normally tracing handles a lot of data, and therefore for performance buffering is highly recommended. If you are focusing on debugging a problem area, perhaps you could try keeping a static reference to `listeners` and calling `.Flush()` on the relevant lines. – Kind Contributor Feb 28 '15 at 05:49
  • Quick and easy, as you suggest, we can't all ways be in control of the source to be developed :) That said, this solutions does kind of trigger a double output print inside visual studio. But thanks anyway – Christopher Bonitz Jan 24 '17 at 09:03
  • Remember to flush the output before closing the application, otherwise you will be missing the last lines of the stream – Christopher Bonitz Jan 24 '17 at 09:22
0

You will have to do something like this:

// Set up listener
string filename = @"C:\listener.txt";
FileStream traceLog = new FileStream(filename, FileMode.OpenOrCreate);
TextWriterTraceListener listener = new TextWriterTraceListener(traceLog);

// Output to listener
listener.WriteLine("Trace message here");

// Flush any open output before termination.
// Maybe in an override of Form.OnClosed.
listener.Flush();

Taken from here.

Another related question

Community
  • 1
  • 1
Aamir
  • 14,882
  • 6
  • 45
  • 69