将云服务、虚拟机或 Service Fabric 诊断数据发送到 Application Insights

云服务、虚拟机、虚拟机规模集和 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 资源的检测键。

  • Channels 元素包含一个或多个 Channels 元素。

    • name 属性唯一引用该通道。
    • 使用 loglevel 属性可以指定通道允许的日志级别。 可用日志级别从最多信息到最少信息的顺序依次为:
      • “详细”
      • 信息
      • 警告
      • 错误
      • 严重

通道的作用类似于筛选器,可让你选择要发送到目标接收器的特定日志级别。 例如,可以收集详细日志并将其发送到存储,但只将“错误”日志发送到接收器。

下图显示了这种关系。

Diagnostics Public Configuration

下图汇总了配置值及其工作原理。 还可以在层次结构中不同级别下的配置中包含多个接收器。 位于顶层的接收器用作全局设置,在单个元素中指定的接收器类似于该全局设置的重写。

Diagnostics Sinks Configuration with Application Insights

完整的接收器配置示例

下面是公共配置文件的完整示例。该文件

  1. 将所有错误都发送到 Application Insights(在 DiagnosticMonitorConfiguration 节点中指定)
  2. 此外,还会发送应用程序日志的详细级别日志(在 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"
                        }
                    ]
                }
            }
        ]
    }
}

在上面的配置中,以下各行的含义如下所述:

发送 Azure 诊断收集的所有数据

<DiagnosticMonitorConfiguration overallQuotaInMB="4096" sinks="ApplicationInsights">
"DiagnosticMonitorConfiguration": {
    "overallQuotaInMB": 4096,
    "sinks": "ApplicationInsights",
}

只将错误日志发送到 Application Insights 接收器

<DiagnosticMonitorConfiguration overallQuotaInMB="4096" sinks="ApplicationInsights.MyTopDiagdata">
"DiagnosticMonitorConfiguration": {
    "overallQuotaInMB": 4096,
    "sinks": "ApplicationInsights.MyTopDiagData",
}

将详细的应用程序日志发送到 Application Insights

<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。

后续步骤