Azure 具有多个应用程序的应用程序洞察
我有一个Application Insights,它记录来自一个应用程序服务和一个应用程序功能的跟踪(一个资源用于两个功能)。 我需要根据资源(应用程序服务或应用程序功能)过滤跟踪,如果可能的话,还需要根据实际记录的应用程序功能过滤跟踪。 查看跟踪,我看到以下属性列表: 我想在Azure 具有多个应用程序的应用程序洞察,azure,logging,azure-functions,azure-web-app-service,azure-application-insights,Azure,Logging,Azure Functions,Azure Web App Service,Azure Application Insights,我有一个Application Insights,它记录来自一个应用程序服务和一个应用程序功能的跟踪(一个资源用于两个功能)。 我需要根据资源(应用程序服务或应用程序功能)过滤跟踪,如果可能的话,还需要根据实际记录的应用程序功能过滤跟踪。 查看跟踪,我看到以下属性列表: 我想在appName属性中查找资源名称,但实际上是Application Insights资源名称,这对我来说是无用的,因为所有这些跟踪都来自该资源 注意:我不喜欢在消息中设置前缀来过滤跟踪的解决方法 更新 我遵循Peter
appName
属性中查找资源名称,但实际上是Application Insights资源名称,这对我来说是无用的,因为所有这些跟踪都来自该资源
注意:我不喜欢在消息中设置前缀来过滤跟踪的解决方法
更新
我遵循Peter Bon的建议,创建了一个全新的Function V3项目。该项目的基本版本在没有遥测初始值设定项的情况下也可以工作,我的意思是Cloud_RoleName属性已正确填充。
然后,我添加了修改以适应示例代码,我发现在注入新的遥测客户端时出现了问题。我知道,不建议在应用程序函数中手动注入遥测客户端,但我绝对需要将自定义事件发送到Application Insights,据我所知,在应用程序函数中默认使用ILogger接口是不可能的
Startup.cs
public class Startup : FunctionsStartup
{
private TelemetryConfiguration telemetryConfiguration;
public override void Configure(IFunctionsHostBuilder builder)
{
var localRoot = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
var azureRoot = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
var configBuilder = new ConfigurationBuilder()
.SetBasePath(localRoot ?? azureRoot)
.AddEnvironmentVariables()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true);
var configuration = configBuilder.Build();
if (builder != null)
{
this.ConfigureServices(builder.Services, configuration);
}
}
private void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<ITelemetryInitializer>(x => new CustomTelemetryInitializer(configuration["appFunctionName"]));
telemetryConfiguration = new TelemetryConfiguration(configuration["APPINSIGHTS_INSTRUMENTATIONKEY"]);
telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
var telemetryClient = new TelemetryClient(telemetryConfiguration);
services.AddSingleton(telemetryClient);
services.AddSingleton<ISampleInterface, SampleService>();
}
}
SampleService.cs
public class SampleService : ISampleInterface
{
private TelemetryClient telemetryClient;
public SampleService(TelemetryClient telemetryClient)
{
this.telemetryClient = telemetryClient;
}
public void TestAppInsights()
{
telemetryClient.TrackEvent("Sample Custom Event with init");
telemetryClient.TrackTrace("Sample Custom Trace with init");
}
}
Function.cs
public class Function1
{
private ISampleInterface service;
public Function1(ISampleInterface service)
{
this.service = service;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request with init.");
this.service.TestAppInsights();
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}
公共类函数1
{
专用接口服务;
公共功能1(ISampleInterface服务)
{
服务=服务;
}
[功能名称(“功能1”)]
公共异步任务运行(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数使用init处理请求”);
this.service.testappings();
字符串名称=请求查询[“名称”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
string responseMessage=string.IsNullOrEmpty(名称)
?“此HTTP触发的函数已成功执行。请在查询字符串或请求正文中为个性化响应传递名称。”
:$“您好,{name}。此HTTP触发的函数已成功执行。”;
返回新的OkObjectResult(responseMessage);
}
}
检查可供所有遥测的cloud\u RoleName
属性如何?默认情况下,它将以webapp或函数的名称(包括插槽名称)作为值
否则,如果要在一个位置添加自定义特性或修改所有遥测的特性,可以使用,如所示:
使用Microsoft.ApplicationInsights.Channel;
使用Microsoft.ApplicationInsights.Extensibility;
命名空间CustomInitializer.Telemetry
{
公共类MyTelemetryInitializer:ITelemetryInitializer
{
公共无效初始化(ITelemetry遥测)
{
telemetry.Context.Cloud.RoleName=“HttpTriggered”;
}
}
}
这避免了必须为所有记录道添加前缀,正如您所提到的,这是一种解决方法,因为所有遥测都要通过一段代码:
另一件事
[…]但我绝对需要将自定义事件发送到Application Insights,据我所知,在应用程序函数中默认使用ILogger接口是不可能的
请注意,您可以使用
ILogger
接口将输出重定向到Application Insights。它将显示为跟踪。你说得对,仔细查看跟踪,我发现只有来自应用程序功能的跟踪没有cloud\u RoleName
属性。遥测串行化器是一个很好的解决方案,但似乎无法正常工作。我按照您的建议创建了Nizializer,并在启动时注册了它,但是跟踪还没有cloud_RoleName属性。可能是因为我正在使用应用程序函数ILogger接口…@user2297037您使用的azure函数运行时是什么?它应该可以与SDK 3.0.3版的功能一起工作。我不知道您的代码是什么样子,但是。你可以看看它,看看在设置函数时是否遗漏了什么Hey Peter,我认为你的解决方案是正确的,但我认为它不适用于v3函数。谢谢你的帮助。
public class Function1
{
private ISampleInterface service;
public Function1(ISampleInterface service)
{
this.service = service;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request with init.");
this.service.TestAppInsights();
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
}