I have developed a background service in .NET 8.0 which is responsible for receiving messages from an Azure Service Bus queue. The Program.cs
has Application Insights configured as you can see in the code snippet below.
Is it possible to see in Application Insights the average response time of my background service? In the Performance section I can only see response times from API calls.
// Program.cs of the background service
services.AddApplicationInsightsTelemetryWorkerService(new ApplicationInsightsServiceOptions
{
ConnectionString = "InstrumentationKey=...",
EnableAdaptiveSampling = false
});
// ReceiptsBackgroundService.cs
public async Task Handle(Message message, CancellationToken cancellationToken)
{
using (IServiceScope scope = _serviceScopeFactory.CreateScope())
{
var scopedProcessingService = scope.ServiceProvider
.GetRequiredService<IReceiptsScopedProcessingService>();
await scopedProcessingService.DoWorkAsync(message, cancellationToken);
}
await _queueClient.CompleteAsync(message.SystemProperties.LockToken)
.ConfigureAwait(false);
}
// ReceiptsScopedProcessingService.cs
public async Task DoWorkAsync(Message message, CancellationToken stoppingToken)
{
// Do work
_logger.Log(LogLevel.Information, "Processing successful.", "Processing successful.");
}
In Application Insights, in the Performance section, I only see the publisher application's performance.
s it possible to see in Application Insights the average response time of my background service?
I use below code to get the duration of the background service :
using Azure.Messaging.ServiceBus;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var rith_app_in_con_strg = "InstrumentationKey=e9961rithwikd91;IngestionEndpoint=https://canadacentral-1.in.applicationinsights.azure.com/;LiveEndpoint=https://canadacentral.livediagnostics.monitor.azure.com/;ApplicationId=f3ef2c6c-0e02-4c15-9329-204d8861b80a";
var rith_serv_bus_con_strg = "Endpoint=sb://demoser898.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=+Wolhrithwikyg=";
var sb_qnme = "myqueue";
var ri_sp = new ServiceCollection()
.AddLogging(riblder =>
{
riblder.AddApplicationInsights(rith_app_in_con_strg);
riblder.SetMinimumLevel(LogLevel.Information);
riblder.AddConsole();
})
.AddSingleton<TelemetryClient>(ri_sp =>
{
var tc = new TelemetryConfiguration
{
ConnectionString = rith_app_in_con_strg
};
return new TelemetryClient(tc);
})
.BuildServiceProvider();
var ri_lg = ri_sp.GetRequiredService<ILogger<Program>>();
var ri_tc = ri_sp.GetRequiredService<TelemetryClient>();
ri_tc.TrackEvent("TestEvent", new Dictionary<string, string> { { "TestKey", "TestValue" } });
ri_tc.Flush();
var sbc = new ServiceBusClient(rith_serv_bus_con_strg);
var pr = sbc.CreateProcessor(sb_qnme);
async Task MessageHandler(ProcessMessageEventArgs args)
{
var msg = args.Message.Body.ToString();
ri_lg.LogInformation("Hello Rithwik, Received message: {Message}", msg);
var timer = Stopwatch.StartNew();
var startTime = DateTime.UtcNow;
try
{
await Task.Delay(2000);
var dependencyTelemetry = new DependencyTelemetry
{
Name = "Azure Service Bus Message Processing",
Target = sb_qnme,
Data = args.Message.MessageId,
Timestamp = startTime,
Duration = timer.Elapsed,
Success = true,
Type = "Queue"
};
ri_tc.TrackDependency(dependencyTelemetry);
await args.CompleteMessageAsync(args.Message);
ri_lg.LogInformation("Message processed successfully.");
}
catch (Exception ex)
{
ri_lg.LogError(ex, "Hello Rithwik, There is an Error processing message");
ri_tc.TrackException(ex);
ri_tc.Flush();
}
}
Task ErrorHandler(ProcessErrorEventArgs args)
{
ri_lg.LogError(args.Exception, "Hello Rithwik, Error occurred in message processor, check it");
ri_tc.TrackException(args.Exception);
ri_tc.Flush();
return Task.CompletedTask;
}
pr.ProcessMessageAsync += MessageHandler;
pr.ProcessErrorAsync += ErrorHandler;
await pr.StartProcessingAsync();
Console.WriteLine("Hello Rithwik, Press any key to stop the App");
Console.ReadKey();
await pr.StopProcessingAsync();
await sbc.DisposeAsync();
ri_tc.Flush();
await Task.Delay(1000);
Console.WriteLine("Hello Rithwik, Processing stopped.");
}
}
Output: