Part Three -Building API Gateway Using Ocelot In ASP.NET Core - Logging

Introduction

In the previous articles of this series, we discussed how to build the API Gateway in ASP.NET Core.

And in this article, we will discuss the Logging module of Ocelot.

If you want to look at the previous articles of those series, please visit the links given below.

Why logging is so important

Logging is an essential part of software development. It provides developers and support teams with special glasses which enable them to see what their code is really doing.

Developers also need to find problems in their applications during and after development. Enough log messages can help us to localize problems easily.

There is no doubt that API Gateway needs logging as well.

Ocelot uses the standard logging interfaces.And it has some default implementation for the standard asp.net core logging. It helps us to understand logging modules quickly!!

Now, let's begin with a simple sample.

Step 1

Create two projects here.

Project Name Project Type Description
APIGateway ASP.NET Core Empty This is the entry of this demo.
CustomersAPIServices ASP.NET Core Web API This is an API Service that handles something about customers.

And, make API Gateway and CustomerAPIServices project the same as a basic demo at first.

Step 2

Add the following configuration in the appsettings.json.

  1. "Logging": {  
  2.     "IncludeScopes"true,  
  3.     "LogLevel": {  
  4.         "Default""Trace",  
  5.         "System""Information",  
  6.         "Microsoft""Information"  
  7.     }  
  8. }  

Step 3

Modify the startup class configure method to enable the console logging with above configuration.

  1. public void Configure(IApplicationBuilder app ,ILoggerFactory loggerFactory)  
  2. {  
  3.     //console logging  
  4.     loggerFactory.AddConsole(Configuration.GetSection("Logging"));  
  5.   
  6.     app.UseOcelot().Wait();  
  7. }  

After finishing this step, when we run the API Gateway and visit some of the services, you may get some information that looks like the following picture!

ASP.NET Core

Based on the above screenshot, we can find out that all of the requests are recorded in the console.

We can analyze our applications that are based on those messages. Most of the time, we do not want the messages to write to the console. We need to store them so that we can find out of them every time, not for an instant.

The next step, I will use NLog, a free logging platform with rich log routing and management capabilities to make the log messages persist.

Step 4

Installing NLog.Web.AspNetCore for the project at first. Next, we need a configuration file that is required by NLog. It helps to configure NLog logging options.

  1. <?xml version="1.0" encoding="utf-8" ?><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"  
  2.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.       autoReload="true"  
  4.       internalLogLevel="Warn"  
  5.       internalLogFile="internal-nlog.txt">  
  6.   
  7.   <targets>      
  8.     <target xsi:type="File"   
  9.     name="debug"   
  10.     fileName="debug-${shortdate}.log"  
  11.         layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" />                  
  12.   </targets>  
  13.   
  14.   <rules>      
  15.     <logger name="*" minlevel="Debug" writeTo="debug" />  
  16.   </rules>  
  17. </nlog>  

Note
In the configuration file, I just write the log message into files. There are many types that you can choose to write based on you requirements, such as Elasticsearch, PostgreSQL, and so on.

Then, we need to modify the startup class configuration method to enable NLog and add the configuration options.

  1. public void Configure(IApplicationBuilder app ,ILoggerFactory loggerFactory)  
  2. {  
  3.     //console logging  
  4.     loggerFactory.AddConsole(Configuration.GetSection("Logging"));  
  5.   
  6.     //nlog logging  
  7.     loggerFactory.AddNLog();  
  8.     loggerFactory.ConfigureNLog("nlog.config");  
  9.   
  10.     app.UseOcelot().Wait();  
  11. }  

After you run up the API Gateway and visit the service, NLog will generate a log file. We can use cat common to see the content of the log file.

ASP.NET Core

Now, we have done the job.

Here is the source code you can find on my GitHub page.

Summary

This article introduced how to use logging module in Ocelot, both the basic and third party components.

We can use this module the same as regular ASP.NET Core logging benefit from the design and implementation of Ocelot project. Hope this article can help you.