云服务、虚拟机、虚拟机规模集和 Service Fabric 都使用 Azure 诊断扩展来收集数据。 Azure 诊断将数据发送到 Azure 存储表。 但是,也可以 Azure 诊断扩展 1.5 或更高版本,通过管道将所有或一部分数据发送到其他位置。
本文介绍如何将数据从 Azure 诊断扩展发送到 Application Insights。
Azure 诊断扩展 1.5 引入了接收器,它是可将诊断数据发送到的额外位置。
Application Insights 接收器的示例配置:
<SinksConfig>
<Sink name="ApplicationInsights">
<ApplicationInsights>{Insert InstrumentationKey}</ApplicationInsights>
<Channels>
<Channel logLevel="Error" name="MyTopDiagData" />
<Channel logLevel="Verbose" name="MyLogData" />
</Channels>
</Sink>
</SinksConfig>
"SinksConfig": {
"Sink": [
{
"name": "ApplicationInsights",
"ApplicationInsights": "{Insert InstrumentationKey}",
"Channels": {
"Channel": [
{
"logLevel": "Error",
"name": "MyTopDiagData"
},
{
"logLevel": "Error",
"name": "MyLogData"
}
]
}
}
]
}
接收器的 name 属性是用于唯一标识该接收器的字符串值。
ApplicationInsights 元素指定要将 Azure 诊断数据发送到的 Application Insights 资源的检测键。
- 如果没有现有的 Application Insights 资源,请参阅创建新的 Application Insights 资源。
- 如果正在使用 Azure SDK 2.8 和更高版本开发云服务,则系统会自动填充此检测密钥。 该值基于打包云服务项目时的 APPINSIGHTS_INSTRUMENTATIONKEY 服务配置设置,请参阅将 Application Insights 与云服务配合使用。
Channels 元素包含一个或多个 Channels 元素。
- name 属性唯一引用该通道。
- 使用 loglevel 属性可以指定通道允许的日志级别。 可用日志级别从最多信息到最少信息的顺序依次为:
- “详细”
- 信息
- 警告
- 错误
- 严重
通道的作用类似于筛选器,可让你选择要发送到目标接收器的特定日志级别。 例如,可以收集详细日志并将其发送到存储,但只将“错误”日志发送到接收器。
下图显示了这种关系。
下图汇总了配置值及其工作原理。 还可以在层次结构中不同级别下的配置中包含多个接收器。 位于顶层的接收器用作全局设置,在单个元素中指定的接收器类似于该全局设置的重写。
下面是公共配置文件的完整示例。该文件:
- 可将所有错误发送到 Application Insights(在 DiagnosticMonitorConfiguration 节点中指定)。
- 此外,还会发送应用程序日志的详细级别日志(在 Logs 节点中指定)。
<WadCfg>
<DiagnosticMonitorConfiguration overallQuotaInMB="4096"
sinks="ApplicationInsights.MyTopDiagData"> <!-- All info below sent to this channel -->
<DiagnosticInfrastructureLogs />
<PerformanceCounters>
<PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT3M" />
<PerformanceCounterConfiguration counterSpecifier="\Memory\Available MBytes" sampleRate="PT3M" />
</PerformanceCounters>
<WindowsEventLog scheduledTransferPeriod="PT1M">
<DataSource name="Application!*" />
</WindowsEventLog>
<Logs scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Verbose"
sinks="ApplicationInsights.MyLogData"/> <!-- This specific info sent to this channel -->
</DiagnosticMonitorConfiguration>
<SinksConfig>
<Sink name="ApplicationInsights">
<ApplicationInsights>{Insert InstrumentationKey}</ApplicationInsights>
<Channels>
<Channel logLevel="Error" name="MyTopDiagData" />
<Channel logLevel="Verbose" name="MyLogData" />
</Channels>
</Sink>
</SinksConfig>
</WadCfg>
"WadCfg": {
"DiagnosticMonitorConfiguration": {
"overallQuotaInMB": 4096,
"sinks": "ApplicationInsights.MyTopDiagData", "_comment": "All info below sent to this channel",
"DiagnosticInfrastructureLogs": {
},
"PerformanceCounters": {
"PerformanceCounterConfiguration": [
{
"counterSpecifier": "\\Processor(_Total)\\% Processor Time",
"sampleRate": "PT3M"
},
{
"counterSpecifier": "\\Memory\\Available MBytes",
"sampleRate": "PT3M"
}
]
},
"WindowsEventLog": {
"scheduledTransferPeriod": "PT1M",
"DataSource": [
{
"name": "Application!*"
}
]
},
"Logs": {
"scheduledTransferPeriod": "PT1M",
"scheduledTransferLogLevelFilter": "Verbose",
"sinks": "ApplicationInsights.MyLogData", "_comment": "This specific info sent to this channel"
}
},
"SinksConfig": {
"Sink": [
{
"name": "ApplicationInsights",
"ApplicationInsights": "{Insert InstrumentationKey}",
"Channels": {
"Channel": [
{
"logLevel": "Error",
"name": "MyTopDiagData"
},
{
"logLevel": "Verbose",
"name": "MyLogData"
}
]
}
}
]
}
}
在上面的配置中,以下各行的含义如下所述:
<DiagnosticMonitorConfiguration overallQuotaInMB="4096" sinks="ApplicationInsights">
"DiagnosticMonitorConfiguration": {
"overallQuotaInMB": 4096,
"sinks": "ApplicationInsights",
}
<DiagnosticMonitorConfiguration overallQuotaInMB="4096" sinks="ApplicationInsights.MyTopDiagdata">
"DiagnosticMonitorConfiguration": {
"overallQuotaInMB": 4096,
"sinks": "ApplicationInsights.MyTopDiagData",
}
<Logs scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Verbose" sinks="ApplicationInsights.MyLogData"/>
"DiagnosticMonitorConfiguration": {
"overallQuotaInMB": 4096,
"sinks": "ApplicationInsights.MyLogData",
}
- 通道只能记录类型,而不能记录性能计数器。 如果通过性能计数器元素指定通道,则会忽略它。
- 通道的日志级别不能超过 Azure 诊断正在收集的日志级别。 例如,不能在 Logs 元素中收集应用程序日志错误,并且不能尝试向 Application Insight 接收器发送详细日志。 scheduledTransferLogLevelFilter 属性收集的日志数必须始终等于或大于尝试向接收器发送的日志数。
- 无法将 Azure 诊断扩展收集的 Blob 数据发送到 Application Insights。 例如,Directories 节点下指定的任何数据。 对于故障转储,实际故障转储将发送到 Blob 存储,并只会将已生成故障转储的通知发送到 Application Insights。
- 了解如何在 Application Insights 中查看 Azure 诊断信息。
- 使用 PowerShell 为应用程序启用 Azure 诊断扩展。
- 使用 Visual Studio 为应用程序启用 Azure 诊断扩展。