Azure 功能未记录自定义事件、对应用洞察的依赖性
Azure function is not logging custom events, dependencies to app insights
我们在 C#
中开发了 Azure Function V3
。我们尝试将一些 custom events, dependencies
等记录到 Azure Application Insights
。我们无法使用 TelemetryClient
登录应用洞察。代码运行良好,没有任何错误。此外,可以看到从配置文件中检索到 instrumentation key
。但是 Ilogger
日志可以在应用洞察跟踪 table 中找到。请在下面找到我们使用的代码,
public class CommunityCreate
{
private readonly TelemetryClient telemetryClient;
public CommunityCreate(TelemetryConfiguration telemetryConfiguration)
{
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
[FunctionName("Function1")]
[return: ServiceBus("sample", Connection = "ServiceBusProducerConnection")]
public async Task<string> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "Account/{id:int?}")] HttpRequest req, string id, ILogger log)
{
//log.LogInformation("C# HTTP trigger function processed a request.");
DateTime start = DateTime.UtcNow;
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
var evt = new EventTelemetry("Function called");
evt.Context.User.Id = name;
this.telemetryClient.TrackEvent(evt);
// Generate a custom metric, in this case let's use ContentLength.
this.telemetryClient.GetMetric("contentLength").TrackValue(req.ContentLength);
// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "GET api/planets/1/",
Target = "swapi.co",
Data = "https://swapi.co/api/planets/1/",
Timestamp = start,
Duration = DateTime.UtcNow - start,
Success = true
};
dependency.Context.User.Id = name;
this.telemetryClient.TrackDependency(dependency);
telemetryClient.TrackEvent("Ack123 Recieved");
telemetryClient.TrackMetric("Test Metric", DateTime.Now.Millisecond);
return name;
}
}
请确保您使用的是正确的软件包,如下所示:
Microsoft.Azure.WebJobs.Logging.ApplicationInsights, version 3.0.18
和
更新包Microsoft.NET.Sdk.Functions
最新版本3.0.9.
如果您是运行本地项目,请在local.settings.json
中添加APPINSIGHTS_INSTRUMENTATIONKEY
,如下所示:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
}
}
或者,如果您 运行 它在 azure 门户上,请使用 azure 功能配置 Application insights。
然后我测试了您的代码,自定义事件或依赖项已正确登录到 Application insights 中。这是屏幕截图:
如果问题仍然存在,请告诉我(并提供更多详细信息)。
我们在 C#
中开发了 Azure Function V3
。我们尝试将一些 custom events, dependencies
等记录到 Azure Application Insights
。我们无法使用 TelemetryClient
登录应用洞察。代码运行良好,没有任何错误。此外,可以看到从配置文件中检索到 instrumentation key
。但是 Ilogger
日志可以在应用洞察跟踪 table 中找到。请在下面找到我们使用的代码,
public class CommunityCreate
{
private readonly TelemetryClient telemetryClient;
public CommunityCreate(TelemetryConfiguration telemetryConfiguration)
{
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
}
[FunctionName("Function1")]
[return: ServiceBus("sample", Connection = "ServiceBusProducerConnection")]
public async Task<string> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "Account/{id:int?}")] HttpRequest req, string id, ILogger log)
{
//log.LogInformation("C# HTTP trigger function processed a request.");
DateTime start = DateTime.UtcNow;
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
var evt = new EventTelemetry("Function called");
evt.Context.User.Id = name;
this.telemetryClient.TrackEvent(evt);
// Generate a custom metric, in this case let's use ContentLength.
this.telemetryClient.GetMetric("contentLength").TrackValue(req.ContentLength);
// Log a custom dependency in the dependencies table.
var dependency = new DependencyTelemetry
{
Name = "GET api/planets/1/",
Target = "swapi.co",
Data = "https://swapi.co/api/planets/1/",
Timestamp = start,
Duration = DateTime.UtcNow - start,
Success = true
};
dependency.Context.User.Id = name;
this.telemetryClient.TrackDependency(dependency);
telemetryClient.TrackEvent("Ack123 Recieved");
telemetryClient.TrackMetric("Test Metric", DateTime.Now.Millisecond);
return name;
}
}
请确保您使用的是正确的软件包,如下所示:
Microsoft.Azure.WebJobs.Logging.ApplicationInsights, version 3.0.18
和
更新包Microsoft.NET.Sdk.Functions
最新版本3.0.9.
如果您是运行本地项目,请在local.settings.json
中添加APPINSIGHTS_INSTRUMENTATIONKEY
,如下所示:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "xxxx",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"APPINSIGHTS_INSTRUMENTATIONKEY": "xxx"
}
}
或者,如果您 运行 它在 azure 门户上,请使用 azure 功能配置 Application insights。
然后我测试了您的代码,自定义事件或依赖项已正确登录到 Application insights 中。这是屏幕截图:
如果问题仍然存在,请告诉我(并提供更多详细信息)。