C# 是否可以在我的应用程序中显示VS输出窗口内容?
长话短说:我在项目中使用的某些第三方非托管dll显然在运行时将其错误转储到Visual Studio输出窗口(显示“调试”的输出)。看起来是这样的:C# 是否可以在我的应用程序中显示VS输出窗口内容?,c#,visual-studio,debugging,C#,Visual Studio,Debugging,长话短说:我在项目中使用的某些第三方非托管dll显然在运行时将其错误转储到Visual Studio输出窗口(显示“调试”的输出)。看起来是这样的: *** WARNING ERROR 11 from R3ORP. The degree 0 polynomial fit is a perfect *** fit within machine precision. Here is a traceback of subprogram calls in revers
*** WARNING ERROR 11 from R3ORP. The degree 0 polynomial fit is a perfect
*** fit within machine precision.
Here is a traceback of subprogram calls in reverse order:
Routine name Error type Error code
------------ ---------- ----------
R3ORP 6 11 (Called internally)
R4ORP 0 0 (Called internally)
R2ORP 0 0 (Called internally)
R2URV 0 0 (Called internally)
RCURV 0 0
USER 0 0
我想记录这些错误或在我的应用程序中显示它们。
是否可以通过某种方式从我的应用程序访问VS输出流?在没有附加VS的情况下,我仍然可以这样做吗?我不需要整个流,但我想以某种方式捕捉那些“警告”。我已尝试将侦听器添加到
Diagnostics.Debug
和Diagnostics.Trace
,但未成功 有!您可以实现自己的TraceListener类
首先编写TraceListener类:
class CustomTraceListener : TraceListener
{
public string AllMessages { get; set; }
public override void Write(string message)
{
AllMessages += message;
}
public override void WriteLine(string o)
{
Write(o + '\n');
}
public override string ToString()
{
return AllMessages;
}
}
当然,您可以在Write()
方法中实现自定义行为(如写入文本框等)
然后,您只需将类的实例添加到系统的调试跟踪侦听器中即可:
CustomTraceListener myTraceListener = new CustomTraceListener();
Debug.Listeners.Add(myTraceListener);
Debug.WriteLine("this is a test");
Debug.WriteLine("this is another test");
string result = myTraceListener.ToString();
这是一个非常有趣的问题 我用
System.Diagnostics.TraceListener
类找到了一个解决方案
sealed class StackOverflowSampleListener : TraceListener
{
// Singleton
public static readonly StackOverflowSampleListener Instance =
new StackOverflowSampleListener();
public void InitializeTracing(bool ReadDebugOutput)
{
if (ReadDebugOutput == true)
Debug.Listeners.Add(this);
else
Trace.Listeners.Add(this);
}
public StringBuilder Buffer = new StringBuilder();
public override void Write(string message)
{
// Do something with your messages!
Buffer.Append(message);
}
public override void WriteLine(string message)
{
// Do something with your messages!
Buffer.Append(message);
}
}
某些Form.cs代码中的示例:
public Form1()
{
InitializeComponent();
StackOverflowSampleListener.Instance.InitializeTracing(true);
StackOverflowSampleListener.Instance.Buffer.ToString();
}
GuyMontag的回答绝对正确,但我更喜欢一个更轻的版本,带有单例实现,并且可能在发布模式下收集消息(看看initialize methode) 如果这是一个非托管dll,并且输出显示在Visual Studio输出窗口中,则表示它正在使用函数写入数据 由于此数据仅用于调试,因此无法保证后续版本仍将输出此信息,或保持相同的格式。
如果您不关心这一点,可以在“c#capture debug output”上快速搜索。使用“我曾尝试将侦听器添加到
诊断中。debug
和Diagnostics.Trace
,但都不起作用。”显示使用您尝试的内容,因为这确实是一种方法。@ORdia,也不起作用(显然,输出窗口聚合了来自不同来源的消息,到目前为止,我似乎找不到产生这些错误的来源。但它不是,跟踪
,它不是调试
,也不是控制台
。谢谢你的回答。遗憾的是,正如我在问题中提到的,我已经尝试添加侦听器了到Diagnostics.Debug
和Diagnostics.Trace
-它不起作用。我的意思是,它确实捕获了使用这些类发送到输出的消息。但它显然没有捕获输出中的所有消息,包括我感兴趣的第三方非托管dll中的消息。感谢您的回答。正如我在一篇评论中解释的,这种方法对我不起作用(谢谢,我会研究一下。不,DebugView和侦听OutputDebugString的类都没有捕获到这些错误。>\u<我猜一定是其他原因。不过这是一篇有趣的文章。你能链接发出这些消息的组件吗?@NikitaBrizhak调试输出只能有一个订阅服务器,所以当你调试应用程序时,请使用该组件,Visual Studio将捕获输出。要解决此问题,您可以尝试手动捕获函数OutputDebugStringA和OutputDebugStringW的输出。不,我无法链接库本身,因为我无权分发它。但我相当肯定消息源自Intel Fortran代码>二进制文件,该库内部使用。我找不到任何信息,这将帮助我了解这些消息是如何发送的,或者VS是如何捕获它们的。