ApplicationInsights customMetrics data not availab

2019-08-17 05:56发布

问题:

I'm using Azure function and WebJob in .Net and sending logs into ApplicationInsights using TelemetryClient.

I have almost same logging code for WebJob and Azure function.

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.

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();
    }

I can see below data for Function same I need for WebJob.

回答1:

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:

  1. Microsoft.Azure.WebJobs.Logging.ApplicationInsights
  2. Microsoft.Extensions.Logging
  3. Microsoft.Extensions.Logging.Console

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;
}

Note: Don't forget adding the APPINSIGHTS_INSTRUMENTATIONKEY in your application setting.

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.

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...");
    }

Update:

Update: