Клиент класса ниже необходимо добавить клиент .Name как свойство каждого журнала. Например, класс имеет только один метод client.ByuProduct("product1"), но его нужно будет использовать в любом другом методе.
Код: Выделить всё
public class Client
{
private readonly ILogger logger;
public string Name { get; set; }
private Dictionary loggedProperties = new();
public Client(ILogger logger, string name)
{
this.logger = logger;
Name = name;
// This is wrong unless the separate instances of logger is created or each Client instance
// loggerScope = logger.BeginPropertyScope(("customerName", Name));
loggedProperties.Add("ClientName", name);
}
///
/// Simulating that product was bought by the client
///
///
public void BuyProduct(string productId)
{
this.logger.LogInformationWithProps(loggedProperties, "Buying product:{ProductId}", productId);
}
}
Код: Выделить всё
///
/// Logs message with properties/structured logging
///
///
///
///
/// The identifier for the scope.
/// The type of the state to begin scope for.
public static void LogInformationWithProps(this ILogger logger, TState state, string? message, params object?[] args)
{
using (var scope = logger.BeginScope(state))
{
logger.Log(LogLevel.Information, message, args);
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... beginscope
Мобильная версия