Emre Kızıldaş
Published in

Emre Kızıldaş

Centralized logging in .Net Core using AWS Elasticsearch Service — Serilog & Kibana

This article will continue only English language. Sorry for my Turkish followers.

Hello everyone. I will talk about logging architecture on a .Net Core web application and use AWS Elasticsearch services. Let’s begin!

First of all we’re going to create an Elasticsearch service on our Amazon Web Services account. You should click this address after login your account.

AWS Elasticsearch Service Setup

In this screen, we’ll choose deployment type and Elasticsearch version. When I was writing this article AWS Elasticsearch Service was support maximum Elasticsearch 6.7 version.

And then you need configure your cluster. You can give an Elasticserach domain name, for example elastic. I think if you have a small web application you should choose t2.small.elasticsearch instance type. Other cluster settings may remain the default.

We’ll use public access out Kibana and Elasticsearch Service for this example. And our access policy is public access. Because I don’t want to deal with authorization.

Our access policy set with public access.

Finally we look our Elasticsearch Service setting summary and then confirm this settings.

After about 10 minutes your service will be activated. You can look your Kibana access point or Elastic endpoint.

Amazon Web Services Security Credentials

In order to use the services, we will need the security keys of our AWS Account. You can get your security credentials with Account > My Security Credentials > Access Keys Tab > Create New Access Key button. You should keep your access key and secret key.

Let’s start .Net Core web application

We need to install some NuGet packages in our project for logging. These packages are:

You should be careful that versions when you downloading packages.

We want logging every action on our website. First of all we need configure our Startup.cs class and setup AWS credentials on our project.

I want keep my AWS Credentials keys in appsettings.json file. For example:

"AWS": {"AccessKey": "AKIAI2*********5FPA","SecretKey": "FkasJzg*********pLnxf30u/FkkO*****fO","Region": "us-east-1","ElasticUrl": "https://search-elastic-*********************.us-east-1.es.amazonaws.com"}

After that we’ll set up AWS credentials in our constructor of Startup class.

Now, we’ll configure our Elasticsearch connection settings. We’ll use Serilog library and our index name will be emrelog-yyyy.MM.

You can change your index name with set IndexFormat parameter.

Here is finally our Startup class. And we’re ready for logging every action on our website.

Create Action Logger Attribute

We need use attribute tags on our controller classes for watching every action. Now, create a new folder on your project and set that folder name to Infrastructure. And then create a class in your infrastructure file. The class name set to ActionLoggerAttribute.

This class inherit of ActionFilterAttribute class. This attribute class give us watch every action on when we use that. It has some override methods. We’ll use OnActionExecuted method.

public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
}

And here you need to choose what you would like to know in your log item. For example I want know action and controller name and user IP address.

System.Net.IPAddress ipAddress = context.HttpContext.Connection.RemoteIpAddress;string controllerName = ((ControllerActionDescriptor)context.ActionDescriptor).ControllerName;string actionName = ((ControllerActionDescriptor)context.ActionDescriptor).ActionName;

Now, we’ll create a log object and then we’ll use this information on our log item. You can use ForContext method with your log object. This method can help you what do you want see on your log items.

And after that you should choose your log item level. For example Error, Information, Verbose etc. We’ll choose Information and Error log level.

Use ActionLog Attribute

You should use this attribute on your controller class.

using AWSElasticSearchNetCore.Infrastructure;[ActionLogger]public class HomeController : Controller

And here’s the controller class:

Start your project and look this errorless page :)

Configure Kibana Service

Open your Kibana service. The Kibana service link will be available on your Elasticsearch service page.

Kibana start page

After that open Management tab and press Create Index Pattern button.

You should write the index name that we set in the Startup class to the entry here. If you see Success! message then you should press next step button.

Finally, choose time filter type. I think you should choose timestamp field name. And then click Create index pattern button.

Now click Discover tab on left side menu. And you’ll see this screen:

Wow! Here is our log!

Thanks you so much for reading my article. And sorry about my bad English grammar. You can see all of these project codes on my Github repository.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store