-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathWorker.cs
More file actions
37 lines (31 loc) · 1.19 KB
/
Worker.cs
File metadata and controls
37 lines (31 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using HeavyArrayForecast;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
namespace ServiceProfilerInWorkerNet6;
public class Worker : BackgroundService
{
private readonly TelemetryClient _telemetryClient;
private readonly ILogger _logger;
public Worker(
TelemetryClient telemetryClient,
ILogger<Worker> logger)
{
_telemetryClient = telemetryClient ?? throw new ArgumentNullException(nameof(telemetryClient));
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
// These request operations will be captured by the profiler
using (_telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
_ = WeatherForecastHelper.GetForecasts();
_logger.LogInformation("Wether reported.");
}
// Wait for 10 seconds, do not repeat too frequently.
await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
}
}
}