背景
对于每隔几毫秒发生的事件,最好使每个事件的开销较低(小于一毫秒)。 否则,对性能的影响将很大。 记录事件意味着你将向磁盘写入内容。 如果磁盘不够快,你将丢失事件。 你需要一个解决方案,而不是记录事件本身。
在处理大量事件时,了解每个事件的度量值也无济于事。 大多数时候,你只需要一些统计信息。 因此,你可以在进程本身中获取统计信息,然后偶尔编写一个事件来报告统计信息,这是 EventCounter 将执行的操作。
代码实现
下面是有关如何实现 System.Diagnostics.Tracing.EventSource 的示例。 创建名为 MinimalEventCounterSource.cs 的新文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics.Tracing;namespace WebApplication42
{[EventSource(Name = "Sample.EventCounter.Minimal")]public sealed class MinimalEventCounterSource : EventSource{public static readonly MinimalEventCounterSource Log = new MinimalEventCounterSource();private EventCounter _requestCounter;private MinimalEventCounterSource() =>_requestCounter = new EventCounter("request-time", this){DisplayName = "Request Processing Time",DisplayUnits = "ms"};public void Request(string url, float elapsedMilliseconds){Console.WriteLine("url:" + url + " elapsedMilliseconds:" + elapsedMilliseconds);WriteEvent(1, url, elapsedMilliseconds);_requestCounter?.WriteMetric(elapsedMilliseconds);}protected override void Dispose(bool disposing){_requestCounter?.Dispose();_requestCounter = null;base.Dispose(disposing);}}
}
添加操作筛选器,创建名为 LogRequestTimeFilterAttribute.cs 的新文件,并使用以下代码:
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;namespace WebApplication42
{public class LogRequestTimeFilterAttribute : ActionFilterAttribute{private readonly Stopwatch _stopwatch = new Stopwatch();public override void OnActionExecuting(ActionExecutingContext context) => _stopwatch.Start();public override void OnActionExecuted(ActionExecutedContext context){_stopwatch.Stop();MinimalEventCounterSource.Log.Request(context.HttpContext.Request.GetDisplayUrl(), _stopwatch.ElapsedMilliseconds);}}
}
操作筛选器在请求开始时启动 Stopwatch,并在其完成后停止,捕获运行时间。 总毫秒数记录到 MinimalEventCounterSource 单一实例。 为了应用此筛选器,需要将其添加到筛选器集合。 在 Startup.cs 文件中,更新包含此筛选器的 ConfigureServices 方法。
// This method gets called by the runtime. Use this method to add services to the container.public void ConfigureServices(IServiceCollection services){services.AddControllers(options => options.Filters.Add<LogRequestTimeFilterAttribute>());services.AddSwaggerGen(c =>{c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication42", Version = "v1" });});}
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:70
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:19
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:18
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:19
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:22
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:17
url:https://localhost:5008/WeatherForecast elapsedMilliseconds:17