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:
protected void btonClick_Click(object sender, EventArgs e)
{
Console.WriteLine("You click me ...................");
System.Diagnostics.Debug.WriteLine("You click me ..................");
System.Diagnostics.Trace.WriteLine("You click me ..................");
}
But I see nothing in the Output panel. How do I solve this problem?
This question is related to
c#
asp.net
visual-studio-2010
console
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.
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.
Use response.write method in the code-behind.
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; }
}
}
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!");
Source: Stackoverflow.com