4

Troubleshoot your ASP.NET Core web app using logging

This article refers to project.json which Microsoft has deprecated in favour of a revamped .csproj file.

A new article will be published shortly and I’ll put a link here.

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.

Let’s start with logging to the console.

Add a couple of dependencies to project.json.

      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.AspNetCore.Mvc": "1.0.0",
        "Microsoft.AspNetCore.Mvc.Core": "1.0.0",
        "Microsoft.AspNetCore.StaticFiles": "1.0.0",
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0"
      },

Make sure to restore them.

dotnet restore

Update Startup.cs.

        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

project.json

 "dependencies": {
    "NLog.Extensions.Logging": "1.0.0-*"
  }

Startup.cs

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

            // ...
        }

Log4Net

Link: AspNet.log4net

Note: This currently only works if you’re targeting the full .NET framework (keep checking the link above to find out when this changes).

project.json

 "dependencies": {
    "dotnetliberty.AspNet.log4net": "1.0.0-beta8"
  }

Startup.cs

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

            // ...
        }

Serilog

Link: Serilog.Extensions.Logging

project.json

 "dependencies": {
    "Serilog.Extensions.Logging": "1.0.0"
  }

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)