using Amazon.Lambda.Logging.AspNetCore; using System; using System.Collections.Concurrent; namespace Microsoft.Extensions.Logging { /// /// The ILoggerProvider implementation that is added to the ASP.NET Core logging system to create loggers /// that will send the messages to the CloudWatch LogGroup associated with this Lambda function. /// internal class LambdaILoggerProvider : ILoggerProvider, ISupportExternalScope { // Private fields private readonly LambdaLoggerOptions _options; private IExternalScopeProvider _scopeProvider; private readonly ConcurrentDictionary _loggers; // Constants private const string DEFAULT_CATEGORY_NAME = "Default"; /// /// Creates the provider /// /// public LambdaILoggerProvider(LambdaLoggerOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } _options = options; _loggers = new ConcurrentDictionary(); _scopeProvider = options.IncludeScopes ? new LoggerExternalScopeProvider() : NullExternalScopeProvider.Instance; } /// /// Creates the logger with the specified category. /// /// /// public ILogger CreateLogger(string categoryName) { var name = string.IsNullOrEmpty(categoryName) ? DEFAULT_CATEGORY_NAME : categoryName; return _loggers.GetOrAdd(name, loggerName => new LambdaILogger(name, _options) { ScopeProvider = _scopeProvider }); } /// public void SetScopeProvider(IExternalScopeProvider scopeProvider) { _scopeProvider = scopeProvider; foreach (var logger in _loggers) { logger.Value.ScopeProvider = _scopeProvider; } } /// /// /// public void Dispose() { } } }