Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (36 sloc) 1.54 KB
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace AsyncLocal.SimpleInjector.Web.Controllers
{
public class LoggingDecorator : ICustomerService
{
private readonly Func<ICustomerService> _decorateeFunc;
private readonly ILogger<CustomerService> _logger;
private readonly CorrelationContainer _correlationContainer;
public LoggingDecorator(
Func<ICustomerService> decorateeFunc,
ILogger<CustomerService> logger,
CorrelationContainer correlationContainer)
{
_decorateeFunc = decorateeFunc;
_logger = logger;
_correlationContainer = correlationContainer;
}
public async Task<Customer> GetCustomer(int customerId)
{
// Get async local correlation id and log.
var correlationIdBeforeAwait = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Getting customer by id {customerId} ({correlationIdBeforeAwait})");
// Call decoratee.
var decoratee = _decorateeFunc.Invoke();
var customer = await decoratee.GetCustomer(customerId);
// For demo purposes: get correlation id again after await and log.
var correlationIdAfterAwait = _correlationContainer.GetCorrelationId();
_logger.LogWarning($"Retrieved customer by id {customerId} ({correlationIdBeforeAwait})");
// Return values.
return customer;
}
}
}
You can’t perform that action at this time.