Denis535
Denis535

Reputation: 3590

Asp.net core + IIS Express. How to view log messages?

I'm trying to print a log message in ASP.NET Core this way:

Console.WriteLine( "Hello World!" );

loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole( LogLevel.Debug );
var logger = loggerFactory.CreateLogger("Startup");
logger.LogWarning( "Hi!" );

Where can I see this message?

The Output window doesn't contain this message.

If I run the project as Web, it runs the dnx console where I can see the message, but it is not convenient.

If I run project as IIS Express I don't see the console, or the message.

Is there way to view the message in Visual Studio?

Upvotes: 10

Views: 14559

Answers (4)

Faruq
Faruq

Reputation: 1521

None of the other answers addresses the actual question: "How can we see those messages when running the project with IIS Express?". So, I decided to add my own, I hope it helps somebody.

The usual console window does not show up when you choose IIS Express. But you can still see those messages by following the below steps.

  1. Look for the Output window in Visual Studio. If you already have it, skip the next step
  2. Goto View-menu > Output or press Ctrl + W,O. The output window will show up.
  3. In the output window, change the dropdown to "<-your-project-name-> ASP.NET Core Web Server" or something similar.
  4. You should be able to see those messages here.

enter image description here

I am using VS 2019, the above steps can differ based on your IDE version.

Upvotes: 10

K&#233;vin Chalet
K&#233;vin Chalet

Reputation: 42010

You can use the Debug listener to achieve what you want:

"dependencies": {
  "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final"
}

Console.WriteLine( "Hello World!" );

loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddDebug( LogLevel.Debug );
var logger = loggerFactory.CreateLogger("Startup");
logger.LogWarning( "Hi!" );

Upvotes: 5

Bruno Garcia
Bruno Garcia

Reputation: 6398

IIS Express will not output its logs to Visual Studio's output window.

You'll need to use a different log provider.

Write it to a log file. You can use a library for that, for example Serilog:

using Serilog;

public class Startup
{
  public Startup(IHostingEnvironment env)
  {
      Log.Logger = new LoggerConfiguration()
          .WriteTo.File("log.txt")
          .CreateLogger();
...
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                        ILoggerFactory loggerFactory)
  {
      loggerFactory.AddSerilog();

You'll need this NuGet package.

Upvotes: 4

Janshair Khan
Janshair Khan

Reputation: 2687

Go to Documents => IISExpress => Logs.

This is what you are looking for.

Upvotes: 2

Related Questions