Nice programing

디버그 모드가 아닐 때 Visual Studio의 출력 창에 메시지를 표시합니까?

nicepro 2020. 12. 25. 22:56
반응형

디버그 모드가 아닐 때 Visual Studio의 출력 창에 메시지를 표시합니까?


Java에서는을 사용 System.out.println(message)하여 출력 창에 메시지를 인쇄 할 수 있습니다 .

Visual Studio에서 동등한 것은 무엇입니까?

디버그 모드에있을 때 이것을 사용하여 출력 창에서 메시지를 볼 수 있다는 것을 알고 있습니다.

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

Visual Studio에서 디버깅하지 않고 어떻게이 작업을 수행 할 수 있습니까?


결과는 출력 창이 아니라 테스트 결과 세부 정보에 있습니다 (하단의 테스트 결과 창에서 테스트 결과를 마우스 오른쪽 버튼으로 클릭하고 TestResultDetails로 이동).

이것은 Debug.WriteLine 및 Console.WriteLine에서 작동합니다.


Trace 메시지는 디버그 모드가 아닌 경우에도 출력 창에서 발생할 수 있습니다. TRACE 컴파일러 상수가 정의되어 있는지 확인하기 만하면됩니다.


Trace.WriteLine 메서드는 조건부로 컴파일 된 메서드입니다. 즉, 코드가 컴파일 될 때 TRACE 상수가 정의 된 경우에만 실행됩니다. 기본적으로 Visual Studio에서 TRACE는 DEBUG 모드에서만 정의됩니다.

프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 선택합니다. 컴파일 탭으로 이동합니다. 릴리스 모드를 선택하고 정의 된 전 처리기 상수에 TRACE를 추가하십시오. 그러면 문제가 해결 될 것입니다.


이 전체 스레드 는 모든 추적 또는 디버그 출력을 보려면 디버거를 실행해야 한다는 것을 깨달을 때까지 h # $ l을 혼란스럽게했습니다 . 디버그 출력 (디버거 외부)이 필요했습니다. 내 웹 애플리케이션은 디버깅 할 때 제대로 실행되지만 디버거가 실행되지 않을 때는 실행되지 않기 때문입니다 (디버거를 통해 실행할 때 SqlDataSource가 올바르게 인스턴스화 됨).

릴리스 모드에서 실행할 때 디버그 출력을 볼 수 있다고 해서 디버거를 실행하지 않으면 아무것도 볼 수 없다는 의미는 아닙니다. Visual Studio의 출력 창쓰기를 주의 깊게 읽으 십니까? DebugView 를 대안으로 주었습니다 . 매우 유용합니다!

바라건대 이것은 다른 사람들이 이것으로 혼란스러워하는 데 도움이되기를 바랍니다.


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
}

출력 창에 쓸 큰 텍스트가있는 경우 일반적으로 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");
  }));
}

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:

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 :-)

ReferenceURL : https://stackoverflow.com/questions/562590/display-a-message-in-visual-studios-output-window-when-not-debug-mode

반응형