Problem redirecting debug output to a file using trace listener

杀马特。学长 韩版系。学妹 提交于 2019-12-01 03:04:12

问题


I have created a debug listener to redirect the output from the Debug/Console window to a file(with a call stack), using the following code:

void SomeMethod()
{
    // Create a file for output .txt.
    Stream debugFile = File.Create(fileName);

    // create TextWriterTraceListener named "file"
    TextWriterTraceListener debugWriter = new TextWriterTraceListener(debugFile, "file");

    // add to debug listeners
    Debug.Listeners.Add(debugWriter);
    // set callstack to be shown
    Debug.Listeners["file"].TraceOutputOptions |= TraceOptions.Callstack;
    // set auto-flush
    Debug.AutoFlush = true;
}

but the output won't redirect to the file I specified, it's always empty.

I am calling this from the constructor in my main form. Is the place where I'm calling it from a problem?

What I am trying to achieve here is to have the exceptions from the Debug output window placed in a file with a call stack, so that I can find them and correct them.

UPDATE: After some research I came to a conclusion that adding a new TraceListener to the Debug Listeners collection does not redirect the output from the Debug/Console. It is actually just responding to Write, WriteLine etc. methods as does the default listener. The problem still remains: How to capture the output of the Debug/Console window and how to get the stack trace of the exceptions that appear there?

Anyone have any ideas?


回答1:


Here's an article that answers a part of my question: http://www.codeproject.com/KB/trace/DbMonNET.aspx

i.e. how to capture the output of the Debug/Console window. But, it seems that there's no way of getting the stack trace from this output. Looking from this perspective it looks like a bad approach anyway.

FURTHER RESEARCH: Looks like these exceptions are appearing because they are handled in some other dll that is not linked properly, and they are handled there instead of my try/catch blocks. This is probably the place where I should be looking my error for i.e. where there's a dll reference I should instead add a project reference.

MORE RESEARCH: Enable breaking at exceptions in Visual Studio main menu: Debug -> Exceptions -> Check the type of exceptions you want the application to break at(Common Language Runtime)...There's no better way to deal with exceptions.



来源:https://stackoverflow.com/questions/5568630/problem-redirecting-debug-output-to-a-file-using-trace-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!