/// <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="formatter">A formatter to format 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,
            ITextFormatter formatter,
            string regionName                  = null,
            string logStreamNamePrefix         = 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,
                TextFormatter  = formatter,
                CreateLogGroup = createLogGroup
            };

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

            var client = CreateClient(regionName);

            return(loggerConfiguration.AmazonCloudWatch(options, client));
        }
        /// <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));
        }
Exemplo n.º 3
0
        public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, CloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period)
        {
            this.cloudWatchClient = cloudWatchClient;
            this.options          = options;
            renderer = options.LogEventRenderer ?? new RenderedMessageLogEventRenderer();

            UpdateLogStreamName();
        }
Exemplo n.º 4
0
        public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, CloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period)
        {
            this.cloudWatchClient = cloudWatchClient;
            this.options          = options;
            renderer = options.LogEventRenderer ?? new RenderedMessageLogEventRenderer();

            UpdateLogStreamName();

            traceLogger = new LoggerConfiguration().WriteTo.Trace().CreateLogger();
        }
        /// <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);
            IAmazonCloudWatchLogs client;

            if (regionName != null)
            {
                var region = RegionEndpoint.GetBySystemName(regionName);
                client = new AmazonCloudWatchLogsClient(credentials, region);
            }
            else
            {
                client = new AmazonCloudWatchLogsClient(credentials);
            }
            return(loggerConfiguration.AmazonCloudWatch(options, client));
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
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>
        /// <param name="loggerConfiguration">The LoggerSinkConfiguration to register this sink with.</param>
        /// <param name="options">Options to be used for the CloudWatch sink. <see cref="CloudWatchSinkOptions"/> for details.</param>
        /// <param name="cloudWatchClient">An AWS CloudWatch client which includes access to AWS and AWS specific settings like the AWS region.</param>
        /// <returns></returns>
        public static LoggerConfiguration AmazonCloudWatch(this LoggerSinkConfiguration loggerConfiguration, CloudWatchSinkOptions options, IAmazonCloudWatchLogs cloudWatchClient)
        {
            // validating input parameters
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (string.IsNullOrEmpty(options.LogGroupName))
            {
                throw new ArgumentException("options.LogGroupName");
            }
            if (cloudWatchClient == null)
            {
                throw new ArgumentNullException(nameof(cloudWatchClient));
            }

            // create the sink
            ILogEventSink sink = new CloudWatchLogSink(cloudWatchClient, options);

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