【Part 3】在ASP.NET Core中使用Ocelot构建API网关 - Logging

Introduction

在之前的系列文章中, 我们讨论了如何使用.Net Core构建API网关。

在这篇文章里,我们将讨论Ocelot的日志模块。

 

如果你想看前面系列的文章,请点击以下链接。

 

Why logging is so important

日志是软件开发的一个基本部分。它帮助开发者和团队通过特殊的视角能知道代码真正在如何执行。

开发人员还需要在开发期间和开发之后发现他们的应用程序中的问题。足够的日志消息可以帮助我们轻松地定位问题。

毫无疑问,API网关也需要日志记录。

Ocelot使用标准日志接口,并且有一些默认的实现对于.NET Core logging,它帮助我们快速地去理解日志模块。

现在,我们开始一个简单的演示。

Step 1

先创建两个项目。

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.

 

并且,使APIGateway和CustomerAPIServices项目相同,作为一个基本的演示一开始。

Step 2

在appsettings.json中添加以下配置。

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

Step 3

修改startup类configure方法,以使用上述配置启用控制台日志记录。

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

完成此步骤后,当我们运行API网关并访问一些服务时,您可能会得到如下图所示的一些信息!
ASP.NET Core

根据上面的屏幕截图,我们可以发现所有的请求都记录在控制台中。

我能基于这些日志信息分析程序。大多时候,我们不想日志输出在控制台,我们需要把它们储存起来,这样我们每次都能找到它们,而不是一瞬间。

下一步,我将使用NLog,一个具有丰富的日志路由和管理功能的免费日志平台,以使日志消息持久。

Step 4

按照 nuget包,NLog.Web.AspNetCore ,然后,需要一个配置文件,去配置NLog选项。

  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

在配置文件中,我仅仅输出内容到文件,但是这里有很多种输出类型,可根据你的需要选择配置。比如:Elasticsearch, Seq, PostgreSQL...

然后,我们需要修改startup class configuration method 去启动NLog ,并且配置选项。

 

  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. }  

 

在你运行API Gateway并且访问服务之后, NLog将生成日志文件,我们能在Console中看到日志文件内容。

ASP.NET Core

到此为止,我们完成啦!

 

Summary

 这篇文章介绍如何在Ocelot中使用日志模块,包括基本组件和第三方组件。

我们可以像普通ASP.NET Core 一样使用这个模块。.NET Core日志记录得益于Ocelot项目的设计和实现。希望这篇文章能对你有所帮助。

 

原文地址:https://www.c-sharpcorner.com/article/building-api-gateway-using-ocelot-in-asp-net-core-part-three-logging2

posted @ 2020-04-16 15:10  ddockerman  阅读(242)  评论(0编辑  收藏  举报