Visual Studio:谁在写控制台?(Visual Studio: Who is writing to console?)

编程入门 行业动态 更新时间:2024-10-23 12:36:19
Visual Studio:谁在写控制台?(Visual Studio: Who is writing to console?)

好的,这是一个很好的(我认为) - 我正在开发一个由开发团队创建的具有很多(太多)依赖dll的应用程序。 我正在尝试调试一个程序集,但控制台输出被Console.WriteLine和Debug.WriteLine '污染'留在代码周围。

无论如何,我可以确切地知道给定线路来自哪个组件,所以我可以让作者清理它们的来源?

更新如果您还遇到此类问题,请注意还有另一个潜在的输出消息源,即任何设置为打印消息时设置为'When hit'的断点。 说了这个,这是一个非常酷的功能,可以防止我上面遇到的那种问题。

OK, here's a good one (I think) - I'm working on an application with lots (far too many) dependency dlls, created by a team of developers. I'm trying to debug just one assembly, but the console output is 'polluted' by the Console.WriteLines and Debug.WriteLines left scattered around the code.

Is there anyway I can work out exactly which assembly a given line is coming from, so I can get the author to clean up their source?

UPDATE If you're also experiencing this kind of issue, note that there is another potential source of output messages which is any breakpoints with 'When hit' set to print a message. Having said which, this is a VERY cool feature, which can prevent the kind of problems I was having above.

最满意答案

是 - 替换Console.Out 。 在创建TextWriter之后使用Console.SetOut ,它不仅将请求的数据转储到原始控制台,还将堆栈跟踪(以及时间戳和请求的数据)转储到文件中。

这里有一些代码,改编自Benjol的答案:

(注意:您需要根据每次写入后或每个写入行后是否需要堆栈跟踪来调整此代码。在下面的代码中,每个char后跟堆栈跟踪!)

using System.Diagnostics; using System.IO; using System.Text; public sealed class StackTracingWriter : TextWriter { private readonly TextWriter writer; public StackTracingWriter (string path) { writer = new StreamWriter(path) { AutoFlush = true }; } public override System.Text.Encoding Encoding { get { return Encoding.UTF8; } } public override void Write(string value) { string trace = (new StackTrace(true)).ToString(); writer.Write(value + " - " + trace); } public override void Write(char[] buffer, int index, int count) { Write(new string(buffer, index, count)); } public override void Write(char value) { // Note that this will create a stack trace for each character! Write(value.ToString()); } public override void WriteLine() { // This is almost always going to be called in conjunction with // real text, so don't bother writing a stack trace writer.WriteLine(); } protected override void Dispose(bool disposing) { writer.Dispose(); } }

要使用Console.WriteLine和Debug.WriteLine to a file记录Debug.WriteLine to a file ,请在代码中尽早进行这样的调用:

var writer = new StackTracingWriter(@"C:\Temp\ConsoleOut.txt"); Console.SetOut(writer); Debug.Listeners.Add(new TextWriterTraceListener(writer));

请注意,这当前也不会写入原始控制台。 为此,您需要在StackTracingWriter使用第二个TextWriter (用于原始控制台),并且每次都写入两个位置。 但是,Debug将继续写入原始控制台。

Yes - replace Console.Out. Use Console.SetOut after creating a TextWriter which not only dumps the requested data to the original console, but also dumps a stack trace (and timestamp, and the requested data) to a file.

Here's some code, adapted from Benjol's answer:

(Note: you will want to adapt this code depending on whether you want a stack trace after each write, or after each writeline. In the code below, each char is followed by a stack trace!)

using System.Diagnostics; using System.IO; using System.Text; public sealed class StackTracingWriter : TextWriter { private readonly TextWriter writer; public StackTracingWriter (string path) { writer = new StreamWriter(path) { AutoFlush = true }; } public override System.Text.Encoding Encoding { get { return Encoding.UTF8; } } public override void Write(string value) { string trace = (new StackTrace(true)).ToString(); writer.Write(value + " - " + trace); } public override void Write(char[] buffer, int index, int count) { Write(new string(buffer, index, count)); } public override void Write(char value) { // Note that this will create a stack trace for each character! Write(value.ToString()); } public override void WriteLine() { // This is almost always going to be called in conjunction with // real text, so don't bother writing a stack trace writer.WriteLine(); } protected override void Dispose(bool disposing) { writer.Dispose(); } }

To use this for logging both Console.WriteLine and Debug.WriteLine to a file, make calls like this as early as possible in your code:

var writer = new StackTracingWriter(@"C:\Temp\ConsoleOut.txt"); Console.SetOut(writer); Debug.Listeners.Add(new TextWriterTraceListener(writer));

Note that this currently doesn't also write to the original console. To do so, you'd need to have a second TextWriter (for the original console) in StackTracingWriter, and write to both places each time. Debug will however continue to be written to the original console.

更多推荐

本文发布于:2023-08-03 13:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390739.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   谁在   Studio   Visual   writing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!