
背景
对于每隔几毫秒发生的事件,最好使每个事件的开销较低(小于一毫秒)。 否则,对性能的影响将很大。 记录事件意味着你将向磁盘写入内容。 如果磁盘不够快,你将丢失事件。 你需要一个解决方案,而不是记录事件本身。
在处理大量事件时,了解每个事件的度量值也无济于事。 大多数时候,你只需要一些统计信息。 因此,你可以在进程本身中获取统计信息,然后偶尔编写一个事件来报告统计信息,这是 EventCounter 将执行的操作。
代码实现
下面是有关如何实现 System.Diagnostics.Tracing.EventSource 的示例。 创建名为 MinimalEventCounterSource.cs 的新文件
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Threading.Tasks;
- usingSystem.Diagnostics.Tracing;
- namespaceWebApplication42
- {
- [EventSource(Name="Sample.EventCounter.Minimal")]
- publicsealedclassMinimalEventCounterSource:EventSource
- {
- publicstaticreadonlyMinimalEventCounterSourceLog=newMinimalEventCounterSource();
- privateEventCounter_requestCounter;
- privateMinimalEventCounterSource()=>
- _requestCounter=newEventCounter("request-time",this)
- {
- DisplayName="RequestProcessingTime",
- DisplayUnits="ms"
- };
- publicvoidRequest(stringurl,floatelapsedMilliseconds)
- {
- Console.WriteLine("url:"+url+"elapsedMilliseconds:"+elapsedMilliseconds);
- WriteEvent(1,url,elapsedMilliseconds);
- _requestCounter?.WriteMetric(elapsedMilliseconds);
- }
- protectedoverridevoidDispose(booldisposing)
- {
- _requestCounter?.Dispose();
- _requestCounter=null;
- base.Dispose(disposing);
- }
- }
- }
添加操作筛选器,创建名为 LogRequestTimeFilterAttribute.cs 的新文件,并使用以下代码:
- usingMicrosoft.AspNetCore.Http.Extensions;
- usingMicrosoft.AspNetCore.Mvc.Filters;
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Diagnostics;
- usingSystem.Linq;
- usingSystem.Threading.Tasks;
- namespaceWebApplication42
- {
- publicclassLogRequestTimeFilterAttribute:ActionFilterAttribute
- {
- privatereadonlyStopwatch_stopwatch=newStopwatch();
- publicoverridevoidOnActionExecuting(ActionExecutingContextcontext)=>_stopwatch.Start();
- publicoverridevoidOnActionExecuted(ActionExecutedContextcontext)
- {
- _stopwatch.Stop();
- MinimalEventCounterSource.Log.Request(
- context.HttpContext.Request.GetDisplayUrl(),_stopwatch.ElapsedMilliseconds);
- }
- }
- }
操作筛选器在请求开始时启动 Stopwatch,并在其完成后停止,捕获运行时间。 总毫秒数记录到 MinimalEventCounterSource 单一实例。 为了应用此筛选器,需要将其添加到筛选器集合。 在 Startup.cs 文件中,更新包含此筛选器的 ConfigureServices 方法。
- //Thismethodgetscalledbytheruntime.Usethismethodtoaddservicestothecontainer.
- publicvoidConfigureServices(IServiceCollectionservices)
- {
- services.AddControllers(options=>options.Filters.Add<LogRequestTimeFilterAttribute>());
- services.AddSwaggerGen(c=>
- {
- c.SwaggerDoc("v1",newOpenApiInfo{Title="WebApplication42",Version="v1"});
- });
- }


- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:70
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:19
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:18
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:19
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:22
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:17
- url:https://localhost:5008/WeatherForecastelapsedMilliseconds:17
原文链接:https://mp.weixin.qq.com/s/rRYKxa1iHLKCKwTH2SK4Mg








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。