c# - 如何从 Function App 设置 session ID 或创建自定义字段到 Application Insights

标签 c# azure azure-functions azure-application-insights telemetry

功能应用如下:

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

local.json 文件具有 applicationInstrument key 。

如何添加附加字段并为应用程序洞察中的“请求”条目设置“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

enter image description here

更新 1

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

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

这将在 customDimension 属性下添加属性

enter image description here

您还可以refer here

关于c# - 如何从 Function App 设置 session ID 或创建自定义字段到 Application Insights,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027917/

相关文章:

.net - Azure 中的 ASP.Net 应用程序缓存

ruby-on-rails-3 - 使用 Capistrano 将 Ruby on Rails 应用程序部署到 Windows Azure VM 时出现超时错误

build - 使用 Azure 函数为 Windows 版本构建软件?

c# - 如何在 .NET 4 中使用 Console.CancelKeyPress? (在 .NET 3.5 及以下版本中工作正常)

c# - 如果FirstOrDefault返回null,则返回列表中的第一项

c# - 如何在 IHttpModule 中测试 HttpApplication 事件

azure - Visual Studio 2017 MSBuild 无法构建 Azure Functions

c# - 如何指定和使用真实的接口(interface)?

Azure AKS - 此容器服务处于失败状态

c# - Azure函数: System. Private.CoreLib:无法加载文件或程序集 'System.ComponentModel.Annotations...'