3

ASP Core 2 in IIS, Console Application in the project properties. Visual studio 2017 Community.

I'm trying to log to the console, but the console is not showing up at all.

The objective is to simply show up the console for logging purposes while having the browser connection.

I've configured the application in BuildWebHost so that:

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
         .ConfigureAppConfiguration((builderContext, config) =>
                  {
                      IHostingEnvironment env = builderContext.HostingEnvironment;
                   })
         .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddConsole();
                logging.AddDebug();
            })
         .UseNLog() 
         .UseStartup<Startup>()
         .Build();

To add further context: - I'm able to correctly log to file with NLog - I see the logging messages in the Output window in VS - I know how to send and see log messages into the browser.

I just want to fire the application in debug mode to visual studio and having the console windows opening up. Is that possible?

Running the application with dotnet-run from a command prompt is not a solution as I wouldn't have the edit and continue feature.

Normally, when you create a new ASP Core 2 Console Application, you can see the console by simply doing Console.WriteLine("Hello world"); in the Program method. In my app, however, this does not show up the console.

I know that the reason probably lies in the fact that the Console output is being redirected or not-existing; in fact, if I do Console.ReadKey(); I get the following:

 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.'

Any hint is appreciated.


A couple of references/test tried to no avail:

planetmaker
  • 5,884
  • 3
  • 28
  • 37
alelom
  • 2,130
  • 3
  • 26
  • 38

2 Answers2

6

If you want it as you debug in Visual Studio 2017, just open the Output window and look at the dropdown, you should see the ASP.NET Core commandline as an option.

The Console.Read isn't needed as it is all async (it stops the app from running probably).

If you're not on VS2017, then just run it from a command-line (dotnet run).

Shawn Wildermuth
  • 7,318
  • 3
  • 23
  • 28
  • 1
    I need the console to show up when I debug from visual studio, not opening the application from a command prompt. Otherwise I lose the Edit and Continue feature. I'll check again the asp option in the output window. – alelom Feb 15 '18 at 08:46
  • 5
    @alexlomba87: You missed Shawn's point. ASP.NET Core used to have an option in Visual Studio to load the app self-hosted (instead of using IIS Express), which would then cause a console window to open. However, this has been removed. Regardless, you can still see the console output in the Output pane in Visual Studio. There's a drop down there called "Show output from" and one of the choices is "ASP.NET Core Web server". Select this and you'll see all the console output. There is no other way now in Visual Studio. – Chris Pratt Feb 15 '18 at 13:57
  • Simple `Console.Writeline` showed up in the stdoutlog file for me. – Kunal Patel Jan 07 '20 at 17:47
  • Thank you for the simple answer! – johnw182 Jan 12 '23 at 21:50
0

We use that code to see detail on console. VS 2017, Wep Api

public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
      Configuration = configuration;
      HostingEnvironment = hostingEnvironment;

      if (HostingEnvironment.IsDevelopment())
      {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                .MinimumLevel.Override("System", LogEventLevel.Warning)
                .Enrich.FromLogContext()
                .WriteTo.Console()
                .CreateLogger();
     }
}

console screenshot

Ergin Çelik
  • 719
  • 7
  • 14