Пример #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();
        }
Пример #2
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonCloudWatchLogsConfig config = new AmazonCloudWatchLogsConfig();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonCloudWatchLogsClient client = new AmazonCloudWatchLogsClient(creds, config);

            DescribeLogGroupsResponse resp = new DescribeLogGroupsResponse();

            do
            {
                DescribeLogGroupsRequest req = new DescribeLogGroupsRequest
                {
                    NextToken = resp.NextToken
                    ,
                    Limit = maxItems
                };

                resp = client.DescribeLogGroups(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.LogGroups)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.NextToken));
        }
        /// <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();
        }
Пример #4
0
        public IAmazonCloudWatchLogs CreateClient()
        {
            var config = new AmazonCloudWatchLogsConfig();

            config.UseHttp        = true;
            config.RegionEndpoint = RegionEndpoint.USEast1;
            config.ServiceURL     = "http://localhost:4586";

            return(new AmazonCloudWatchLogsClient(config));
        }
Пример #5
0
        public AWSSink(AWSCredentials credentials, RegionEndpoint regionEndPoint, string logGroupName, string logStreamName)
        {
            Config = new AmazonCloudWatchLogsConfig();
            Config.RegionEndpoint = regionEndPoint;
            _queue  = new AsyncProducerConsumerQueue <LogEvent>();
            _client = new AmazonCloudWatchLogsClient(credentials, Config);
            _cancellationTokenSource = new CancellationTokenSource();

            LogGroupName  = logGroupName;
            LogStreamName = logStreamName;

            Worker(_cancellationTokenSource.Token);
        }
Пример #6
0
        protected IAmazonCloudWatchLogs CreateClient(AWSCredentials credentials, RegionEndpoint region)
        {
            var config = new AmazonCloudWatchLogsConfig {
                RegionEndpoint = region
            };

            Amazon.PowerShell.Utils.Common.PopulateConfig(this, config);
            this.CustomizeClientConfig(config);
            var client = new AmazonCloudWatchLogsClient(credentials, config);

            client.BeforeRequestEvent += RequestEventHandler;
            client.AfterResponseEvent += ResponseEventHandler;
            return(client);
        }
Пример #7
0
        private void SetupClient(ClientConfig clientConfig)
        {
            if (Client != null)
            {
                return;
            }

            if (clientConfig == null)
            {
                if (typeof(T) == typeof(AmazonCloudWatchLogsClient))
                {
                    clientConfig = new AmazonCloudWatchLogsConfig();
                }
                else
                {
                    clientConfig = new AmazonCloudWatchConfig();
                }
            }


            if (string.IsNullOrEmpty(_endPoint) && clientConfig.RegionEndpoint == null && ConfigurationManager.AppSettings["AWSServiceEndpoint"] != null)
            {
                _endPoint = ConfigurationManager.AppSettings["AWSServiceEndpoint"];
            }

            if (string.IsNullOrEmpty(_accessKey) && ConfigurationManager.AppSettings["AWSAccessKey"] != null)
            {
                _accessKey = ConfigurationManager.AppSettings["AWSAccessKey"];
            }

            if (string.IsNullOrEmpty(_secret) && ConfigurationManager.AppSettings["AWSSecretKey"] != null)
            {
                _secret = ConfigurationManager.AppSettings["AWSSecretKey"];
            }

            if (!string.IsNullOrEmpty(_endPoint))
            {
                if (_endPoint.StartsWith("http"))
                {
                    clientConfig.ServiceURL = _endPoint;
                }
                else
                {
                    clientConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(_endPoint);
                }
            }

            if (string.IsNullOrEmpty(_accessKey))
            {
                try
                {
                    if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AWSProfileName"]) || ProfileManager.ListProfileNames().Contains("default"))
                    {
                        if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["AWSRegion"]))
                        {
                            Client = AWSClientFactoryWrapper <T> .CreateServiceClient();
                        }
                        else if (clientConfig.RegionEndpoint != null)
                        {
                            Client = AWSClientFactoryWrapper <T> .CreateServiceClient(clientConfig);
                        }
                    }
                    else
                    {
                        foreach (var availableRole in InstanceProfileAWSCredentials.GetAvailableRoles())
                        {
                            LogLog.Debug(typeof(CloudWatchClientWrapperBase <>), "Role: " + availableRole);
                        }
                        Client = AWSClientFactoryWrapper <T> .CreateServiceClient(clientConfig);
                    }
                }
                catch (AmazonServiceException e)
                {
                    LogLog.Debug(typeof(CloudWatchClientWrapperBase <>), "Exception caught while creating client", e);
                }
            }
        /// <summary>
        /// Opens the component.
        /// </summary>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        public async Task OpenAsync(string correlationId)
        {
            if (IsOpen())
            {
                return;
            }

            var awsConnection = await _connectionResolver.ResolveAsync(correlationId);

            // Assign service name
            awsConnection.Service      = "logs";
            awsConnection.ResourceType = "log-group";

            if (!string.IsNullOrEmpty(awsConnection.Resource))
            {
                _group = awsConnection.Resource;
            }

            // Undefined stream creates a random stream on every connect
            if (string.IsNullOrEmpty(_stream))
            {
                _stream = IdGenerator.NextLong();
            }

            // Validate connection params
            var err = awsConnection.Validate(correlationId);

            if (err != null)
            {
                throw err;
            }

            // Create client
            var region = RegionEndpoint.GetBySystemName(awsConnection.Region);
            var config = new AmazonCloudWatchLogsConfig()
            {
                RegionEndpoint = region
            };

            _client = new AmazonCloudWatchLogsClient(awsConnection.AccessId, awsConnection.AccessKey, config);

            // Create a log group if needed
            try
            {
                await _client.CreateLogGroupAsync(
                    new CreateLogGroupRequest
                {
                    LogGroupName = _group
                }
                    );
            }
            catch (ResourceAlreadyExistsException)
            {
                // Ignore. Everything is ok
            }

            // Create or read log stream
            try
            {
                await _client.CreateLogStreamAsync(
                    new CreateLogStreamRequest
                {
                    LogGroupName  = _group,
                    LogStreamName = _stream
                }
                    );

                _lastToken = null;
            }
            catch (ResourceAlreadyExistsException)
            {
                var response = await _client.DescribeLogStreamsAsync(
                    new DescribeLogStreamsRequest
                {
                    LogGroupName        = _group,
                    LogStreamNamePrefix = _stream
                }
                    );

                if (response.LogStreams.Count > 0)
                {
                    _lastToken = response.LogStreams[0].UploadSequenceToken;
                }
            }

            if (_timer == null)
            {
                _timer = new FixedRateTimer(OnTimer, _interval, _interval);
                _timer.Start();
            }
        }