azure - 从 Azure Function App 将自定义跟踪消息写入 Application Insight

标签 azure azure-functions azure-application-insights azure-monitoring

我正在开发 Azure Function App,希望添加自定义消息/跟踪,以帮助调试和提高性能。这是我正在使用的代码:

var telemetry = new Microsoft.ApplicationInsights.TelemetryClient();
telemetry.TrackTrace("Alert Button Pressed by Device ->"+CloudObject.A, SeverityLevel.Warning,new Dictionary<string, string> { { "IoT Object", IOTMESSAGE } });

但是当我转到 Application Insight 和查询跟踪(全部)时,我没有看到我正在设置的跟踪消息。

我做错了什么吗?

最佳答案

我用相同的代码重现并得到了预期的结果,如下:

 [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;
            var telemetry = new TelemetryClient();
            telemetry.TrackEvent("Loading HomeController-Index View");          
            telemetry.TrackTrace("Alert Button Pressed by Device ->" + CloudObject.A, SeverityLevel.Warning, new Dictionary<string, string> { { "IoT Object", IOTMESSAGE } });
            if (name == null)
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync<object>();
                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);
        }

应用洞察:

https://i.imgur.com/vWEqPNu.png

查询跟踪:

enter image description here

关于azure - 从 Azure Function App 将自定义跟踪消息写入 Application Insight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74593584/

相关文章:

azure - 如何根据消息类型将事件中心消息路由到不同的 Azure 函数

Azure Functions - 引用与 CLI 相同的库

azure - 使用 Incapsula,Application Insights 可用性失败,并显示服务器违反了协议(protocol)

azure - Ilog 和 TelemetryClient 在 Application Insights 中记录详细信息的区别

android - 适用于 Android 的 Azure Devops 管道 - 无法找到哈希字符串“android-23”的目标

Azure逻辑应用程序计算CSV文件中列的总和

ajax - Azure Web App不加载.json文件

azure - 有没有办法通过 Azure 功能向 Azure 服务主体发送电子邮件?

azure - 有没有办法在 host.json 文件中为 azure 函数设置 "extensionBundle"的批处理大小

azure - 如何以及在何处在网络核心 Web 应用程序中设置 TelemetryClient?