本文介绍了向请求遥测添加自定义维度-Azure功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用v2.x创建一个新的Function应用程序,并且正在集成Application Insights以自动执行请求日志记录,因为Azure Function现在已与App Insights集成在一起(如文档链接).我需要做的是在Application Insights Request Telemetry中的自定义维度中记录几个自定义字段.是否可以不使用自定义请求"日志记录(使用TrackRequest方法)

I am creating a new Function app using v2.x and I am integrating Application Insights for request logging that is automatically being done as Azure Function is now integrated with App Insights (as mentioned in the documentation link). What I would need to do is log few custom fields in the custom dimensions in Application Insights Request Telemetry. Is it possible without using Custom Request logging (using TrackRequest method)

推荐答案

关于添加自定义属性,您可以参考此教程:添加属性:ITelemetryInitializer .以下是我测试的HTTP触发功能.

About adding custom properties, you could refer to this tutorial:Add properties: ITelemetryInitializer. The below is my test a HTTP trigger function.

public static class Function1
{
    private static string key = "Your InstrumentationKey";
    private static TelemetryClient telemetry = new TelemetryClient() { InstrumentationKey = key };
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        if (!telemetry.Context.Properties.ContainsKey("Function_appName"))
        {
            telemetry.Context.Properties.Add("Function_appName", "testfunc");
        }
        else
        {
            telemetry.Context.Properties["Function_appName"] = "testfunc";
        }

        telemetry.TrackEvent("eventtest");
        telemetry.TrackTrace("tracetest");

        string name = req.Query["name"];

        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
}

运行此功能后,转到Application Insights搜索可以检查数据或转到Logs(Analytics).

After running this function, go to the Application Insights Search could check the data Or go to Logs(Analytics).

更新:

这篇关于向请求遥测添加自定义维度-Azure功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:19