如何在Azure功能本地使用Application Insights?

问题描述 投票:5回答:3

我正在使用接口ILogger来记录Azure函数中的事件。我可以在Azure中发布它并将其连接到Azure中的Application Insights。

我想在开发期间在Visual Studio中查看Application Insights中的日志。在here中,我可以看到这在ASP.NET核心Web应用程序中可以在Startup.cs中放入一些代码。使用VS 2017中的工具中的新项目模板,Azure功能可以实现类似的功能吗?

我正在使用VS 2017和Azure Function CLI 1.0.0-beta-100。

azure azure-functions azure-application-insights
3个回答
4
投票

据我所知,目前我们无法在您当地的azure功能项目中直接包含Application Insights。

这是一个解决方法:

你需要自己实现它。从Nuget包管理器安装了Microsoft.ApplicationInsights之后。

然后使用TelemetryClient将日志发送到azure。

更多细节,您可以参考以下代码:

[FunctionName("HttpTriggerCSharp")]
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
    var appInsights = GetTelemetryClient();
    //track an event
    appInsights.TrackEvent("I am starting now. I'm timer triggered");
    // track a numeric value
    appInsights.TrackMetric("Ticks based on current time", DateTime.Now.Ticks);
    // track an exception
    appInsights.TrackException(new Exception($"Random exception at {DateTime.Now}"));

    // send data to azure
    appInsights.Flush();

    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();

    // Set name to query string or body data
    name = name ?? data?.name;

    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
}

private static TelemetryClient GetTelemetryClient()
{
    var telemetryClient = new TelemetryClient();
    telemetryClient.InstrumentationKey = "Your InstrumentationKey";
    return telemetryClient;
}

结果:

enter image description here


7
投票

将应用程序洞察(AI)遥测添加到Azure功能简单明了。

你只需要官方文档中的这些two步骤;

  1. 创建Application Insights实例。 应用程序类型应设置为常规 抓住仪器密钥
  2. 更新功能应用程序的设置 添加应用设置 - APPINSIGHTS_INSTRUMENTATIONKEY = {Instrumentation Key}

然而,显而易见的是如何在本地开发功能应用程序时捕获AI遥测数据,就在它让它进入云端之前。首先,您需要为Azure Functions的prepare本地开发环境。那你几乎准备好了。您只需要在local.settings.json文件上重复上面的第二步。所以你的文件看起来应该是这样的;

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_EXTENSION_VERSION": "beta",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "HOST_NAME": "localhost:7072",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "11111111-2222-3333-4444-555555555555"
  },
  "Host": {
    "LocalHttpPort": 7072
  }
}

在我的情况下,我有2个AI实例,我的Azure门户和我的本地开发环境(APPINSIGHTS_INSTRUMENTATIONKEY)中的local.settings.jsonvalues是不同的,所以我不会混淆prod和dev遥测。


2
投票

因此,显然由于对库进行了更改,它会不时地停止工作。所以这就是帮助我在2019年4月开始工作的原因。希望它能帮到别人。

我没有添加任何日志或任何特定于我的功能应用程序,现在对我来说,它几乎将所有控制台数据打印到我的应用程序洞察中的跟踪日志(本地,不需要在Azure中配置任何内容)。此外,我的测试场景在TargetFramework netstandard2.0和AzureFunctionsVersion 2上运行。

首先,您需要将此NuGet包添加到功能应用程序:https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/

然后你必须在项目根目录中添加一个空文件:ApplicationInsights.config你不需要在构建或任何东西时将它复制到输出,这只是必须存在的东西或Application Insights不能在本地工作。

然后你需要在APPINSIGHTS_INSTRUMENTATIONKEY中为你的local.settings.json添加一些值。这是它与常规控制台应用程序的不同之处,即使检测键是空的,它也可以在本地工作。在功能应用中显然你需要在那里添加一些价值。它可以是任何东西,我已经完成了它并且它工作正常:

"APPINSIGHTS_INSTRUMENTATIONKEY": "you-need-to-have-anything-at-all-here-so-it-will-work-locally"
© www.soinside.com 2019 - 2024. All rights reserved.