问题描述
我正在使用.Net中的Azure函数和WebJob,并使用TelemetryClient将日志发送到ApplicationInsights.
I'm using Azure function and WebJob in .Net and sending logs into ApplicationInsights using TelemetryClient.
对于WebJob和Azure函数,我具有几乎相同的日志记录代码.
I have almost same logging code for WebJob and Azure function.
对于azure函数,我可以在Requests,Traces和customMetrics中看到我的数据,但是对于WebJob,customMetrics中没有可用的数据,而仅在Traces和Requests中可用.
For azure function, I can see my data in Requests, Traces and customMetrics but for WebJob no data is available in customMetrics only available in Traces and Requests.
我的EventHub WebJob日志记录代码
My EventHub WebJob logging Code
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所需的Function相同的数据.
I can see below data for Function same I need for WebJob.
[![在此处输入图片描述] [1]] [1]
[![enter image description here][1]][1]
推荐答案
如果要将ApplicationInsights
与WebJob一起使用,则即使当前是beta版,也需要使用Microsoft.Azure.WebJobs.Logging.ApplicationInsights
nuget软件包.
If you want to use ApplicationInsights
with WebJob, you need use the Microsoft.Azure.WebJobs.Logging.ApplicationInsights
nuget package even it's a beta currently.
您总共需要三个软件包:
You need three packages in total:
- Microsoft.Azure.WebJobs.Logging.ApplicationInsights
- Microsoft.Extensions.Logging
- Microsoft.Extensions.Logging.Console
配置JobHostConfiguration
Configure the 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
.
Note: Don't forget adding the APPINSIGHTS_INSTRUMENTATIONKEY
in your application setting.
关于ILogger
过滤,您可以参考 Application Insights Integration
Wiki,CategoryLevels
允许您为特定类别指定日志级别,以便您可以微调日志记录输出.
About the ILogger
filtering, you could refer to the Application Insights Integration
wiki, CategoryLevels
allows you to specify log levels for specific categories so you can fine-tune the logging output.
您可以添加LogError
并添加代码:
And you could add the LogError
with code:
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...");
}
更新:
更新:
这篇关于ApplicationInsights customMetrics数据不适用于WebJob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!