如何查询网站二级页面流量,注册建公司网站,网店美工具体要求,微信公众号运营内容前言.NetCore日志#xff0c;相信大家多少都接触过#xff0c;博客园有关 ① AspNetCore依赖注入第三方日志组件 ②第三方日志组件Nlog,Serilog 应用方法的博文层出不穷。结合程序的部署结构#xff0c;本文分单体和微服务聊一聊AspNetCore中追踪日志流的方法。TraceIdAsp… 前言 .NetCore日志相信大家多少都接触过博客园有关 ① AspNetCore依赖注入第三方日志组件 ②第三方日志组件Nlog,Serilog 应用方法的博文层出不穷。结合程序的部署结构本文分单体和微服务聊一聊AspNetCore中追踪日志流的方法。TraceId AspNetCore程序基于Pipeline和中间件处理请求 根据需要记录日志 生产出故障时在数量庞大的日志记录中追踪某个请求完整的处理链显得很有必要这个深有体会。针对单体程序AspNetCore贴心的为我们提供了HttpContext.TraceIdentifier属性 这个TraceId由{ConnectionId}:{Request Number}组成理论上这个id标记了位于某Http连接上的某次请求。① 为什么由 {ConnectionId}:{Request Number}组成默认大部分读者知晓Http1.1 一个连接上可发起多个Http请求② TraceId 中ConnectionId由Kestrel从{0-9a-z}中生成可参考https://github.com/aspnet/KestrelHttpServer/blob/a48222378b8249a26b093b5b835001c7c7b45815/src/Kestrel.Core/Internal/Infrastructure/CorrelationIdGenerator.csok 现在着重聊一下应用方式和衍生知识点① 启用NLog日志 添加 PackageReference IncludeNLog.Web.AspNetCore Version4.9.0 /public class Program { public static void Main(string[] args) { var webHost WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, configureDelagate) { //configureDelegate默认会按照如下顺序加载ChainedConfiguration、appsetting.*.jsonEnvironment、CommandLine configureDelagate.AddJsonFile($appsettings.secrets.json, optional: true, reloadOnChange: true); }) .ConfigureLogging((hostingContext, loggingBuilder) { loggingBuilder.AddConsole().AddDebug(); }) .UseNLog() // 默认会找工作目录下nlog.config配置文件 .UseStartupStartup() .Build(); webHost.Run(); } }很明显Nlog要在Pipeline中自由获取HttpContext属性这里需要注册 IHttpContextAccessorpublic class Startup{ // rest of the code omitted for brevity public void ConfigureServices(IServiceCollection services) { services.AddSingletonIHttpContextAccessor, HttpContextAccessor(); } // rest of the code omitted for brevity}② 配置Nlog 以支持 TraceId实际上nlog支持记录很多HttpContext信息详情请关注https://nlog-project.org/config/?tablayout-renderers。 下面的Nlog配置文件呈现了TraceId User_Id 业务上的UserId能帮助我们在茫茫日志中快速缩小日志?xml version1.0 encodingutf-8 ?nlog xmlnshttp://www.nlog-project.org/schemas/NLog.xsd xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance autoReloadtrue throwExceptionsfalse internalLogFileinternal-nlog.txt variable namelogDir valuelogs/${date:formatyyyyMMdd} / !-- 日志存储文件夹-- variable nameformat value${date:formatyy/MM/dd HH\:mm\:ss} [${level}].[${logger}].[${aspnet-TraceIdentifier}].[${aspnet-user-identity}]${newline}${message} ${exception:formattostring} / targets target nameinfo xsi:typeFile layout${format} fileName${logDir}/info.log encodingutf-8/ /targets rules logger name* minlevelInfo writeToinfo / /rules/nlog 结果如下以上是在单体程序内根据traceid追踪请求流的方法。进一步思考在微服务中各服务独立形成TraceId在初始阶段生成 TraceId 并在各微服务中保持该Traceid即可追踪微服务的请求流。 CorrelationId这里首先假设你的微服务/ 分布式服务已经部署ELK 等日志几种采集处理框架没有部署ELK也可将多个服务的日志写到同一个物理文件夹。 隆重介绍轮子 CorrelationId https:/github.com/stevejgordon/CorrelationIdCorrelationId是通过自定义Header来标记TraceId概念 CorrelationId 在首次收到请求时自定义名为【X-Correlation-ID】 的请求头在本服务Response写入该Header 后置服务检测到请求头中包含该Header 将该CorrelationId作为本服务的TraceId 向后流转这样在集中日志中能通过某TraceID追踪微服务/分布式 全链路请求处理日志。使用方式也相当简单// Install-Package CorrelationId -Version 2.1.0public void ConfigureServices(IServiceCollection services){ services.AddMvc(); services.AddCorrelationId();}一般在所有请求处理Middleware之前注册 CorrelationId 这样在所有中间件就能获取到 TraceIdpublic void Configure(IApplicationBuilder app, IHostingEnvironment env){ app.UseCorrelationId(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc();}打算应用该TraceId追踪全流程请求日志的服务都需要包含 中间件。Ok 本文由浅入深TraceID在单体程序和 分布式程序中的应用 希望对大家在日志排障时有所帮助。