/// <summary>
        /// Activates logging to AWS CloudWatch
        /// </summary>
        /// <remarks>This overload is intended to be used via AppSettings integration.</remarks>
        /// <param name="loggerConfiguration">The LoggerSinkConfiguration to register this sink with.</param>
        /// <param name="logGroupName">The log group name to be used in AWS CloudWatch.</param>
        /// <param name="logStreamNameProvider">The log stream name provider.</param>
        /// <param name="regionName">The system name of the region to which to write.</param>
        /// <param name="logEventRenderer">A renderer to render Serilog's LogEvent.</param>
        /// <param name="minimumLogEventLevel">The minimum log event level required in order to write an event to the sink.</param>
        /// <param name="batchSizeLimit">The batch size to be used when uploading logs to AWS CloudWatch.</param>
        /// <param name="period">The period to be used when a batch upload should be triggered.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="logGroupName"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="logStreamNameProvider"/> is <see langword="null"/>.</exception>
        public static LoggerConfiguration AmazonCloudWatch(
            this LoggerSinkConfiguration loggerConfiguration,
            string logGroupName,
            ILogStreamNameProvider logStreamNameProvider,
            string regionName = null,
            ILogEventRenderer logEventRenderer = null,
            LogEventLevel minimumLogEventLevel = CloudWatchSinkOptions.DefaultMinimumLogEventLevel,
            int batchSizeLimit  = CloudWatchSinkOptions.DefaultBatchSizeLimit,
            TimeSpan?period     = null,
            bool createLogGroup = CloudWatchSinkOptions.DefaultCreateLogGroup)
        {
            if (logGroupName == null)
            {
                throw new ArgumentNullException(nameof(logGroupName));
            }
            if (logStreamNameProvider == null)
            {
                throw new ArgumentNullException(nameof(logStreamNameProvider));
            }

            var options = new CloudWatchSinkOptions
            {
                LogGroupName         = logGroupName,
                MinimumLogEventLevel = minimumLogEventLevel,
                BatchSizeLimit       = batchSizeLimit,
                Period = period ?? CloudWatchSinkOptions.DefaultPeriod,
                LogStreamNameProvider = logStreamNameProvider,
                LogEventRenderer      = logEventRenderer,
                CreateLogGroup        = createLogGroup
            };

            var client = CreateClient(regionName);

            return(loggerConfiguration.AmazonCloudWatch(options, client));
        }
Esempio n. 2
0
        /// <summary>
        /// Add an Amazon CloudWatch sink to your Serilog <see cref="ILogger"/> instance
        /// </summary>
        /// <param name="loggerConfiguration">The configuration we will add the sink to</param>
        /// <param name="logGroup">Name of the Log Group that we will write to (i.e. 'MyLogs')</param>
        /// <param name="logStreamNameProvider">The log stream name provider to use to generate log stream names</param>
        /// <param name="restrictedToMinimumLevel">Minimum log level to write to CloudWatch</param>
        /// <param name="batchSizeLimit">Maximum number of CloudWatch events we will try to write in each batch (default: 100)</param>
        /// <param name="createLogGroup">Should we attempt to create the log group if it doesn't already exist?</param>
        /// <param name="batchUploadPeriodInSeconds">Maximum length of time that we will hold log events in the queue before triggering a write to CloudWatch (default, 10 seconds)</param>
        /// <param name="queueSizeLimit">Maximum number of log events we can hold in the queue before triggering a send to CloudWatch (default 10,000)</param>
        /// <param name="maxRetryAttempts">Maximum number of retry attempts we will make to write to CloudWatch before failing</param>
        /// <param name="logGroupRetentionPolicy">Retention policy for your Log Group (Default: 1 week (7 days))</param>
        /// <param name="textFormatter">The text formatter to use to format the logs (Defaults to <see cref="JsonFormatter"/> )</param>
        /// <param name="cloudWatchClient">
        /// Client to use to connect to AWS CloudWatch. Defaults to creating a new client which will follow the rules
        /// outlined in the <see href="https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/net-dg-config-creds.html">AWS documentation</see>.
        /// </param>
        /// <returns><see cref="LoggerConfiguration"/> which can be used to fluently add more config details to your log</returns>
        public static LoggerConfiguration AmazonCloudWatch(
            this LoggerSinkConfiguration loggerConfiguration,
            string logGroup,
            ILogStreamNameProvider logStreamNameProvider,
            LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
            int batchSizeLimit             = 100,
            int batchUploadPeriodInSeconds = 10,
            bool createLogGroup            = true,
            int queueSizeLimit             = 10000,
            byte maxRetryAttempts          = 5,
            LogGroupRetentionPolicy logGroupRetentionPolicy = LogGroupRetentionPolicy.OneWeek,
            ITextFormatter textFormatter           = null,
            IAmazonCloudWatchLogs cloudWatchClient = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }

            if (String.IsNullOrWhiteSpace(logGroup))
            {
                throw new ArgumentException("You must provide a log group name (like: 'your-application/your-component')", nameof(logGroup));
            }

            if (logStreamNameProvider == null)
            {
                throw new ArgumentNullException(nameof(logStreamNameProvider), "You must provide a log stream name provider (like DefaultLogStreamProvider)");
            }

            var options = new CloudWatchSinkOptions
            {
                BatchSizeLimit        = batchSizeLimit,
                LogGroupName          = logGroup,
                LogStreamNameProvider = logStreamNameProvider,
                CreateLogGroup        = createLogGroup,
                MinimumLogEventLevel  = restrictedToMinimumLevel,
                Period                  = TimeSpan.FromSeconds(batchUploadPeriodInSeconds),
                QueueSizeLimit          = queueSizeLimit,
                RetryAttempts           = maxRetryAttempts,
                LogGroupRetentionPolicy = logGroupRetentionPolicy,
                TextFormatter           = textFormatter ?? new JsonFormatter()
            };

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var client = cloudWatchClient ?? new AmazonCloudWatchLogsClient();

            // Create and register the sink
            var sink = new CloudWatchLogSink(client, options);

            return(loggerConfiguration.Sink(sink, options.MinimumLogEventLevel));
        }