如何在 Azure Functions App 的 Application Insights 中获取 user_authenticatedID?
How to get user_authenticatedID in Application Insights for Azure Functions App?
我已经通过几个不同的资源来添加它,但它似乎没有用。我想在向我的 Azure 函数发出请求时记录经过身份验证的用户。我可以从声明主体中获得经过身份验证的用户。我按照文档注入遥测配置并使用配置实例化 TelemetryClient。我检查声明以查看它们是否为空,如果不是,我设置 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;
}
我不确定你想在哪里展示 TelemetryClient.Context.User.AuthenticatedId
。
如果想在Logs中看到,需要添加:
log.LogInformation(tc.Context.User.AuthenticatedUserId);
如果想在application insights中看到,需要使用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;
});
}
}
}
}
}
我已经通过几个不同的资源来添加它,但它似乎没有用。我想在向我的 Azure 函数发出请求时记录经过身份验证的用户。我可以从声明主体中获得经过身份验证的用户。我按照文档注入遥测配置并使用配置实例化 TelemetryClient。我检查声明以查看它们是否为空,如果不是,我设置 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;
}
我不确定你想在哪里展示 TelemetryClient.Context.User.AuthenticatedId
。
如果想在Logs中看到,需要添加:
log.LogInformation(tc.Context.User.AuthenticatedUserId);
如果想在application insights中看到,需要使用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;
});
}
}
}
}
}