ApplicationInsights customMetrics 数据不适用于 WebJob
ApplicationInsights customMetrics data not available for WebJob
我在 .Net 中使用 Azure 函数和 WebJob,并使用 TelemetryClient 将日志发送到 ApplicationInsights。
我有几乎相同的 WebJob 和 Azure 函数日志记录代码。
对于 azure 函数,我可以在 Requests、Traces 和 customMetrics 中看到我的数据,但是对于 WebJob,在 customMetrics 中没有数据可用,仅在 Traces 和 Requests 中可用。
我的 EventHub WebJob 日志记录代码
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var eventData in messages)
{
Log log = LogManager.GetLogger();
RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "EventhubWebJob" };
requestTelemetry.Properties.Add("MessageId", Guid.NewGuid().ToString());
IOperationHolder<RequestTelemetry> operation = log.TelemetryClient.StartOperation(requestTelemetry);
operation.Telemetry.Success = true;
log.Trace($"Message processing start...");
try
{
log.Trace("Message received.");
Console.WriteLine($"Message received.");
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
log.Trace(ex.Message);
throw;
}
finally
{
log.Trace($"Message processing completed.");
log.TelemetryClient.StopOperation(operation);
}
}
return context.CheckpointAsync();
}
我可以在下面看到与 WebJob 所需的功能相同的数据。
[![在此处输入图片描述][1]][1]
如果您想将 ApplicationInsights
与 WebJob 一起使用,您需要使用 Microsoft.Azure.WebJobs.Logging.ApplicationInsights
nuget 包,即使它目前是测试版。
您总共需要三个包裹:
- Microsoft.Azure.WebJobs.Logging.ApplicationInsights
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Logging.控制台
配置 JobHostConfiguration
string instrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
if (!string.IsNullOrEmpty(instrumentationKey))
{
// build up a LoggerFactory with ApplicationInsights and a Console Logger
config.LoggerFactory = new LoggerFactory().AddApplicationInsights(instrumentationKey, null).AddConsole();
config.Tracing.ConsoleLevel = TraceLevel.Off;
}
注意:不要忘记在您的应用程序设置中添加 APPINSIGHTS_INSTRUMENTATIONKEY
。
关于 ILogger
过滤,您可以参考 Application Insights Integration
wiki,CategoryLevels
允许您为特定类别指定日志级别,以便您可以微调日志输出。
您可以使用代码添加 LogError
:
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
{
//you can directly use this line of code.
logger.LogError(new Exception(),"it is a test error...");
}
更新:
更新:
我在 .Net 中使用 Azure 函数和 WebJob,并使用 TelemetryClient 将日志发送到 ApplicationInsights。
我有几乎相同的 WebJob 和 Azure 函数日志记录代码。
对于 azure 函数,我可以在 Requests、Traces 和 customMetrics 中看到我的数据,但是对于 WebJob,在 customMetrics 中没有数据可用,仅在 Traces 和 Requests 中可用。
我的 EventHub WebJob 日志记录代码
public Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
{
foreach (var eventData in messages)
{
Log log = LogManager.GetLogger();
RequestTelemetry requestTelemetry = new RequestTelemetry { Name = "EventhubWebJob" };
requestTelemetry.Properties.Add("MessageId", Guid.NewGuid().ToString());
IOperationHolder<RequestTelemetry> operation = log.TelemetryClient.StartOperation(requestTelemetry);
operation.Telemetry.Success = true;
log.Trace($"Message processing start...");
try
{
log.Trace("Message received.");
Console.WriteLine($"Message received.");
}
catch (Exception ex)
{
operation.Telemetry.Success = false;
log.Trace(ex.Message);
throw;
}
finally
{
log.Trace($"Message processing completed.");
log.TelemetryClient.StopOperation(operation);
}
}
return context.CheckpointAsync();
}
我可以在下面看到与 WebJob 所需的功能相同的数据。
[![在此处输入图片描述][1]][1]
如果您想将 ApplicationInsights
与 WebJob 一起使用,您需要使用 Microsoft.Azure.WebJobs.Logging.ApplicationInsights
nuget 包,即使它目前是测试版。
您总共需要三个包裹:
- Microsoft.Azure.WebJobs.Logging.ApplicationInsights
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Logging.控制台
配置 JobHostConfiguration
string instrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
if (!string.IsNullOrEmpty(instrumentationKey))
{
// build up a LoggerFactory with ApplicationInsights and a Console Logger
config.LoggerFactory = new LoggerFactory().AddApplicationInsights(instrumentationKey, null).AddConsole();
config.Tracing.ConsoleLevel = TraceLevel.Off;
}
注意:不要忘记在您的应用程序设置中添加 APPINSIGHTS_INSTRUMENTATIONKEY
。
关于 ILogger
过滤,您可以参考 Application Insights Integration
wiki,CategoryLevels
允许您为特定类别指定日志级别,以便您可以微调日志输出。
您可以使用代码添加 LogError
:
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, ILogger logger)
{
//you can directly use this line of code.
logger.LogError(new Exception(),"it is a test error...");
}
更新:
更新: