我正在使用 v2.x 创建一个新的 Function 应用程序,并且正在集成 Application Insights 以进行请求日志记录,这是自动完成的,因为 Azure Function 现在已与 App Insights 集成(如文档中所述)link)。我需要做的是在 Application Insights 请求遥测中的自定义维度中记录一些自定义字段。是否可以不使用自定义请求日志记录(使用TrackRequest
method)
关于添加自定义属性,可以参考这个教程:添加属性:ITelemetryInitializer。下面是我测试的一个HTTP触发功能。
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 Search可以检查数据或转到Logs(Analytics)。
Update:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)