visual-studio 非调试模式时在 Visual Studio 的输出窗口中显示消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/562590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:10:35  来源:igfitidea点击:

Display a message in Visual Studio's output window when not debug mode?

visual-studio

提问by Frank

In Java, you can use System.out.println(message)to print a message to the output window.

在 Java 中,您可以使用System.out.println(message)将消息打印到输出窗口。

What's the equivalent in Visual Studio ?

Visual Studio 中的等价物是什么?

I know when I'm in debug mode I can use this to see the message in the output window:

我知道当我处于调试模式时,我可以使用它来查看输出窗口中的消息:

Debug.WriteLine("Debug : User_Id = "+Session["User_Id"]);
System.Diagnostics.Trace.WriteLine("Debug : User_Id = "+Session["User_Id"]);

How can this be done without debugging in Visual Studio?

如果不在 Visual Studio 中进行调试,如何做到这一点?

回答by Jos

The results are not in the Output window but in the Test Results Detail (TestResult Pane at the bottom, right click on on Test Results and go to TestResultDetails).

结果不在“输出”窗口中,而是在“测试结果详细信息”中(底部的“测试结果窗格”,右键单击“测试结果”并转到“测试结果详细信息”)。

This works with Debug.WriteLine and Console.WriteLine.

这适用于 Debug.WriteLine 和 Console.WriteLine。

回答by Frederik Gheysels

The Trace messages can occur in the output window as well, even if you're not in debug mode. You just have to make sure the the TRACE compiler constant is defined.

即使您未处于调试模式,Trace 消息也可能出现在输出窗口中。您只需要确保定义了 TRACE 编译器常量。

回答by JaredPar

The Trace.WriteLine method is a conditionally compiled method. That means that it will only be executed if the TRACE constant is defined when the code is compiled. By default in Visual Studio, TRACE is only defined in DEBUG mode.

Trace.WriteLine 方法是一种条件编译的方法。这意味着只有在编译代码时定义了 TRACE 常量,才会执行它。默认情况下,在 Visual Studio 中,TRACE 仅在调试模式下定义。

Right Click on the Project and Select Properties. Go to the Compile tab. Select Release mode and add TRACE to the defined preprocessor constants. That should fix the issue for you.

右键单击项目并选择属性。转到编译选项卡。选择 Release 模式并将 TRACE 添加到定义的预处理器常量。那应该可以为您解决问题。

回答by XyberICE

This whole thread confused the h#$l out of me until I realized you have to be running the debugger to see ANY trace or debug output. I needed a debug output (outside of the debugger) because my WebApp runs fine when I debug it but not when the debugger isn't running (SqlDataSource is instantiated correctly when running through the debugger).

整个线程把 h#$l 弄糊涂了,直到我意识到你必须运行调试器才能看到任何跟踪或调试输出。我需要一个调试输出(在调试器之外),因为我的 WebApp 在调试时运行良好,但在调试器未运行时运行良好(SqlDataSource 在通过调试器运行时正确实例化)。

Just because debug output can be seen when you're running in release mode doesn'tmean you'll see anything if you're not running the debugger. Careful reading of Writing to output window of Visual Studio?gave me DebugViewas an alternative. Extremely useful!

仅仅因为在发布模式下运行时可以看到调试输出并不意味着如果您没有运行调试器,您将看到任何东西。仔细阅读写到 Visual Studio 的输出窗口?给了我DebugView作为替代方案。非常有用!

Hopefully this helps anyone else confused by this.

希望这可以帮助其他人对此感到困惑。

回答by Liam

For me this was the fact that debug.writeline shows in the Immediate window, not the Output. My installation of VS2013 by default doesn't even show an option to open the Immediate window, so you have to do the following:

对我来说,这是 debug.writeline 显示在立即窗口中的事实,而不是输出。默认情况下,我安装的 VS2013 甚至没有显示打开立即窗口的选项,因此您必须执行以下操作:

Select Tools -> Customize 
Commands Tab
View | Other Windows menu bar dropdown
Add Command...
The Immediate option is in the Debug section.

Once you have Ok'd that, you can go to View -> Other Windows and select the Immediate Window and hey presto all of the debug output can be seen.

确定后,您可以转到“查看”->“其他窗口”并选择“立即窗口”,然后就可以看到所有调试输出。

Unfortunately for me it also showed about 50 errors that I wasn't aware of in my project... maybe I'll just turn it off again :-)

不幸的是,它也显示了大约 50 个我在我的项目中不知道的错误......也许我会再次关闭它:-)

回答by Ionut Enache

To write in the Visual Studio output window I used IVsOutputWindowand IVsOutputWindowPane. I included as members in my OutputWindowclass which look like this :

要在我使用的 Visual Studio 输出窗口中写入IVsOutputWindowIVsOutputWindowPane. 我作为成员加入了我的OutputWindow班级,如下所示:

public class OutputWindow : TextWriter
{
  #region Members

  private static readonly Guid mPaneGuid = new Guid("AB9F45E4-2001-4197-BAF5-4B165222AF29");
  private static IVsOutputWindow mOutputWindow = null;
  private static IVsOutputWindowPane mOutputPane = null;

  #endregion

  #region Constructor

  public OutputWindow(DTE2 aDte)
  {
    if( null == mOutputWindow )
    {
      IServiceProvider serviceProvider = 
      new ServiceProvider(aDte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
      mOutputWindow = serviceProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
    }

    if (null == mOutputPane)
    {
      Guid generalPaneGuid = mPaneGuid;
      mOutputWindow.GetPane(ref generalPaneGuid, out IVsOutputWindowPane pane);

      if ( null == pane)
      {
        mOutputWindow.CreatePane(ref generalPaneGuid, "Your output window name", 0, 1);
        mOutputWindow.GetPane(ref generalPaneGuid, out pane);
      }
      mOutputPane = pane;
    }
  }

  #endregion

  #region Properties

  public override Encoding Encoding => System.Text.Encoding.Default;

  #endregion

  #region Public Methods

  public override void Write(string aMessage) => mOutputPane.OutputString($"{aMessage}\n");

  public override void Write(char aCharacter) => mOutputPane.OutputString(aCharacter.ToString());

  public void Show(DTE2 aDte)
  {
    mOutputPane.Activate();
    aDte.ExecuteCommand("View.Output", string.Empty);
  }

  public void Clear() => mOutputPane.Clear();

  #endregion
}

If you have a big text to write in output window you usually don't want to freeze the UI. In this purpose you can use a Dispatcher. To write something in output window using this implementation now you can simple do this:

如果您要在输出窗口中写入大文本,您通常不想冻结 UI。为此,您可以使用Dispatcher. 要使用此实现在输出窗口中编写一些内容,您现在可以简单地执行以下操作:

Dispatcher mDispatcher = HwndSource.FromHwnd((IntPtr)mDte.MainWindow.HWnd).RootVisual.Dispatcher;

using (OutputWindow outputWindow = new OutputWindow(mDte))
{
  mDispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  {
    outputWindow.Write("Write what you want here");
  }));
}