Click here to Skip to main content
15,846,145 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have been trying for days to write the last 5 five lines of the debug window into an external text file. No matter what I do, it fails. Now I am having issues with the third line from the bottom "sb.ToString().Split('\n').Reverse()" underlined in red. The ERROR Reads: No overload for method 'Reverse' takes 0 arguments. I don't know what this means and I don't know how to fix it. Can someone please help.

C#
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Collections;
namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            byte[] data = new byte[] {1,2,3,4,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,72,101,108,111,13,10};
            for (int i = 0; i < 28; i++)
            {
                //Console.WriteLine("{0}",data[i]);
                StringBuilder sb = new StringBuilder();
                StringWriter sw = new StringWriter(sb);
                TextWriterTraceListener myWriter = new TextWriterTraceListener(sw);
                Debug.Listeners.Add(myWriter);
                string output = string.Join("\n", sb.ToString().Split('\n').Reverse().Take(5).Reverse());
                string path = @"c:\temp\Log.txt";
                File.WriteAllText(path, output);
            }
        }
    }
}
Posted
Comments
ZurdoDev 26-May-15 16:21pm    
Reverse is a static method on Array and not to be used on this instance.

See https://msdn.microsoft.com/en-us/library/d3877932(v=vs.110).aspx

So, you would call Array.Reverse(sb.ToString().Split('\n'));
Sergey Alexandrovich Kryukov 26-May-15 17:27pm    
The whole idea of writing lines of some window to a file is wrong. You are not writing anything "from a window". You just write some data to file.
—SA

1 solution

Your problem is: you are correctly using TextWriterTraceListener, but it is used for tracing or debugging output. But you are not writing anything to Trace, so there is nothing to capture. The whole code simply makes no sense.

Read MSDN documentation thoroughly, pay attention for the code sample, read all about it and also Trace and Debug:
https://msdn.microsoft.com/en-us/library/system.diagnostics.textwritertracelistener%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.trace%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.debug(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
computerpublic 27-May-15 5:53am    
Sir,
I have read the msdn's and looked at the examples. I dont understand why you say I am not writing anything to Trace. Can you show me how to fix the String Output line?
Sergey Alexandrovich Kryukov 27-May-15 9:09am    
Because you don't show any Trace code. Where?
If you simply want to do output, do it, why capturing anything (nothing, in this case)?
Again, look at the code sample.
—SA
computerpublic 28-May-15 0:48am    
The following is the only example that comes anywhere close to what i am trying to accomplish, however I don't understand how to put the last 5 lines of the debugger inside it.
// Create a file for output named TestFile.txt.
Stream myFile = File.Create(@"c:\check\TestFile.txt");
//* Create a new text writer using the output stream, and add it to
// * the trace listeners.
TextWriterTraceListener myTextListener = new TextWriterTraceListener(myFile);
Trace.Listeners.Add(myTextListener);
//Write output to the file.
Trace.Write("test");
// Flush the output.
Trace.Flush();

Sergey Alexandrovich Kryukov 28-May-15 0:53am    
All right, this is completely different story: Trace is used. And I explain why your code sample produces no content in the file: because nothing was sent to output. Isn't that clear? If it is, will you accept the answer formally?
—SA
computerpublic 28-May-15 19:41pm    
I still do not understand how to get the last 5 lines of the debugger. Its been several days. I did as you told me and I am still stuck. What exactly do you want me accept? I just to get the thing to work. Can you show me how to get the last 5 lines of the debugger into an external file?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900