using AWS.Logger; using AWS.Logger.AspNetCore; using Microsoft.Extensions.Configuration; using System; namespace Microsoft.Extensions.Logging { /// /// Extensions methods for ILoggerFactory to add the AWS logging provider /// public static class AWSLoggerFactoryExtensions { /// /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig /// /// /// Configuration on how to connect to AWS and how the log messages should be sent. /// A custom formatter which accepts a LogLevel, a state, and an exception and returns the formatted log message. /// public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, AWSLoggerConfig config, Func formatter = null) { // If config is null. Assuming the logger is being activated in a debug environment // and skip adding the provider. We don't want to prevent developers running their application // locally because they don't have access or want to use AWS for their local development. if (config == null) { factory.CreateLogger("AWS.Logging.AspNetCore").LogWarning("AWSLoggerConfig is null, skipping adding AWS Logging provider."); return factory; } var provider = new AWSLoggerProvider(config, formatter); factory.AddProvider(provider); return factory; } /// /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig /// /// /// Configuration and loglevels on how to connect to AWS and how the log messages should be sent. /// A custom formatter which accepts a LogLevel, a state, and an exception and returns the formatted log message. /// public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, AWSLoggerConfigSection configSection, Func formatter = null) { // If configSection is null. Assuming the logger is being activated in a debug environment // and skip adding the provider. We don't want to prevent developers running their application // locally because they don't have access or want to use AWS for their local development. if (configSection == null) { factory.CreateLogger("AWS.Logging.AspNetCore").LogWarning("AWSLoggerConfigSection is null. LogGroup is likely not configured in config files. Skipping adding AWS Logging provider."); return factory; } var provider = new AWSLoggerProvider(configSection, formatter); factory.AddProvider(provider); return factory; } /// /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig /// /// /// Configuration on how to connect to AWS and how the log messages should be sent. /// The minimum log level for messages to be written. /// public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, AWSLoggerConfig config, LogLevel minLevel) { var provider = new AWSLoggerProvider(config, minLevel); factory.AddProvider(provider); return factory; } /// /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig /// /// /// Configuration on how to connect to AWS and how the log messages should be sent. /// A filter function that has the logger category name and log level which can be used to filter messages being sent to AWS. /// public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, AWSLoggerConfig config, Func filter) { var provider = new AWSLoggerProvider(config, filter); factory.AddProvider(provider); return factory; } } }