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

后端 未结 7 2244
说谎
说谎 2020-12-22 21:40

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

    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; }
        }
    }
    

提交回复
热议问题