protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogInformation("Worker start loop at: {time}", DateTimeOffset.Now);
await httpClient.GetAsync("http://microsoft.com", stoppingToken);
_logger.LogInformation("Worker end loop at: {time}", DateTimeOffset.Now);
}
}
Logs are not correlated with or without app insights.
info: WorkerWithScopes.Worker[0]
Worker end loop at: 07/19/2019 11:23:49 -07:00
info: WorkerWithScopes.Worker[0]
Worker start loop at: 07/19/2019 11:23:49 -07:00
WorkerScope works on top of System.Diagnostics.Activity
and can work along with HttpClient and Azure SDKs.
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
using var scope = _logger.BeginWorkerScope();
_logger.LogInformation("Worker start loop at: {time}", DateTimeOffset.Now);
await httpClient.GetAsync("http://microsoft.com", stoppingToken);
_logger.LogInformation("Worker end loop at: {time}", DateTimeOffset.Now);
}
}
or
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var message = (await _serviceBusReceiver.ReceiveAsync(1)).Single();
using var scope = _logger.BeginWorkerScope(message.ExtractActivity());
_logger.LogInformation("Worker start loop at: {time}", DateTimeOffset.Now);
await httpClient.GetAsync("http://microsoft.com", stoppingToken);
_logger.LogInformation("Worker end loop at: {time}", DateTimeOffset.Now);
}
}
info: WorkerWithScopes.Worker[0]
=> SpanId:9d2f81c429565d48, TraceId:50baf6cd0b6697439cf60104e0315515, ParentId:
Worker start loop at: 07/18/2019 20:22:08 -07:00
info: WorkerWithScopes.Worker[0]
=> SpanId:9d2f81c429565d48, TraceId:50baf6cd0b6697439cf60104e0315515, ParentId:
Worker end loop at: 07/18/2019 20:22:08 -07:00