Mistral logo

Loggly In ASP.NET Core Using Serilog

09.04.2018 - READING TIME: 6 minutes

Loggly In ASP.NET Core Using Serilog

Author: Adnan Mulalić 


L (7)

I have spent some time trying to implement Loggly using Serilog in ASP.NET Core app and I want to share knowledge. I hope it will save someone some time :)
You can download completed code here: https://github.com/stensolino/LogglySolutions

So lets start!

The first step is to create a new solution. Choose ASP.NET Core Web Application, enter solution and project names and click OK.

L (8)

The next step is to choose project template. We will use API as a template. Choose it and click OK.

L (9)

Default integrated logger

Before we proceed forward, a few words about default logger included with ASP.NET Core API project. When we created the new project using ASP.NET Core API template, Programs.cs file is created, and in it we can find WebHost.CreateDefaultBuilder(args) function which will be invoked on application start. Besides other stuff, the function will register default logger. By default, Console and Debug provider will be enabled and configuration will be read from appsettings.json file. In-app settings.json file you can see that configuration is set to “Warning” log level. For our test case change it to “Information” level.

To use this default logger we only need to add it to constructor where we want to use it. For example, we can use ValuesController created on project initialization. Open ValuesController and add ILogger private read-only property and append it to the constructor.

L (10)

It’s time to run the application. If the application is run with IIS Express profile, our log can be tracked in Output screen inside Visual Studio. For our example run it with “LogglySolutions.Api” profile

L (11)

This way the application will start with the opening console where we can track our log:

L (12)

Here we can see some default log output and our log from ValuesController.

To disable those logs we can set LogLevel to “None” in appsettings.json. Be aware that under the appsettings.json file you can find the appsettings.Development.json file which will override settings in appsettings.json when we run the application in Development mod. The override will happen only for those settings which are in the appsettings.Development.json, it will not replace the file.

Disabling default integrated logger

Before proceeding with installation and implementation of Serilog we will disable the default logger. It’s not mandatory but there is no reason to have two loggers enabled.

Because the default logger is implemented by invoking WebHost.CreateDefaultBuilder(args) method we have to remove all providers. Open Program.cs and edit BuildWebHost method to looks as:

L (13)

You also could replace the WebHost.CreateDefaultBuilder(args) method and configure the host on your own, and omit to add the default logger, but this is out of scope of this article. Anyway, the code above will do the job, disable the default logger.

Now you can also remove logger config from appsettings.json file. After removing, our app settings.json should contain only empty json: {}. Don’t forget to clear app settings.Development.json also.

If you run the app now, you will see that there are no logs.

Installing Serilog

Now it’s time to install Serilog NuGet packages. The first NuGet package which we need is Serilog.AspNetCore. You can install it using Visual Studio or by a command:

L (14)

After installing the base Serilog package, next step is to install Serilog Sinks providers. There are many options available. We will go forward with Serilog.Sinks.Console, Serilog.Sinks.File and Serilog.Sinks.Loggly. Again we can use Visual Studio or the command:

L (15)

Serilog Console Provider Implementation

We have prepared Serilog for implementation. We will start from the Program.cs file.

L (16)

Here we run BuildWebHost(args) and assign the result to webHost. In building web host we assign values to the _environmentName variable which we use in building the configuration to read the appsettings.json file specific environment. We also configure Log.Logger with the configuration from appsettings.json. At the end we run web host.

The next step is to add the logger configuration to the appsettings.json file:

L (17)

The important part is the WriteTo where we add all providers we want to use. Later we will add File and Loggly provider here also.

Just before we run the app to test the new logger we just have to edit ValuesController.cs to remove the old and add the new logger. It should look like below:

L (18)

If we run the application we should see the logs in the console:

L (19)

Serilog File Provider Implementation

Adding the File Provider is straightforward, we just need to add it to the appsettings.json file. Here is the updated appsettings.json :

L (20)

As you can see we just added new provider to the “WriteTo” list. In one of previous steps we already have installed Serilog RollingFile provider NuGet package. As “Args” we provide “pathFormat”, path where our file will be saved and “outputTemplate” which defines format in which logs will be written.

Run the app and you can go to the path (in my case C:\LogglySolutions\) and you should find the new log file.

Finally, Implementing Serilog Loggly Provider

As I implement Console and File provider I have expected that Loggly provider will have the same implementation procedure, but at the end there is little more work to do. At the moment of writing this article Serilog Loggly provider was not configurable by the appsettings.json, it could be enabled here by adding it to the provider list but the feature configuration has to be done through code.

To implement and test Loggly provider you need to have a loggly account. You can get one for free.

The first step is to prepare configuration for Loggly. As already mentioned, we can just enable Loggly provider in the appsettings.json file, just add:

L (21)

in the provider's list where the Console and the RollingFile providers are.

We will also need additional settings for the loggly, we can add those additional settings to the appsetting.json, but we will need to read it manually. Here is the complete final appsettings.json file, just replace “placeholder” with your values:

L (22)

As you can see the Loggly section is placed under Serilog. That is not required, you can add it at root level but I just want to keep it organized.

I will create a class (LogglySettings) in which the Loggly configuration will be loaded. I also created a new folder Settings in the root of the project and placed LogglySettings.cs in it.

L (23)

Now open Program.cs and add bind Loggly configuration to LogglySettings class:

L (24)

You can do this in other ways also, but I prefer this way. You can even enter Loggly values inside the code. It’s all up to you.

The last step is to configure Loggly provider. We will create private method SetupLogglyConfiguration in the Program class and call that method from the Main method. Here is the complete class:


The SetupLogglyConfiguration method is actually configuration through the code, not automatically from settings file but in future this will probably be possible to do from the settings file. We just use logglySettings which we created earlier to set values.

That’s all. Now you can run the app and you should see the log result in your loggly account:

L (3)

Thank you for taking the time to read this and please leave any (positive and negative) comments. In that way we are contributing to our great community and we all get some benefit.

I wish you a great day!