You’ve created your new ASP.NET Core web app and are happily building your features.

Then something breaks and you’ve no idea what.

How can you find out what’s causing the problem?

If you’ve created your ASP.NET Core web app from scratch (not using any templates or magic but bare-bones style using dotnet new) then you’ll find you have absolutely no error handling/logging set up. This makes diagnosing errors pretty difficult.

Shine a light on the problem

ASP.NET Core uses middleware to configure your application. You’ll use middleware to configure things like authentication, error handling and logging.

Add logging middleware to see what (if any) errors are being reported by your application.

Don’t forget to select the .NET Core SDK version you’re using before you follow along.

Let’s start with logging to the console.

Add a couple of dependencies to your app

dotnet add package Microsoft.Extensions.Logging -v 1.1.*

dotnet add package Microsoft.Extensions.Logging.Console -v 1.1.*

Make sure to restore them.

dotnet restore

Update Startup.cs to add the Console logger (via loggerFactory).

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
  loggerFactory.AddConsole();

  app.UseMvc();
  app.UseStaticFiles();

  app.Run(context =>
  {
    return context.Response.WriteAsync("Hello world");
  });
}

Now the console will show you what’s going on “under the hood”.

dotnet run

view-error-log

In this example .NET Core can’t find a specific view.

Microsoft Logging Extensions?

Microsoft.Extensions.Logging is a new common logging subsystem that makes it possible to use the same logging code but target different log providers.

Microsoft provide some basic loggers in Microsoft.Extensions. For example you can write to the Event Log.

microsoft-extensions-logging

As you can see, there is one for Slack. This is not provided by Microsoft (note the different icon).

You can bring in third party loggers as dependencies (or write your own). Here are some popular ones.

NLog

Link: NLog.Extensions.Logging

dotnet add package NLog.Extensions.Logging

Startup.cs

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{ 
  loggerFactory.AddNLog();

  // ...
}

Log4Net

Link: AspNet.log4net

dotnet add package Microsoft.Extensions.Logging.Log4Net.AspNetCore

Startup.cs

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{ 
  loggerFactory.AddLog4Net();

  // ...
}

Serilog

Link: Serilog.Extensions.Logging

dotnet add package Serilog.Extensions.Logging

Startup.cs

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
  loggerFactory.AddSerilog();

  // ...
}

In Summary

This is just the tip of the iceberg when it comes to logging in ASP.NET Core.

Next we’ll take a look at logging your own messages and how to log different levels (e.g. info/debug) depending on your environment.

photo credit: person-question via freeimages (license)

Want to learn .net Core?

  • Want to learn .NET Core but don't know where to start?
  • Don't have time to keep up with everything Microsoft is putting out?
  • Stuck on legacy apps when you want to build something new?
Enter your details below and get my weekly update; learn how to build better .NET web apps.
comments powered by Disqus