/// <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="accessKey">The access key to use to access AWS CloudWatch.</param>
        /// <param name="secretAccessKey">The secret access key to use to access AWS CloudWatch.</param>
        /// <param name="regionName">The system name of the region to which to write.</param>
        /// <param name="logStreamNamePrefix">The log stream name prefix. Will use default log stream name if leave empty.</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="accessKey"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="secretAccessKey"/> is <see langword="null"/>.</exception>
        public static LoggerConfiguration AmazonCloudWatch(
            this LoggerSinkConfiguration loggerConfiguration,
            string logGroupName,
            string accessKey,
            string secretAccessKey,
            string regionName                  = null,
            string logStreamNamePrefix         = 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 (accessKey == null)
            {
                throw new ArgumentNullException(nameof(accessKey));
            }
            if (secretAccessKey == null)
            {
                throw new ArgumentNullException(nameof(secretAccessKey));
            }

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

            if (!String.IsNullOrWhiteSpace(logStreamNamePrefix))
            {
                options.LogStreamNameProvider = new ConstantLogStreamNameProvider(logStreamNamePrefix);
            }

            var credentials = new BasicAWSCredentials(accessKey, secretAccessKey);
            var client      = CreateClient(credentials, regionName);

            return(loggerConfiguration.AmazonCloudWatch(options, client));
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CloudWatchLogSink"/> class.
        /// </summary>
        /// <param name="cloudWatchClient">The cloud watch client.</param>
        /// <param name="options">The options.</param>
        public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, CloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period)
        {
            if (options.BatchSizeLimit < 1)
            {
                throw new ArgumentException($"{nameof(CloudWatchSinkOptions)}.{nameof(options.BatchSizeLimit)} must be a value greater than 0.");
            }
            this.cloudWatchClient = cloudWatchClient;
            this.options          = options;

            if (options.LogEventRenderer != null && options.TextFormatter != null)
            {
                throw new System.InvalidOperationException($"{nameof(options.LogEventRenderer)} and {nameof(options.TextFormatter)} cannot both be applied");
            }

            this.renderer = options.TextFormatter != null
                ? new TextFormatterLogEventRenderer(options.TextFormatter)
                : (options.LogEventRenderer ?? new RenderedMessageLogEventRenderer());
        }
        /// <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="regionName">The system name of the region to which to write.</param>
        /// <param name="logStreamNamePrefix">The log stream name prefix. Will use default log stream name if leave empty.</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>
        public static LoggerConfiguration AmazonCloudWatch(
            this LoggerSinkConfiguration loggerConfiguration,
            string logGroupName,
            string regionName                  = null,
            string logStreamNamePrefix         = 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));
            }

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

            if (!String.IsNullOrWhiteSpace(logStreamNamePrefix))
            {
                options.LogStreamNameProvider = new ConstantLogStreamNameProvider(logStreamNamePrefix);
            }

            IAmazonCloudWatchLogs client;

            if (regionName != null)
            {
                var region = RegionEndpoint.GetBySystemName(regionName);
                client = new AmazonCloudWatchLogsClient(region);
            }
            else
            {
                client = new AmazonCloudWatchLogsClient();
            }
            return(loggerConfiguration.AmazonCloudWatch(options, client));
        }