背景
對(duì)于每隔幾毫秒發(fā)生的事件,最好使每個(gè)事件的開(kāi)銷較低(小于一毫秒)。 否則,對(duì)性能的影響將很大。 記錄事件意味著你將向磁盤(pán)寫(xiě)入內(nèi)容。 如果磁盤(pán)不夠快,你將丟失事件。 你需要一個(gè)解決方案,而不是記錄事件本身。
在處理大量事件時(shí),了解每個(gè)事件的度量值也無(wú)濟(jì)于事。 大多數(shù)時(shí)候,你只需要一些統(tǒng)計(jì)信息。 因此,你可以在進(jìn)程本身中獲取統(tǒng)計(jì)信息,然后偶爾編寫(xiě)一個(gè)事件來(lái)報(bào)告統(tǒng)計(jì)信息,這是 EventCounter 將執(zhí)行的操作。
代碼實(shí)現(xiàn)
下面是有關(guān)如何實(shí)現(xiàn) System.Diagnostics.Tracing.EventSource 的示例。 創(chuàng)建名為 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);
- }
- }
- }
添加操作篩選器,創(chuàng)建名為 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);
- }
- }
- }
操作篩選器在請(qǐng)求開(kāi)始時(shí)啟動(dòng) Stopwatch,并在其完成后停止,捕獲運(yùn)行時(shí)間。 總毫秒數(shù)記錄到 MinimalEventCounterSource 單一實(shí)例。 為了應(yīng)用此篩選器,需要將其添加到篩選器集合。 在 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
原文鏈接:https://mp.weixin.qq.com/s/rRYKxa1iHLKCKwTH2SK4Mg