Writing C# debug output to .txt file
Asked Answered
M

4

22

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?

Muskrat answered 28/10, 2011 at 7:57 Comment(3)
I recommend you just redirect the output to a file (e.g. your_app > log.txt). It's more flexible.Autograft
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?Homomorphism
nlog's good also log4net Edit oops not sure those are options on .NET Micro...Flanagan
K
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();
    }
}
Karney answered 28/10, 2011 at 8:4 Comment(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.Reservoir
I just update my answer, thanks to point me out the problem. :)Karney
R
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.

Reservoir answered 28/10, 2011 at 8:27 Comment(1)
+1 I think you answer my answer/question you solution should work in .NET Micro Framework. I'am still using debugview.exe.Rodenhouse
H
14

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.AutoFlush = true;

Debug.WriteLine("Some Value", "Some Category");
Debug.WriteLine("Some Other Value");
Hospitable answered 16/9, 2014 at 1:33 Comment(5)
Will this flush to and release the file automatically and all that jazz?Butene
I believe autoflush can be configured in the .config file - <trace autoflush="true"> - see #4346452. 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.Hospitable
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 anywayTymon
Remember to flush the output before closing the application, otherwise you will be missing the last lines of the streamTymon
@Butene I just improved the answer without a configurationConstrain
C
-1

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

Curacy answered 28/10, 2011 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.