How to use Console.WriteLine in ASP.NET (C#) during debug?

后端 未结 7 817
误落风尘
误落风尘 2020-12-22 21:51

I want to write some result to the console in ASP.NET (C#). It works in a Window application, but a Web application does not work. Here is what I have tried:



        
相关标签:
7条回答
  • 2020-12-22 22:06

    Use response.write method in the code-behind.

    0 讨论(0)
  • 2020-12-22 22:13

    Make sure you start your application in Debug mode (F5), not without debugging (Ctrl+F5) and then select "Show output from: Debug" in the Output panel in Visual Studio.

    0 讨论(0)
  • 2020-12-22 22:16

    If for whatever reason you'd like to catch the output of Console.WriteLine, you CAN do this:

    protected void Application_Start(object sender, EventArgs e)
    {
        var writer = new LogWriter();
        Console.SetOut(writer);
    }
    
    public class LogWriter : TextWriter
    {
        public override void WriteLine(string value)
        {
            //do whatever with value
        }
    
        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }
    }
    
    0 讨论(0)
  • 2020-12-22 22:17

    Trace.Write("Error Message") and Trace.Warn("Error Message") are the methods to use in web, need to decorate the page header trace=true and in config file to hide the error message text to go to end-user and so as to stay in iis itself for programmer debug.

    0 讨论(0)
  • 2020-12-22 22:23

    using System.Diagnostics;

    The following will print to your output as long as the dropdown is set to 'Debug' as shown below.

    Debug.WriteLine("Hello, world!");


    enter image description here

    0 讨论(0)
  • 2020-12-22 22:23

    You shouldn't launch as an IIS server. check your launch setting, make sure it switched to your project name( change this name in your launchSettings.json file ), not the IIS.

    0 讨论(0)
提交回复
热议问题