Metrics.NET库的配置可以使用静态Metric类的Config属性完成。
全局上下文名称(Global context name)
在进程中指标的根级上下文可以通过以下方式进行配置(以优先级降序排列):
- 设置全局变量:Metrics.GlobalContextName
- 设置App Settings 关键字“Metrics.GlobalContextName”
- 如果以下没有设置,默认为“MachineName.ProcessName”
全局上下文名称可以包含以下变量占位符:
- $Env.MachineName$会被替换成机器名
- $Env.ProcessName$会被替成进程名
- $Env.AppDomainAppVirtualPath$会被替换成应用的虚拟路径
- $Env.$会被替换成环境变量中相应的值
配置样例
Metric.Config
.WithHttpEndpoint("http://localhost:1234/metrics/")
.WithAllCounters()
.WithInternalMetrics()
.WithReporting(config => config
.WithConsoleReport(TimeSpan.FromSeconds(30))
.WithCSVReports(@"c:\temp\reports\",TimeSpan.FromMinutes(30))
.WithTextFileReport(@"C:\temp\reports\metrics.txt",TimeSpan.FromHours(1))
);
App.Config配置
下面的配置项可以放入app.config(或web.config),用以控制Metrics.NET类库:
<!-- Completely disable all metrics -->
<add key="Metrics.CompletelyDisableMetrics" value="true"/>
<!-- Equivalent of calling Metric.Config.WithHttpEndpoint("http://localhost:1234/") -->
<add key="Metrics.HttpListener.HttpUriPrefix" value="http://localhost:1234/"/>
<!-- Equivalent of calling
Metric.Config.WithReporting(config =>
config.WithCSVReports(@"..\MetricsCSV\", TimeSpan.FromSeconds(10))
-->
<add key="Metrics.CSV.Path" value="..\MetricsCSV\"/>
<add key="Metrics.CSV.Interval.Seconds" value="10"/>
<!-- Specify how many times a scheduled reporter can consecutively fail to push data.
-1 : unlimited
0 : reporter stops trying to send data once an attempt to push fails (default)
x>0 : reporter tolerates x consecutive failures. If the number of failures is greater than x, the reporter stops.
Regardless of setting, each failure is handled by the MetricsErrorHandler.
-->
<add key="Metrics.Reports.ToleratedConsecutiveFailures" value="3"/>