Esempio n. 1
0
        /// <summary>
        /// Construct an instance of AWSLoggerCore
        /// </summary>
        /// <param name="config">Configuration options for logging messages to AWS</param>
        /// <param name="logType">Logging Provider Name to include in UserAgentHeader</param>
        public AWSLoggerCore(AWSLoggerConfig config, string logType)
        {
            _config  = config;
            _logType = logType;

            var awsConfig = new AmazonCloudWatchLogsConfig();

            if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
            {
                var serviceUrl = _config.ServiceUrl.Trim();
                awsConfig.ServiceURL = serviceUrl;
                if (serviceUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
                {
                    awsConfig.UseHttp = true;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(_config.Region))
                {
                    awsConfig.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(_config.Region);
                }
            }

            var credentials = DetermineCredentials(config);

            _client = new AmazonCloudWatchLogsClient(credentials, awsConfig);


            ((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;
            ((AmazonCloudWatchLogsClient)this._client).ExceptionEvent     += ServiceClienExceptionEvent;

            StartMonitor();
            RegisterShutdownHook();
        }
Esempio n. 2
0
        public CloudwatchLogsSink(IAmazonCloudWatchLogs cwl, string logGroupName = null, string logStreamName = null)
        {
            logGroupName  = logGroupName ?? "acl";
            logStreamName = logStreamName ?? $"{AlteredEnvironment.App}/{AlteredEnvironment.Env}/{AlteredEnvironment.Sha}/{Guid.NewGuid()}";

            var createLogRequest = new CreateLogStreamRequest
            {
                LogGroupName  = logGroupName,
                LogStreamName = logStreamName
            };

            subscription =
                (from createLogResponse in cwl.CreateLogStreamAsync(createLogRequest).ToObservable()
                 from logBatch in logs
                 .SubscribeOn(NewThreadScheduler.Default)
                 .Buffer(LogRate, MaxLogs)
                 where logBatch.Count > 0
                 let logEvents = (from log in logBatch
                                  select new InputLogEvent
            {
                Timestamp = DateTime.UtcNow,
                Message = $"{log}"
            }).ToList()
                                 let putLogsRequest = new PutLogEventsRequest
            {
                LogGroupName = logGroupName,
                LogStreamName = logStreamName,
                SequenceToken = lastSequenceToken.Value,
                LogEvents = logEvents
            }
                 from putLogsResponse in cwl.PutLogEventsAsync(putLogsRequest)
                 //let putLogsResponse = cwl.PutLogEventsAsync(putLogsRequest).Result
                 select putLogsResponse.NextSequenceToken)
                .Subscribe(lastSequenceToken);
        }
        /// <summary>
        /// Construct an instance of AWSLoggerCore
        /// </summary>
        /// <param name="config">Configuration options for logging messages to AWS</param>
        /// <param name="logType">Logging Provider Name to include in UserAgentHeader</param>
        public AWSLoggerCore(AWSLoggerConfig config, string logType)
        {
            _config = config;
            _logType = logType;

            var credentials = DetermineCredentials(config);

            if (!string.IsNullOrWhiteSpace(_config.ServiceUrl))
            {
                var cloudWatchConfig = new AmazonCloudWatchLogsConfig
                {
                    ServiceURL = _config.ServiceUrl
                };

                _client = new AmazonCloudWatchLogsClient(credentials, cloudWatchConfig);
            }
            else if (_config.Region != null)
            {
                _client = new AmazonCloudWatchLogsClient(credentials, Amazon.RegionEndpoint.GetBySystemName(_config.Region));
            }
            else
            {
                _client = new AmazonCloudWatchLogsClient(credentials);
            }

            ((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;
            ((AmazonCloudWatchLogsClient)this._client).ExceptionEvent += ServiceClienExceptionEvent;

            StartMonitor();
            RegisterShutdownHook();
        }
Esempio n. 4
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="logStreamPrefix">
        /// Prefix of the log stream that we will append to. We will default to writing to:
        /// <code>
        /// ${logStreamPrefix}/{HostName}/{UniqueInstanceGuid}
        /// </code>
        /// where you provide the log stream prefix, host name is looked up using <see cref="Dns.GetHostName" />
        /// and the Unique Instance Guid is generated at the time of log creation. This means you will have a
        /// separate log stream for each instance of your cloud watch logger. This is useful as it will
        /// generate a new log stream each time your program restarts. You can disable appending
        /// the guid by setting <paramref name="appendUniqueInstanceGuid"/> to false.
        /// </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="appendUniqueInstanceGuid">Should a unique guid be appended to the log stream that will be used for all writes from this log instance?</param>
        /// <param name="appendHostName">Should the machines HostName (as determined by <see cref="Dns.GetHostName" />) be appended to the log stream prefix?</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,
            string logStreamPrefix,
            LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose,
            int batchSizeLimit             = 100,
            int batchUploadPeriodInSeconds = 10,
            bool createLogGroup            = true,
            int queueSizeLimit             = 10000,
            byte maxRetryAttempts          = 5,
            LogGroupRetentionPolicy logGroupRetentionPolicy = LogGroupRetentionPolicy.OneWeek,
            bool appendUniqueInstanceGuid          = true,
            bool appendHostName                    = true,
            ITextFormatter textFormatter           = null,
            IAmazonCloudWatchLogs cloudWatchClient = null)
        {
            var provider = new ConfigurableLogStreamNameProvider(
                logStreamPrefix,
                appendHostName,
                appendUniqueInstanceGuid);

            return(AmazonCloudWatch(loggerConfiguration,
                                    logGroup,
                                    provider,
                                    restrictedToMinimumLevel,
                                    batchSizeLimit,
                                    batchUploadPeriodInSeconds,
                                    createLogGroup,
                                    queueSizeLimit,
                                    maxRetryAttempts,
                                    logGroupRetentionPolicy,
                                    textFormatter,
                                    cloudWatchClient));
        }
Esempio n. 5
0
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile("appSettings.json", optional: false, reloadOnChange: true)
                          .AddJsonFile("appSettings." + env.EnvironmentName + ".json", optional: true, reloadOnChange: true)
                          .AddEnvironmentVariables();

            Configuration = builder.Build();

            HostingEnvironment = env;

            // Serilog
            Serilog.Debugging.SelfLog.Enable(x => Debug.WriteLine(x));
            var awsOptions = Configuration.GetAWSOptions();

            // AWS clients
            _s3Client         = awsOptions.CreateServiceClient <IAmazonS3>();
            _lambdaClient     = awsOptions.CreateServiceClient <IAmazonLambda>();
            _cloudWatchClient = awsOptions.CreateServiceClient <IAmazonCloudWatchLogs>();

            _logger = new LoggerConfiguration()
                      .ReadFrom.Configuration(Configuration)
                      .WriteTo.AmazonCloudWatch(new CloudWatchSinkOptions {
                LogGroupName = "markdown-webapi", Period = TimeSpan.FromSeconds(5)
            }, _cloudWatchClient)
                      .CreateLogger();

            _logger.Information("Server started: HostingEnvironment: {@HostingEvironment}", env);
            _logger.Information("Server started: Environment: {@Environment}", Environment.GetEnvironmentVariables());
            _logger.Information("Server started: Configuration: {@Configuration}", Configuration.AsEnumerable());

            _settings           = new WebApiSettings(Configuration);
            _fileUploadSettings = new FileUploadSettings(Configuration);
        }
        //http://docs.aws.amazon.com/firehose/latest/dev/limits.html and http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html
        public CloudWatchLogsSink(IPlugInContext context,
                                  IAmazonCloudWatchLogs client
                                  ) : base(context, 5, 500, 1024 * 1024)
        {
            if (_count > 10000)
            {
                throw new ArgumentException("The maximum buffer size for CloudWatchLog is 10000");
            }

            _client = client;

            _logGroupName  = ResolveVariables(_config["LogGroup"]);
            _logStreamName = ResolveVariables(_config["LogStream"]);

            if (string.IsNullOrWhiteSpace(_logGroupName))
            {
                throw new ArgumentException("'LogGroup' setting in config file cannot be null, whitespace, or 'LogGroup'");
            }

            if (string.IsNullOrWhiteSpace(_logStreamName) || _logStreamName.Equals("LogStream"))
            {
                throw new ArgumentException("'LogStream' setting in config file cannot be null, whitespace or 'LogStream'");
            }

            _throttle = new AdaptiveThrottle(
                new TokenBucket(1, 5), //5 requests per second
                _backoffFactor,
                _recoveryFactor,
                _minRateAdjustmentFactor);
        }
Esempio n. 7
0
        public static void InitializeLoggerFromConfig(IAmazonCloudWatchLogs client,
                                                      LoggingConfiguration loggingConfiguration)
        {
            if (isInitialized)
            {
                return;
            }

            lock (LockObj)
            {
                var options      = GetCloudWatchSinkOptionsFromConfig(loggingConfiguration);
                var loggerConfig = new LoggerConfiguration();
                ConfigureFileLoggerSink(loggerConfig, loggingConfiguration);
                ConfigureConsoleLoggerSink(loggerConfig, loggingConfiguration);
                var logger = loggerConfig
                             .WriteTo.Logger(l1 => l1
                                             .MinimumLevel.ControlledBy(loggingConfiguration.LevelSwitch)
                                             .WriteTo.AmazonCloudWatch(options, client))
                             .CreateLogger();
                isInitialized = false;
                InitializeLogger(logger);
            }

            LogInfo(new Model.LogEvent()
            {
                Message = "Initialized Logger successfully."
            });
        }
Esempio n. 8
0
        /// <summary>
        /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="config">Configuration on how to connect to AWS and how the log messages should be sent.</param>
        /// <param name="minLevel">The minimum log level for messages to be written.</param>
        /// <returns></returns>
        public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, IAmazonCloudWatchLogs client, AWSLoggerConfig config, LogLevel minLevel)
        {
            var provider = new AWSLoggerProvider(client, config, minLevel);

            factory.AddProvider(provider);

            return(factory);
        }
 public CloudWatchLogsSink(
     IPlugInContext context,
     IAmazonCloudWatchLogs cloudWatchLogsClient
     ) : this(context)
 {
     // Setup Client
     CloudWatchLogsClient = cloudWatchLogsClient;
 }
Esempio n. 10
0
        public CloudWatchLogSink(IAmazonCloudWatchLogs cloudWatchClient, CloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period)
        {
            this.cloudWatchClient = cloudWatchClient;
            this.options          = options;
            renderer = options.LogEventRenderer ?? new RenderedMessageLogEventRenderer();

            UpdateLogStreamName();
        }
 public PlatformLogsController(PlatformResourcesContext context, UserManager <IdentityUser> userManager, IAmazonCloudWatch cloudwatchClient, IAmazonCloudWatchEvents cloudwatcheventsClient, IAmazonCloudWatchLogs cloudwatchLogsClient)
 {
     this._context               = context;
     this.CloudwatchClient       = cloudwatchClient;
     this.CloudwatchEventsClient = cloudwatcheventsClient;
     this.CloudwatchLogsClient   = cloudwatchLogsClient;
     this._userManager           = userManager;
 }
 /// <summary>
 /// Constructs CloudWatchLogsClientWrapper object.
 /// </summary>
 /// <param name="client">Instance of IAmazonCloudWatchLogs.</param>
 /// <param name="settings">The wrapper settings object.</param>
 public CloudWatchLogsClientWrapper(
     IAmazonCloudWatchLogs client,
     CloudWatchLogsWrapperSettings settings
     )
 {
     _client   = client;
     _settings = settings;
 }
Esempio n. 13
0
        /// <summary>
        /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="config">Configuration on how to connect to AWS and how the log messages should be sent.</param>
        /// <param name="filter">A filter function that has the logger category name and log level which can be used to filter messages being sent to AWS.</param>
        /// <returns></returns>
        public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, IAmazonCloudWatchLogs client, AWSLoggerConfig config, Func <string, LogLevel, bool> filter)
        {
            var provider = new AWSLoggerProvider(client, config, filter);

            factory.AddProvider(provider);

            return(factory);
        }
Esempio n. 14
0
 public ScopedUpdatingService(ILogger <ScopedUpdatingService> logger, PlatformResourcesContext context, IAmazonEC2 EC2Client, IAmazonCloudWatch cloudwatchClient, IAmazonCloudWatchEvents cloudwatcheventsClient, IAmazonCloudWatchLogs cloudwatchlogsClient)
 {
     _logger      = logger;
     this.context = context;
     ec2Client    = EC2Client;
     cwClient     = cloudwatchClient;
     cweClient    = cloudwatcheventsClient;
     cwlClient    = cloudwatchlogsClient;
 }
Esempio n. 15
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();
        }
Esempio n. 16
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));
        }
        public AWSLoggerCore(IAmazonCloudWatchLogs client, AWSLoggerConfig config, string logType)
        {
            _config  = config;
            _logType = logType;
            _client  = client;

            ((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;

            StartMonitor();
            RegisterShutdownHook();
        }
Esempio n. 18
0
        private static bool LogGroupExists(IAmazonCloudWatchLogs client, string logGroupName)
        {
            var describeLogGroupRequest = new DescribeLogGroupsRequest
            {
                LogGroupNamePrefix = logGroupName,
                Limit = 1
            };
            var describeLogGroupResponse =
                (client.DescribeLogGroupsAsync(describeLogGroupRequest)).GetAwaiter()
                .GetResult();

            return(describeLogGroupResponse.LogGroups.Any(x => x.LogGroupName == logGroupName));
        }
Esempio n. 19
0
        //--- Constructors ---
        public TwitterStream()
        {
            var            region = RegionEndpoint.GetBySystemName(AWS_REGION);
            var            chain  = new CredentialProfileStoreChain();
            AWSCredentials awsCredentials;

            if (!chain.TryGetAWSCredentials("lambdasharp", out awsCredentials))
            {
                throw new Exception("AWS Credentials not found!");
            }
            _client = new AmazonCloudWatchLogsClient(awsCredentials, region);
            SetTwitterCredentials();
            _stream = Stream.CreateFilteredStream();
        }
Esempio n. 20
0
        public AWSLoggerCore(AWSLoggerConfig config, string logType)
        {
            _config  = config;
            _logType = logType;
            var credentials = config.Credentials;

            _client = new AmazonCloudWatchLogsClient(credentials, _config.RegionEndpoint);
            ((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;
            StartMonitor();
            RegisterShutdownHook();

            // Disable adding messages when logger is not properly configured
            isInit = !(string.IsNullOrWhiteSpace(credentials?.GetCredentials()?.AccessKey) ||
                       string.IsNullOrWhiteSpace(credentials?.GetCredentials()?.SecretKey) ||
                       string.IsNullOrWhiteSpace(_config?.LogGroup));
        }
Esempio n. 21
0
        /// <summary>
        /// Adds the AWS logging provider to the log factory using the configuration specified in the AWSLoggerConfig
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="config">Configuration on how to connect to AWS and how the log messages should be sent.</param>
        /// <returns></returns>
        public static ILoggerFactory AddAWSProvider(this ILoggerFactory factory, IAmazonCloudWatchLogs client, AWSLoggerConfig config)
        {
            // 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(client, config);

            factory.AddProvider(provider);

            return(factory);
        }
Esempio n. 22
0
 public AsyncCloudWatchLogsSink(string id, string sessionName, string logGroup, string logStream,
                                IAmazonCloudWatchLogs cloudWatchLogsClient,
                                IAppDataFileProvider appDataFileProvider,
                                ILogger logger,
                                IMetrics metrics,
                                IBookmarkManager bookmarkManager,
                                NetworkStatus networkStatus,
                                AWSBufferedSinkOptions options)
     : base(id, sessionName, appDataFileProvider, logger, metrics, bookmarkManager, networkStatus, options)
 {
     Id                    = id;
     _logGroup             = logGroup;
     _logStream            = logStream;
     _cloudWatchLogsClient = cloudWatchLogsClient;
     // Set throttle at 5 requests per second
     _throttle = new AdaptiveThrottle(new TokenBucket(1, 5), _backoffFactor, _recoveryFactor, _minRateAdjustmentFactor);
 }
Esempio n. 23
0
        //--- Constructors ---
        public TwitterStream()
        {
            var            region = RegionEndpoint.GetBySystemName(AWS_REGION);
            var            chain  = new CredentialProfileStoreChain();
            AWSCredentials awsCredentials;

            if (!chain.TryGetAWSCredentials("lambdasharp", out awsCredentials))
            {
                throw new Exception("AWS Credentials not found!");
            }
            _client = new AmazonCloudWatchLogsClient(awsCredentials, region);
            SetTwitterCredentials();
            _stream = Stream.CreateFilteredStream();

            // Read application vars
            _applicationVars = JsonConvert.DeserializeObject <ApplicationVars>(File.ReadAllText("application_vars.json"));
        }
Esempio n. 24
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());
        }
Esempio n. 25
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, ICloudWatchSinkOptions options) : base(options.BatchSizeLimit, options.Period, options.QueueSizeLimit)
        {
            if (string.IsNullOrEmpty(options?.LogGroupName))
            {
                throw new ArgumentException($"{nameof(ICloudWatchSinkOptions)}.{nameof(options.LogGroupName)} must be specified.");
            }
            if (options.BatchSizeLimit < 1)
            {
                throw new ArgumentException($"{nameof(ICloudWatchSinkOptions)}.{nameof(options.BatchSizeLimit)} must be a value greater than 0.");
            }
            this.cloudWatchClient = cloudWatchClient;
            this.options          = options;

            if (options.TextFormatter == null)
            {
                throw new System.ArgumentException($"{nameof(options.TextFormatter)} is required");
            }

            textFormatter = options.TextFormatter;
        }
Esempio n. 26
0
        public AWSLoggerCore(AWSLoggerConfig config, string logType)
        {
            _config  = config;
            _logType = logType;
            var credentials = DetermineCredentials(config);

            if (_config.Region != null)
            {
                _client = new AmazonCloudWatchLogsClient(credentials, Amazon.RegionEndpoint.GetBySystemName(_config.Region));
            }
            else
            {
                _client = new AmazonCloudWatchLogsClient(credentials);
            }

            ((AmazonCloudWatchLogsClient)this._client).BeforeRequestEvent += ServiceClientBeforeRequestEvent;

            StartMonitor();
            RegisterShutdownHook();
        }
Esempio n. 27
0
        private static void CreateLogGroup(IAmazonCloudWatchLogs client, string logGroupName, int retentionDays)
        {
            var createLogGroupRequest = new CreateLogGroupRequest
            {
                LogGroupName = logGroupName
            };

            (client.CreateLogGroupAsync(createLogGroupRequest)).GetAwaiter()
            .GetResult();

            Thread.Sleep(1000);

            var putRetentionPolicyRequest = new PutRetentionPolicyRequest
            {
                LogGroupName    = logGroupName,
                RetentionInDays = retentionDays
            };

            (client.PutRetentionPolicyAsync(putRetentionPolicyRequest))
            .GetAwaiter().GetResult();
        }
Esempio n. 28
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,
                              IWebHostEnvironment env,
                              IAmazonCloudWatchLogs cloudWatchLogs,
                              AppSettings settings)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Debug()
                         .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
                         .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .WriteTo.File("logs.txt")
                         .WriteTo.AmazonCloudWatch(new CloudWatchSinkOptions {
                CreateLogGroup        = true,
                LogGroupName          = settings.Aws.CloudWatch.GroupName,
                TextFormatter         = new AmazonCloudWatchTextFormatter(),
                LogStreamNameProvider = new AmazonCloudWatchLogStreamNameProvider()
            }, cloudWatchLogs)
                         .CreateLogger();

            app.UseCors(builder => builder.WithOrigins(settings.ClientUrl)
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints => {
                endpoints.MapControllers();
            });
        }
Esempio n. 29
0
        public async static Task Initialize(string service)
        {
            _logGroup += string.Format("{0}{1}.{2}", LOG_GROUP_BASENAME, GetASPNETEnvironment(), service);
            Console.WriteLine("Ensuring log group {0} exists", _logGroup);

            _cwlClient = new AmazonCloudWatchLogsClient();

            var response = await _cwlClient.DescribeLogGroupsAsync(new DescribeLogGroupsRequest { LogGroupNamePrefix = LOG_GROUP_BASENAME });

            if (response.LogGroups.FirstOrDefault(x => string.Equals(x.LogGroupName, _logGroup)) == null)
            {
                await _cwlClient.CreateLogGroupAsync(new CreateLogGroupRequest
                {
                    LogGroupName = _logGroup
                });
            }

            _logStream = DateTime.Now.ToString("s").Replace(':', '-') + "." + new Random().Next(9999).ToString("0000");

            _cwlClient.CreateLogStreamAsync(new CreateLogStreamRequest
            {
                LogGroupName  = _logGroup,
                LogStreamName = _logStream
            }).Wait();

            if (_poller == null)
            {
                _poller = new Task(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(_flushInterval);
                        FlushBufferToCloudWatchLogs();
                    }
                });
                _poller.Start();
            }
        }
        /// <inheritdoc/>
        public AmazonCloudWatchLogsClient FailOverToSecondaryRegion(Throttle throttle)
        {
            var _cloudWatchLogsClient = _failoverSink.FailOverToSecondaryRegion(_throttle);

            if (_cloudWatchLogsClient is not null)
            {
                // Jittered Delay
                var delay = _throttle.GetDelayMilliseconds(1);
                if (delay > 0)
                {
                    Task.Delay((int)(delay * (1.0d + Utility.Random.NextDouble() * ConfigConstants.DEFAULT_JITTING_FACTOR))).Wait();
                }
                // Dispose
                CloudWatchLogsClient.Dispose();
                // Override client
                CloudWatchLogsClient = _cloudWatchLogsClient;

                // Reset CloudWatch Logs sequence token
                _sequenceToken = null;
                GetSequenceTokenAsync(ResolveTimestampInLogStreamName(DateTime.UtcNow), true).Wait();
            }
            return(null);
        }
Esempio n. 31
0
        public async static Task Initialize(string service)
        {
            _logGroup += string.Format("{0}{1}.{2}", LOG_GROUP_BASENAME, GetASPNETEnvironment(), service);
            Console.WriteLine("Ensuring log group {0} exists", _logGroup);

            _cwlClient = new AmazonCloudWatchLogsClient();

            var response = await _cwlClient.DescribeLogGroupsAsync(new DescribeLogGroupsRequest { LogGroupNamePrefix = LOG_GROUP_BASENAME });
            if (response.LogGroups.FirstOrDefault(x => string.Equals(x.LogGroupName, _logGroup)) == null)
            {
                await _cwlClient.CreateLogGroupAsync(new CreateLogGroupRequest
                {
                    LogGroupName = _logGroup
                });
            }

            _logStream = DateTime.Now.ToString("s").Replace(':', '-') + "." + new Random().Next(9999).ToString("0000");

            _cwlClient.CreateLogStreamAsync(new CreateLogStreamRequest
            {
                LogGroupName = _logGroup,
                LogStreamName = _logStream
            }).Wait();

            if (_poller == null)
            {
                _poller = new Task(() =>
                {
                    while (true)
                    {
                        Thread.Sleep(_flushInterval);
                        FlushBufferToCloudWatchLogs();
                    }
                });
                _poller.Start();
            }
        }