serilog

学习ASP.NET Core(10)-全局日志与xUnit系统测试

眉间皱痕 提交于 2020-08-11 18:33:27
上一篇我们介绍了数据塑形,HATEOAS和内容协商,并在制器方法中完成了对应功能的添加;本章我们将介绍日志和测试相关的概念,并添加对应的功能 一、全局日志 在第一章介绍项目结构时,有提到.NET Core启动时默认加载了日志服务,且在appsetting.json文件配置了一些日志的设置,根据设置的日志等级的不同可以进行不同级别的信息的显示,但它无法做到输出固定格式的log信息至本地磁盘或是数据库,所以需要我们自己手动实现,而我们可以借助日志框架实现。 ps:在第7章节中我们记录的是数据处理层方法调用的日志信息,这里记录的则是ASP.NET Core WebAPI层级的日志信息,两者有所差异 1、引入日志框架 .NET程序中常用的日志框架有log4net,serilog 和Nlog,这里我们使用Serilog来实现相关功能,在BlogSystem.Core层使用NuGet安装 Serilog.AspNetCore ,同时还需要搜索 Serilog.Skins 安装希望支持的功能,这里我们希望添加对文件和控制台的输出,所以选择安装的是 Serilog.Skins.File和Serilog.Skins.Console 需要注意的是Serilog是不受appsetting.json的日志设置影响的,且它可以根据命名空间重写记录级别。还有一点需要注意的是

ASP.NET Core使用Elasticsearch记录API请求响应日志实战演练

纵饮孤独 提交于 2020-08-11 08:49:46
原文: ASP.NET Core使用Elasticsearch记录API请求响应日志实战演练 一、ASP.NET Core WebApi如何设计一个日志中间件? ASP.NET Core WebApi一个良好的日志记录内容包含,唯一请求Id(traceId),请求url,请求body内容,响应body内容,执行开始和执行结束时间,总耗时时间等等。通过组合 Docker,ElasticSearch,Kibana,ASP.NET Core 和 Serilog ,您获得了前所未有的便利性和功能,再也没有理由不再将日志记录整合到应用程序中了。。 • 一句话总结今天我们学习到达的目标? 如何在ASP.NET Core使用Elasticsearch和Kibana 来存储和展示我们应用程序的请求详细日志。 ElasticSearch 和 Kibana 改变了这一切。而 Docker 已经使 ElasticSearch 和 Kibana 的启动和运行变得毫不费力。ElasticSearch 和 Kibana 提供的强大功能以及非常高的性能,再加上它是开源的,这真的令人印象非常深刻。 如果对本次分享课程《ASP.NET Core使用Elasticsearch记录请求响应日志实战演练》感兴趣的话,那么请跟着阿笨一起学习吧。 废话不多说,直接上干货,我们不生产干货,我们只是干货的搬运工。 二、ASP

ASP.Net Core Web Api 使用 IdentityServer4 最新版 踩坑记录

故事扮演 提交于 2020-08-11 07:53:02
辅助工具 日志追踪包 : Serilog.AspNetCore 源码查看工具 : ILSpy 项目环境 ###: ASP.NetCore 3.1 IdentityServer4 4.0.0+ 主题内容 测试登录方式 : password 错误内容: connect/token 登陆出错 但百度/google网上的示例没有找到正确的调用方式,无奈只能自己动手,丰衣足食... 首先,先按照之前版本进行传参 POST请求 url: connect/token 参数传递通过 form-data 调用结果: <调用结果> HTTP : 400 { "error": "invalid_request" } 查找调用日志 Invoking IdentityServer endpoint: IdentityServer4.Endpoints.TokenEndpoint for /connect/token 可以看到地址匹配是成功的,那就是校验不通过了,接着看一下这个类的源码,找到错误触发地 public async Task<IEndpointResult> ProcessAsync(HttpContext context) { if (!HttpMethods.IsPost(context.Request.Method) || !context.Request

Turn off mvc request logging in asp.net core 2.2 with serilog

余生颓废 提交于 2020-08-10 03:32:49
问题 I am using Serilog.Extensions.Logging.File to logs in file. Here is my appsettings.json file: "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Information", "System": "None", "Microsoft": "None" } }, "LoggingFile": { "IncludeScopes": false, "pathFormat": "C:/logs/APILogs-{Date}.log", "LogLevel": { "Default": "Trace", "System": "None", "Microsoft": "None" } } My Startup.cs code: public void Configure( IApplicationBuilder app, IHostingEnvironment env,

Turn off mvc request logging in asp.net core 2.2 with serilog

谁说我不能喝 提交于 2020-08-10 03:32:28
问题 I am using Serilog.Extensions.Logging.File to logs in file. Here is my appsettings.json file: "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Information", "System": "None", "Microsoft": "None" } }, "LoggingFile": { "IncludeScopes": false, "pathFormat": "C:/logs/APILogs-{Date}.log", "LogLevel": { "Default": "Trace", "System": "None", "Microsoft": "None" } } My Startup.cs code: public void Configure( IApplicationBuilder app, IHostingEnvironment env,

Turn off mvc request logging in asp.net core 2.2 with serilog

被刻印的时光 ゝ 提交于 2020-08-10 03:32:10
问题 I am using Serilog.Extensions.Logging.File to logs in file. Here is my appsettings.json file: "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Information", "System": "None", "Microsoft": "None" } }, "LoggingFile": { "IncludeScopes": false, "pathFormat": "C:/logs/APILogs-{Date}.log", "LogLevel": { "Default": "Trace", "System": "None", "Microsoft": "None" } } My Startup.cs code: public void Configure( IApplicationBuilder app, IHostingEnvironment env,

Multiple Serilog Loggers

自作多情 提交于 2020-08-09 08:17:29
问题 Is it possible to have multiple serilog loggers? Currently within my WebApi I can call Log.Information for example to log an information event, but is there a way that I can instead make different logs and call ExternalLog.Information or AuthenticationLog.Information from my controller? The purpose behind this is that my web api is currently working with multiple different databases for different yet interrelated projects, and I would like to store logs within each of these databases that

使用Ocelot、IdentityServer4、Spring Cloud Eureka搭建微服务网关:Step by Step(二)

空扰寡人 提交于 2020-07-29 07:18:29
本文继续介绍使用Ocelot、IdentityServer4以及Spring Cloud Eureka搭建微服务网关的步骤。相关文章: 《 使用Ocelot、IdentityServer4、Spring Cloud Eureka搭建微服务网关:Step by Step(一) 》 Step 4:引入Ocelot API网关 新建一个ASP.NET Core API项目,添加对以下NuGet包的引用: Ocelot IdentityServer4 IdentityServer4.AccessTokenValidation Serilog(我使用了Serilog来输出格式化的日志,如果不使用Serilog,则不需要引用) 首先,修改Startup.cs文件,用来启用Ocelot,并指定身份认证机构为之前我们建好的IdentityServer服务: public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddAuthentication() .AddIdentityServerAuthentication("AuthProviderKey",

Output JSON in Azure Function Logs

匆匆过客 提交于 2020-07-23 06:32:26
问题 We'd like to send structured log data from our Azure Functions to Event Hub. So I set up Serilog to log to the console and include all the info I wanted. But now I come to try this in Azure, all of my nice Json formatted data from Serilog is being ignored - and only the standard ILogger output is being shown :( Here's the config I'm using in Startup.ConfigureServices . services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(CreateLogger())); private static Logger CreateLogger() { var

Output JSON in Azure Function Logs

空扰寡人 提交于 2020-07-23 06:31:05
问题 We'd like to send structured log data from our Azure Functions to Event Hub. So I set up Serilog to log to the console and include all the info I wanted. But now I come to try this in Azure, all of my nice Json formatted data from Serilog is being ignored - and only the standard ILogger output is being shown :( Here's the config I'm using in Startup.ConfigureServices . services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(CreateLogger())); private static Logger CreateLogger() { var