UWP application Debug mode outputs debug information in the Output window.

QUNZHI, OUYANG 1 Reputation point
2022-08-10T01:35:14.14+00:00

When I tested the UWP application I created, I found that I could not print debug information to the Output window in Debug mode, I used "System.Diagnostics.Debug.WriteLine("Debug output info.")" and "OutputDebugString("Debug output info.")" is invalid, but after changing to Release mode, the "OutputDebugString("Debug output info.")" method can print debugging information to the Output window. I haven't found a suitable explanation on the Internet, so I am looking for help here. .

My test code is as follows:

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Myapp.SDKTest

{

    /// <summary>

    /// An empty page that can be used on its own or navigated to within a Frame.

    /// </summary>

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

            this.Loaded += MainPage_Loaded;

        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)

        {

            System.Diagnostics.Debug.WriteLine("Debug output info.");

            UnsafeNativeMethods.OutputDebugString("Debug output info.");

        }

    }

    /// <summary>

    /// Call native method to write debug trace.

    /// </summary>

    internal static class UnsafeNativeMethods

    {

        //http://msdn.microsoft.com/en-us/library/windows/apps/aa363362(v=vs.85).aspx

        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]

        internal static extern void OutputDebugString(string lpOutputString);

    }

}

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,239 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-08-11T08:18:38.49+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I've made a test in a C++/CX UWP application, the OutputDebugString function works correctly in both debug and release mode. Then I tried other applications like C# WPF applications and C# console applications. The win32 function doesn't work in these applications in both debug and release mode. So this might be an issue related to the C# language. But the reason for this behavior is still unclear.

    Now a workaround for this scenario is that you could use a macro to run different debug functions in different modes. When running in debug mode, the app will automatically use the System.Diagnostics.Debug.WriteLine function. When running in release mode, the app will run OutputDebugString function instead.

    Like this:

      private void Button_Click(object sender, RoutedEventArgs e)  
            {  
    #if DEBUG  
                 // Debug work here  
                 System.Diagnostics.Debug.WriteLine("Debug output info.");  
    #else  
                // Release work here  
                OutputDebugString("Debug output info.");  
    #endif  
      
            }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments