4

Exactly as what asked in "Writing to output window of Visual Studio?", however solving the remaining issue --

  • I assume there is no way to write to output if i start without debugging (ctrl-f5) right? – previous_developer Feb 27 '12

  • Debug.WriteLine() will only work when running in Debug. That means running it with F5 and not CTRL-F5. – kirk.burleson Jul 15 '13

However, it is not the case because:

  • Just ran a small app here, works fine for me. Perhaps there is a small glitch in your environment? – Bhargav Bhat Feb 27 '12

  • Moreover,

I can see some Informational printed in Visual Studio output window during normal run:

[1/10/2018 11:56:25 AM Informational] ------ Run test started ------
[1/10/2018 11:56:26 AM Informational] NUnit Adapter 3.9.0.0: Test execution started
[1/10/2018 11:56:26 AM Informational] Running selected tests in  ...\Demo.dll
[1/10/2018 11:56:26 AM Informational] NUnit3TestExecutor converted 14 of 14 NUnit test cases
[1/10/2018 11:56:45 AM Informational] NUnit Adapter 3.9.0.0: Test execution complete
[1/10/2018 11:56:45 AM Informational] ========== Run test finished: 1 run (0:00:19.3066647) ==========

For to run in debug mode, The NUnit debug output will look like:

[1/10/2018 2:56:55 PM Informational] ------ Run test started ------
[1/10/2018 2:56:56 PM Informational] NUnit Adapter 3.9.0.0: Test execution started
[1/10/2018 2:56:56 PM Informational] Debugging selected tests in ...\Demo.dll
[1/10/2018 2:56:57 PM Informational] NUnit3TestExecutor converted 14 of 14 NUnit test cases
[1/10/2018 3:03:38 PM Informational] NUnit Adapter 3.9.0.0: Test execution complete
[1/10/2018 3:03:38 PM Informational] ========== Run test finished: 1 run (0:06:43.6161412) ==========

I.e., regardless normal run or in debug mode, NUnit Adapter is able to write to the Visual Studio output window, the difference is only in the text "Running selected tests" or "Debugging selected tests"

So, here is the summary of debugging output cases during normal run, not in debug mode:

  • For C# Console, both Debug.WriteLine and Trace.WriteLine works fine. I've created and verified with this: https://pastebin.com/Du6ZbDV3
  • However for Class/Form etc that don't have Console, neither Debug.WriteLine nor Trace.WriteLine work. Example: https://pastebin.com/b7P0bYEa. "It is pretty simple but still nothing – previous-developer Feb 27 '12".
  • In NUnit testing, which is C# Class lib, neither Debug.WriteLine nor Trace.WriteLine works. I've run mine several times and can confirm this. I'll not be posting my test code here because it is lengthy & needs lots of libs to work, moreover, it is caused by the same glitch as https://pastebin.com/b7P0bYEa.

So all in all, How to write to Visual Studio output window from class lib or win forms?

InteXX
  • 6,135
  • 6
  • 43
  • 80
xpt
  • 20,363
  • 37
  • 127
  • 216
  • I suggest unit testing is a separate issue and depends on the test runner you're using. Some test runners may write info back to visual studio and some may not. And WPF/Winforms apps using Trace do write to VS output console in debug mode, just tested it. (Using VS 2017). Running without debugging has no actual connection to Visual Studio so I'd be surprised if there was a way for the output window to display something from the application. – Alex Paven Jan 10 '18 at 17:43
  • Oh, in that case, I'd more interested in the unit testing -- In my NUnit testing, I did some `Debug.WriteLine` myself, and need that to be in the VS output window, even when just run, not debugging (when debugging the output is fine). OK. I'll provide a small sample then. – xpt Jan 10 '18 at 18:50
  • Oh, just re-read your comment @AlexPaven -- you meant that Trace writes to VS output console "_in debug mode_", but that is not what I'm asking. I _know_ running in debug mode is fine. I'm asking the _normal run_, CTRL-F5 -- "***Debug.WriteLine() will only work when running in Debug. That means running it with F5 and not CTRL-F5.***" Try my small C# Console app then you'd know. – xpt Jan 10 '18 at 18:53
  • You're asking to do something that the IDE is not designed to do. Perhaps starting a new question explaining what you are trying to accomplish, instead of how you want to accomplish it, would prove more useful. – Sam Axe Jan 11 '18 at 06:24
  • _Writing to Visual Studio output window_ during normal run IS I want to accomplish. Please note that, "_regardless normal run or in debug mode, NUnit Adapter is able to write to the Visual Studio output window_". – xpt Jan 11 '18 at 16:05

1 Answers1

6

To using both Debug.WriteLine and Trace.WriteLine we need add using System.Diagnostics, which only works when it’s debugging (F5). Even it’s a C# Console project we still cannot get Debug.WriteLine and Trace.WriteLine output when Start without Debugging (Ctrl + F5), to see the output we have to run it in Debugging (F5).

For Class library project, it could only be built or compiled, I don’t think we could Start or run it as it’s not a console project unless it’s been called in a console project.

UPDATE:

To debug a class library project and show its output, I add a unit test in my solution in VS. My class library code:

    namespace ClassLibrary1
{
   public class hello
    {
        public static void foo()
        {
            Console.WriteLine("Hi, this is a console writeline");
            Debug.WriteLine("Hi, this is a Debug writeline");
        }
    }
}

In unit test project I added the ClassLibrary1 to the Reference

and my unite test project code is simple:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary1;


namespace UnitTestProject3
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            hello.foo();

        }
    }
}

Then after running the test method i could get the output: enter image description here

Fletcher
  • 422
  • 1
  • 6
  • 21
  • Thanks @Fletch, I've updated my OP with, "_regardless normal run or in debug mode, NUnit Adapter is able to write to the Visual Studio output window_", would you comment on how that is possible please? – xpt Jan 11 '18 at 16:03
  • @xpt Well, as it's a test project no matter unit test or coded UI test project, when we Run Tests, the output is from Test, because the project's got Test Adapter(MSTest.Adapter or NUnit Adapter in your side ). Of course, we could also Debug Tests, and see the output from Debug. Visual Studio allows us to debug tests. – Fletcher Jan 12 '18 at 02:30
  • thanks for explaining @Fletch. For Class library project, because there might not be a console associated with it, but if I do `Debug.WriteLine` *within* the Class library code, where would such writes write to? How can I make sure it will always works, regardless normal run or in debug mode? – xpt Jan 12 '18 at 13:16
  • @xpt We need add an executable project to the solution which references the library project and set the executable project as the startup project. We also could add a unit test, and I will take it as an example and update it in my answer. – Fletcher Jan 15 '18 at 06:20
  • A~~h~~, there! Thanks Fletch. I'd never had found it there myself. :) – xpt Jan 15 '18 at 15:40