// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Extensions.Logging;
namespace Microsoft.Teams.Apps;
///
/// Provides backward-compatible logging methods (.Info(), .Error(), .Debug(), .Warn())
/// that delegate to an underlying instance.
///
/// The underlying logger to delegate to.
public class ContextLogger(ILogger logger)
{
///
/// Gets the underlying instance.
///
public ILogger Logger { get; } = logger;
///
/// Logs a message at the level.
///
/// The message arguments. The first string argument is used as the message template.
public void Info(params object?[] args)
{
ArgumentNullException.ThrowIfNull(args);
if (!Logger.IsEnabled(LogLevel.Information)) return;
Logger.LogInformation("{Message}", FormatArgs(args));
}
///
/// Logs a message at the level.
///
/// The message arguments. The first string argument is used as the message template.
public void Error(params object?[] args)
{
ArgumentNullException.ThrowIfNull(args);
if (!Logger.IsEnabled(LogLevel.Error)) return;
Logger.LogError("{Message}", FormatArgs(args));
}
///
/// Logs a message at the level.
///
/// The message arguments. The first string argument is used as the message template.
public void Debug(params object?[] args)
{
ArgumentNullException.ThrowIfNull(args);
if (!Logger.IsEnabled(LogLevel.Debug)) return;
Logger.LogDebug("{Message}", FormatArgs(args));
}
///
/// Logs a message at the level.
///
/// The message arguments. The first string argument is used as the message template.
public void Warn(params object?[] args)
{
ArgumentNullException.ThrowIfNull(args);
if (!Logger.IsEnabled(LogLevel.Warning)) return;
Logger.LogWarning("{Message}", FormatArgs(args));
}
private static string FormatArgs(object?[] args)
{
return args.Length switch
{
0 => string.Empty,
1 => args[0]?.ToString() ?? string.Empty,
_ => string.Join(" ", args.Select(a => a?.ToString() ?? "null"))
};
}
}