首页 文章

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

提问于
浏览
4

我正在使用接口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 .

2 回答

  • 5

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

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

    • 创建Application Insights实例 .

    • 应用程序类型应设置为 General

    • 抓取检测密钥

    • 更新功能应用程序的设置

    • 添加应用设置 - APPINSIGHTS_INSTRUMENTATIONKEY =

    然而,显而易见的是如何在本地开发功能应用程序时捕获AI遥测数据,就在它让它进入 Cloud 端之前 . 首先,您需要prepare Azure功能的本地开发环境 . 然后你几乎准备好了 . 您只需要在 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门户和我的本地开发环境( local.settings.json )中的 APPINSIGHTS_INSTRUMENTATIONKEY 值是不同的,所以我不会混淆prod和dev遥测 .

  • 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

相关问题