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.
- "Logging": {
- "IncludeScopes": true,
- "LogLevel": {
- "Default": "Trace",
- "System": "Information",
- "Microsoft": "Information"
- }
- }
Step 3
Modify the startup class configure method to enable the console logging with above configuration.
- public void Configure(IApplicationBuilder app ,ILoggerFactory loggerFactory)
- {
-
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
-
- app.UseOcelot().Wait();
- }
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!
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.
- <?xml version="1.0" encoding="utf-8" ?><nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- autoReload="true"
- internalLogLevel="Warn"
- internalLogFile="internal-nlog.txt">
-
- <targets>
- <target xsi:type="File"
- name="debug"
- fileName="debug-${shortdate}.log"
- layout="${longdate}|${event-properties:item=EventId.Id}|${uppercase:${level}}|${logger}|${message} ${exception}" />
- </targets>
-
- <rules>
- <logger name="*" minlevel="Debug" writeTo="debug" />
- </rules>
- </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.
- public void Configure(IApplicationBuilder app ,ILoggerFactory loggerFactory)
- {
-
- loggerFactory.AddConsole(Configuration.GetSection("Logging"));
-
-
- loggerFactory.AddNLog();
- loggerFactory.ConfigureNLog("nlog.config");
-
- app.UseOcelot().Wait();
- }
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.
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.