Loggly in ASP.NET Core using Serilog

Adnan Mulalić
ITNEXT
Published in
6 min readMar 22, 2018

--

Click here to share this article on LinkedIn »

I have spend 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!

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

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

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. Beside 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 appsettings.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.

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.

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

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. Override will happen only for those settings which are in the appsettings.Development.json, it will not replace 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:

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, config) =>
{
config.ClearProviders();
})

.UseStartup<Startup>()
.Build();

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

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

If you run the app now, you will see that there is 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 command:

Install-Package Serilog.AspNetCore -DependencyVersion Highest

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:

Install-Package Serilog.Sinks.Console
Install-Package Serilog.Sinks.File
Install-Package Serilog.Sinks.Loggly

Serilog Console Provider Implementation

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

Here we run BuildWebHost(args) and assign 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:

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:

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

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 :

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 implemente 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:

{
"Name": "Loggly"
}

in the providers 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:

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.

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

var logglySettings = new LogglySettings();
configuration.GetSection("Serilog:Loggly").Bind(logglySettings);

You can do this in other ways also, but I prefer this way. You can even enter Loggly values inside 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 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:

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!

--

--