CZGL.ProcessMetrics如何监控.NET应用以实现长尾词效果?

2026-03-30 10:101阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计879个文字,预计阅读时间需要4分钟。

CZGL.ProcessMetrics如何监控.NET应用以实现长尾词效果?

CZGL.ProcessMetrics 是一个 Metrics 库,它能记录程序的 GC、CPU、内存、机器网络、磁盘空间等信息。通过 Prometheus 收集数据,并用 Grafana 进行可视化展示。效果预览:安装 ProcessMetrics。

导读

CZGL.ProcessMetrics 是一个 Metrics 库,能够将程序的 GC、CPU、内存、机器网络、磁盘空间等信息记录下来,使用 Prometheus 采集信息,然后使用 Grafana 显示。

效果图预览:

安装 ProcsssMetrics

只需要通过 Nuget 安装一个库,即可快速为程序添加资源监视,ProcssMetrics 同时支持 Winform、Wpf、ASP.NET Core 等。
CZGL.ProcessMetrics 支持 .NET Standard 2.0 和 .NET Core 3.1,但是在 .NET Standard 2.0 中,因为缺少部分 Core API,所以有部分信息是无法获取的,这部分信息如下:

标识.NET Core API说明gc_memory_infoGC.GetGCMemoryInfo()获取 GC 内存信息total_allocated_bytesGC.GetTotalAllocatedBytes()总分配量dotnet_lock_contention_totalMonitor.LockContentionCount线程池竞争数量

新建一个应用, Nuget 中搜索CZGL.ProcessMetrics直接引用即可。

CZGL.ProcessMetrics如何监控.NET应用以实现长尾词效果?

Nuget 地址:www.nuget.org/packages/CZGL.ProcessMetrics

有两种方式使用 Metrics,第一种是使用内置的 HttpListener,不需要放到 Web 中即可独立提供 URL 访问,适合 winform、wpf 或纯 控制台等应用。但是使用 HttpListener,需要使用管理员方式启动应用才能正常运行。

使用方法:

using CZGL.ProcessMetrics; ... ... MetricsServer metricsServer = new MetricsServer("*:1234/metrics/"); metricsServer.Start();

另外一种是使用 ASP.NET Core,Metrics 作为中间件加入到 Web 应用中,此时使用的是 kestrel 。

在 Nuget 中,搜索CZGL.ProcessMetrics.ASPNETCore包,然后使用中间件生成 Metrics 端点。

app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.ProcessMetrices("/metrics"); });

但是目前无论哪种,都必须让暴露端口出去,让 Prometheus 能够访问到 API。后期会增加支持不需要暴露 API 、提供 Web 服务,即可直接推送监控信息到 Prometheus 的功能。

访问相应的 URL,可以看到有很多信息输出,这些都是 Prometheus 数据的格式。

127.0.0.1:1234/metrics

搭建 Prometheus/Grafana

这里我们使用 Docker 来搭建监控平台。

拉取镜像:

docker pull prom/prometheus docker pull grafana/grafana

/opt/prometheus目录下,新建一个prometheus.yml文件,其内容如下:

# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'github.com/whuanle/CZGL.SystemInfo/releases/tag/v1.0

然后导入模型文件。

即可看到监控界面。

到此这篇关于CZGL.ProcessMetrics监控.NET应用的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计879个文字,预计阅读时间需要4分钟。

CZGL.ProcessMetrics如何监控.NET应用以实现长尾词效果?

CZGL.ProcessMetrics 是一个 Metrics 库,它能记录程序的 GC、CPU、内存、机器网络、磁盘空间等信息。通过 Prometheus 收集数据,并用 Grafana 进行可视化展示。效果预览:安装 ProcessMetrics。

导读

CZGL.ProcessMetrics 是一个 Metrics 库,能够将程序的 GC、CPU、内存、机器网络、磁盘空间等信息记录下来,使用 Prometheus 采集信息,然后使用 Grafana 显示。

效果图预览:

安装 ProcsssMetrics

只需要通过 Nuget 安装一个库,即可快速为程序添加资源监视,ProcssMetrics 同时支持 Winform、Wpf、ASP.NET Core 等。
CZGL.ProcessMetrics 支持 .NET Standard 2.0 和 .NET Core 3.1,但是在 .NET Standard 2.0 中,因为缺少部分 Core API,所以有部分信息是无法获取的,这部分信息如下:

标识.NET Core API说明gc_memory_infoGC.GetGCMemoryInfo()获取 GC 内存信息total_allocated_bytesGC.GetTotalAllocatedBytes()总分配量dotnet_lock_contention_totalMonitor.LockContentionCount线程池竞争数量

新建一个应用, Nuget 中搜索CZGL.ProcessMetrics直接引用即可。

CZGL.ProcessMetrics如何监控.NET应用以实现长尾词效果?

Nuget 地址:www.nuget.org/packages/CZGL.ProcessMetrics

有两种方式使用 Metrics,第一种是使用内置的 HttpListener,不需要放到 Web 中即可独立提供 URL 访问,适合 winform、wpf 或纯 控制台等应用。但是使用 HttpListener,需要使用管理员方式启动应用才能正常运行。

使用方法:

using CZGL.ProcessMetrics; ... ... MetricsServer metricsServer = new MetricsServer("*:1234/metrics/"); metricsServer.Start();

另外一种是使用 ASP.NET Core,Metrics 作为中间件加入到 Web 应用中,此时使用的是 kestrel 。

在 Nuget 中,搜索CZGL.ProcessMetrics.ASPNETCore包,然后使用中间件生成 Metrics 端点。

app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.ProcessMetrices("/metrics"); });

但是目前无论哪种,都必须让暴露端口出去,让 Prometheus 能够访问到 API。后期会增加支持不需要暴露 API 、提供 Web 服务,即可直接推送监控信息到 Prometheus 的功能。

访问相应的 URL,可以看到有很多信息输出,这些都是 Prometheus 数据的格式。

127.0.0.1:1234/metrics

搭建 Prometheus/Grafana

这里我们使用 Docker 来搭建监控平台。

拉取镜像:

docker pull prom/prometheus docker pull grafana/grafana

/opt/prometheus目录下,新建一个prometheus.yml文件,其内容如下:

# my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'github.com/whuanle/CZGL.SystemInfo/releases/tag/v1.0

然后导入模型文件。

即可看到监控界面。

到此这篇关于CZGL.ProcessMetrics监控.NET应用的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持自由互联。