从Powershell运行内部Webjob写入App Insights

时间:2018-05-02 20:45:39

标签: powershell azure-application-insights azure-webjobssdk

我正在使用此处概述的流程运行PowerShell脚本: https://blogs.msdn.microsoft.com/nicktrog/2014/01/22/running-powershell-web-jobs-on-azure-websites/

我还在脚本中捕获错误,并使用此处的方法将它们记录到Application Insights: https://www.c-sharpcorner.com/article/using-azure-application-insights-in-powershell/

当代码运行时,我通过在脚本中引入拼写错误来强制错误。捕获异常并且TrackException()调用没有问题发生,但是没有日志条目能够进入应用程序洞察! 我尝试在本地运行相同的脚本,它记录得很好。

示例脚本:

$rootPath = ($env:webroot_path)
Add-Type -Path "$rootPath\bin\Microsoft.ApplicationInsights.dll"
$appInsightsClient = New-Object Microsoft.ApplicationInsights.TelemetryClient
$appInsightsClient.InstrumentationKey = "the-key"


try {
    $lastSuccessDate = Get-Dates "1/1/1900"
} catch {
    [Console]::WriteLine($_.Exception.Message)
    $telemetryException = New-Object "Microsoft.ApplicationInsights.DataContracts.ExceptionTelemetry"
    $telemetryException.Exception = $_.Exception
    $appInsightsClient.TrackException($telemetryException)

    Exit 1
}

1 个答案:

答案 0 :(得分:0)

要更新ApplicationInsights例外,您需要将Flush()添加到TelemetryClient

添加:

$appInsightsClient.TrackException($TelException)
$appInsightsClient.Flush()