将云服务、虚拟机或 Service Fabric 诊断数据发送到 Application InsightsSend Cloud Service, Virtual Machine, or Service Fabric diagnostic data to Application Insights

云服务、虚拟机、虚拟机规模集和 Service Fabric 都使用 Azure 诊断扩展来收集数据。Cloud services, Virtual Machines, Virtual Machine Scale Sets and Service Fabric all use the Azure Diagnostics extension to collect data. Azure 诊断将数据发送到 Azure 存储表。Azure diagnostics sends data to Azure Storage tables. 但是,也可以 Azure 诊断扩展 1.5 或更高版本,通过管道将所有或一部分数据发送到其他位置。However, you can also pipe all or a subset of the data to other locations using Azure Diagnostics extension 1.5 or later.

本文介绍如何将数据从 Azure 诊断扩展发送到 Application Insights。This article describes how to send data from the Azure Diagnostics extension to Application Insights.

诊断配置说明Diagnostics configuration explained

Azure 诊断扩展 1.5 引入了接收器 - 可将诊断数据发送到的附加位置。The Azure diagnostics extension 1.5 introduced sinks, which are additional locations where you can send diagnostic data.

Application Insights 接收器的示例配置:Example configuration of a sink for 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 属性是用于唯一标识该接收器的字符串值。The Sink name attribute is a string value that uniquely identifies the sink.

  • ApplicationInsights 元素指定要将 Azure 诊断数据发送到的 Application Insights 资源的检测键。The ApplicationInsights element specifies instrumentation key of the Application insights resource where the Azure diagnostics data is sent.

    • 如果没有 Application Insights 资源,请参阅创建新的 Application Insights 资源,了解有关创建资源和获取检测键的详细信息。If you don't have an existing Application Insights resource, see Create a new Application Insights resource for more information on creating a resource and getting the instrumentation key.
    • 如果使用 Azure SDK 2.8 和更高版本开发云服务,系统会自动填充此检测键。If you are developing a Cloud Service with Azure SDK 2.8 and later, this instrumentation key is automatically populated. 该值基于打包云服务项目时指定的 APPINSIGHTS_INSTRUMENTATIONKEY 服务配置设置。The value is based on the APPINSIGHTS_INSTRUMENTATIONKEY service configuration setting when packaging the Cloud Service project. 请参阅将 Application Insights 与云服务配合使用See Use Application Insights with Cloud Services.
  • Channels 元素包含一个或多个 Channels 元素。The Channels element contains one or more Channel elements.

    • name 属性唯一引用该通道。The name attribute uniquely refers to that channel.
    • 使用 loglevel 属性可以指定通道允许的日志级别。The loglevel attribute lets you specify the log level that the channel allows. 可用日志级别从最多信息到最少信息的顺序依次为:The available log levels in order of most to least information are:
      • 详细Verbose
      • 信息Information
      • 警告Warning
      • 错误Error
      • 关键Critical

通道的作用类似于筛选器,可让你选择要发送到目标接收器的特定日志级别。A channel acts like a filter and allows you to select specific log levels to send to the target sink. 例如,可以收集详细日志并将其发送到存储,但只将“错误”日志发送到接收器。For example, you could collect verbose logs and send them to storage, but send only Errors to the sink.

下图显示了这种关系。The following graphic shows this relationship.

诊断公共配置

下图汇总了配置值及其工作原理。The following graphic summarizes the configuration values and how they work. 还可以在层次结构中不同级别下的配置中包含多个接收器。You can include multiple sinks in the configuration at different levels in the hierarchy. 位于顶层的接收器用作全局设置,在单个元素中指定的接收器类似于该全局设置的重写。The sink at the top level acts as a global setting and the one specified at the individual element acts like an override to that global setting.

使用 Application Insights 配置诊断接收器

完整的接收器配置示例Complete sink configuration example

下面是公共配置文件的完整示例。该文件Here is a complete example of the public configuration file that

  1. 将所有错误都发送到 Application Insights(在 DiagnosticMonitorConfiguration 节点中指定)sends all errors to Application Insights (specified at the DiagnosticMonitorConfiguration node)
  2. 此外,还会发送应用程序日志的详细级别日志(在 Logs 节点中指定)。also sends Verbose level logs for the Application Logs (specified at the Logs node).
<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"
                        }
                    ]
                }
            }
        ]
    }
}

在上面的配置中,以下各行的含义如下所述:In the previous configuration, the following lines have the following meanings:

发送 Azure 诊断收集的所有数据Send all the data that is being collected by Azure diagnostics

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

只将错误日志发送到 Application Insights 接收器Send only error logs to the Application Insights sink

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

将详细的应用程序日志发送到 Application InsightsSend Verbose application logs to Application Insights

<Logs scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Verbose" sinks="ApplicationInsights.MyLogData"/>
"DiagnosticMonitorConfiguration": {
    "overallQuotaInMB": 4096,
    "sinks": "ApplicationInsights.MyLogData",
}

限制Limitations

  • 通道只能记录类型,而不能记录性能计数器。Channels only log type and not performance counters. 如果对性能计数器元素指定通道,将忽略该通道。If you specify a channel with a performance counter element, it is ignored.
  • 通道的日志级别不能超过 Azure 诊断所要收集的日志级别。The log level for a channel cannot exceed the log level for what is being collected by Azure diagnostics. 例如,不能在 Logs 元素中收集应用程序日志错误,并且不能尝试向 Application Insight 接收器发送详细日志。For example, you cannot collect Application Log errors in the Logs element and try to send Verbose logs to the Application Insight sink. scheduledTransferLogLevelFilter 属性收集的日志数始终必须等于或大于尝试发送到接收器的日志数。The scheduledTransferLogLevelFilter attribute must always collect equal or more logs than the logs you are trying to send to a sink.
  • 无法将 Azure 诊断扩展收集的 Blob 数据发送到 Application Insights。You cannot send blob data collected by Azure diagnostics extension to Application Insights. 例如,Directories 节点下指定的任何数据。For example, anything specified under the Directories node. 对于故障转储,实际故障转储将发送到 Blob 存储,并只会将生成了故障转储的通知发送到 Application Insights。For Crash Dumps the actual crash dump is sent to blob storage and only a notification that the crash dump was generated is sent to Application Insights.

后续步骤Next Steps