如何从 Function App 设置会话 ID 或创建自定义字段到 Application Insights

2024-05-30

功能应用程序如下:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequestMessage request, ILogger log)
    {
     log.LogInformation("Information", infoOBject);
    }

local.json 文件具有 applicationInstrument 密钥。

如何添加附加字段并为应用程序见解中的“请求”条目设置“Session_Id”。


您需要使用 Application Insights 中的一些自定义日志记录来实现此目的

首先,安装Nuget包

Install-Package Microsoft.ApplicationInsights -Version 2.7.2

然后像下面这样更改上面的代码

public static class Function1
    {
        private static TelemetryClient GetTelemetryClient()
        {
            var telemetryClient = new TelemetryClient();
            telemetryClient.InstrumentationKey = "<your actual insight instrumentkey>";
            telemetryClient.Context.Session.Id = "124556";
            //update 1-Custom properties- Start
            telemetry.Context.Properties["tags"] = "PROD";
            telemetry.Context.Properties["userCorelateId"]="1234"; 
            //update 1-Custom properties- Ends                
            return telemetryClient;
            }       


        [FunctionName("Function1")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)]HttpRequestMessage req, ILogger log)
        {
            var appInsights = GetTelemetryClient();           
            appInsights.TrackRequest(req.RequestUri.ToString(), DateTime.Now, Stopwatch.StartNew().Elapsed, "200", true);
            return req.CreateResponse(HttpStatusCode.OK, "message");

        }


    }

终于在appinsights中了

Update 1

您还可以在请求中添加您自己的附加属性。

E.g,
telemetry.Context.Properties["tags"] = "PROD";

这将添加以下属性customDimension特性

你也可以参考这里 https://cmatskas.com/azure-functions-custom-logging-with-appinsights/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Function App 设置会话 ID 或创建自定义字段到 Application Insights 的相关文章

随机推荐