Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to Visual Studio output window, the sure-fire way

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?

like image 580
xpt Avatar asked Oct 21 '25 14:10

xpt


1 Answers

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

like image 104
Fletcher Avatar answered Oct 23 '25 03:10

Fletcher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!