Azure functions 如何在Azure Functions应用程序的应用程序洞察中获取用户_authenticatedID?

Azure functions 如何在Azure Functions应用程序的应用程序洞察中获取用户_authenticatedID?,azure-functions,azure-application-insights,Azure Functions,Azure Application Insights,我已经通过几种不同的资源添加了这一点,但它似乎不起作用。我想在向我的Azure函数发出请求时记录经过身份验证的用户。我可以从声明主体获得经过身份验证的用户。我按照文档注入遥测配置,并用配置实例化遥测客户端。我检查声明以查看它们是否为null,如果不是,则设置TelemetryClient.Context.User.AuthenticatedId=claimsPrincipal.Identity.Name。但是,在日志中,我无法看到正在填充的字段 class{ private readonl

我已经通过几种不同的资源添加了这一点,但它似乎不起作用。我想在向我的Azure函数发出请求时记录经过身份验证的用户。我可以从声明主体获得经过身份验证的用户。我按照文档注入遥测配置,并用配置实例化遥测客户端。我检查声明以查看它们是否为null,如果不是,则设置TelemetryClient.Context.User.AuthenticatedId=claimsPrincipal.Identity.Name。但是,在日志中,我无法看到正在填充的字段

class{
   private readonly TelemetryClient tc;
   public classConstructor(TelemetryConfiguration config){
      tc = new TelemetryClient(congig)
   }


    public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "Get", Route = 
    "user/session")] HttpRequest req, ILogger log, ClaimsPrincipal claimsPrincipal){

            //null check on claimsprincipal
            tc.Context.User.UserAuthenticatedId = claimsPrincipal?.Identity?.Name;

   }

startup class{
      builder.Services.AddSingleton<TelemetryConfiguration>(provider =>
           var telemetryConfiguration = new TelemetryConfiguration();
           telemetryConfiguration.instrumentationKey = "key"
           return telemetryConfiguration;
}

类{
专用只读遥测客户端tc;
公共类构造函数(遥测配置){
tc=新遥测客户端(congig)
}
公共异步任务运行([HttpTrigger(AuthorizationLevel.Anonymous,“Get”,路由=
“用户/会话”)]HttpRequest请求、ILogger日志、ClaimsPrincipal ClaimsPrincipal){
//claimsprincipal上的空检查
tc.Context.User.UserAuthenticatedId=claimsPrincipal?.Identity?.Name;
}
创业班{
builder.Services.AddSingleton(provider=>
var遥测配置=新遥测配置();
telemetryConfiguration.instrumentationKey=“key”
返回遥测配置;
}

我不确定要在哪里显示
遥测客户端.Context.User.AuthenticatedId

如果要在日志中查看,需要添加:

log.LogInformation(tc.Context.User.AuthenticatedUserId);
如果希望在应用程序洞察中看到它,则需要使用
TrackTrace
TrackEvent

tc.TrackEvent("---------track-event" + tc.Context.User.AuthenticatedUserId);
tc.TrackTrace("---------track-trace" + tc.Context.User.AuthenticatedUserId);
然后,您可以在application insights中看到它们(如下屏幕截图所示)

这里提供我的代码供您参考:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using System.Security.Claims;
using ikvm.runtime;
using Microsoft.Azure.WebJobs.Hosting;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;

namespace FunctionApp6
{
    public class Function1
    {
        private readonly TelemetryClient tc;

        public Function1(TelemetryConfiguration config)
        {
            this.tc = new TelemetryClient(config);
        }

        [FunctionName("Function1")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ClaimsPrincipal claimsPrincipal)
        {

            tc.Context.User.AuthenticatedUserId = "---userid---";

            log.LogInformation(tc.Context.User.AuthenticatedUserId);
            tc.TrackEvent("---------track-event" + tc.Context.User.AuthenticatedUserId);
            tc.TrackTrace("---------track-trace" + tc.Context.User.AuthenticatedUserId);

            string responseMessage = "This HTTP triggered function executed successfully";

            return new OkObjectResult(responseMessage);
        }

        public class MyStartup : IWebJobsStartup
        {
            public void Configure(IWebJobsBuilder builder)
            {
                var configDescriptor = builder.Services.SingleOrDefault(tc => tc.ServiceType == typeof(TelemetryConfiguration));
                if (configDescriptor?.ImplementationFactory != null)
                {
                    var implFactory = configDescriptor.ImplementationFactory;
                    builder.Services.Remove(configDescriptor);
                    builder.Services.AddSingleton(provider =>
                    {
                        if (implFactory.Invoke(provider) is TelemetryConfiguration config)
                        {
                            var newConfig = TelemetryConfiguration.CreateDefault();
                            newConfig.ApplicationIdProvider = config.ApplicationIdProvider;
                            newConfig.InstrumentationKey = config.InstrumentationKey;

                            return newConfig;
                        }
                        return null;
                    });
                }
            }
        }
    }
}
使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Microsoft.ApplicationInsights;
使用Microsoft.ApplicationInsights.Extensibility;
使用System.Security.Claims;
使用ikvm.runtime;
使用Microsoft.Azure.WebJobs.Hosting;
使用System.Linq;
使用Microsoft.Extensions.DependencyInjection;
命名空间函数PP6
{
公共类功能1
{
专用只读遥测客户端tc;
公共功能1(遥测配置)
{
this.tc=新遥测客户端(配置);
}
[功能名称(“功能1”)]
公共异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志,ClaimsPrincipal(ClaimsPrincipal)
{
tc.Context.User.AuthenticatedUserId=“--userid----”;
log.LogInformation(tc.Context.User.AuthenticatedUserId);
tc.TrackEvent(“-----------track event”+tc.Context.User.AuthenticatedUserId);
tc.TrackTrace(“-----------track trace”+tc.Context.User.AuthenticatedUserId);
string responseMessage=“此HTTP触发函数已成功执行”;
返回新的OkObjectResult(responseMessage);
}
公共类MyStartup:IWebJobsStartup
{
公共void配置(IWebJobsBuilder生成器)
{
var configDescriptor=builder.Services.SingleOrDefault(tc=>tc.ServiceType==typeof(遥测配置));
if(configDescriptor?.ImplementationFactory!=null)
{
var implFactory=configDescriptor.ImplementationFactory;
builder.Services.Remove(configDescriptor);
builder.Services.AddSingleton(provider=>
{
如果(implFactory.Invoke(provider)是遥测配置)
{
var newConfig=TelemetryConfiguration.CreateDefault();
newConfig.applicationdprovider=config.applicationdprovider;
newConfig.InstrumentationKey=config.InstrumentationKey;
返回newConfig;
}
返回null;
});
}
}
}
}
}