Esempio n. 1
0
        private List <OutputLogEvent> FunctionLogs(string functionName, DateTime start, DateTime end)
        {
            if (_logs == null)
            {
                using (var cwClient = new AmazonCloudWatchLogsClient())
                {
                    var logStreams = new List <LogStream>();
                    DescribeLogStreamsResponse lResponse;
                    string nextToken = null;
                    do
                    {
                        lResponse =
                            cwClient.DescribeLogStreams(new DescribeLogStreamsRequest("/aws/lambda/" + functionName)
                        {
                            NextToken = nextToken
                        });
                        logStreams.AddRange(lResponse.LogStreams);
                    } while (!string.IsNullOrEmpty(nextToken = lResponse.NextToken));
                    List <OutputLogEvent> logs = new List <OutputLogEvent>();
                    logStreams.ForEach(
                        s =>
                        cwClient.GetLogEvents(new GetLogEventsRequest("/aws/lambda/" + functionName,
                                                                      s.LogStreamName)
                    {
                        Limit = 10000
                    }).Events.ForEach(e => logs.Add(e)));
                    _logs = logs;
                }
            }

            return(_logs);
        }
Esempio n. 2
0
        /// <summary>
        /// 附加到现有日志流并开始记录
        /// </summary>
        /// <param name="client">AWS 客户端</param>
        /// <param name="groupName">日志组名称</param>
        /// <param name="streamName">日志流名称</param>
        /// <returns></returns>
        public static CloudWatchLogger AppendStream(AmazonCloudWatchLogsClient client, string groupName, string streamName)
        {
            var response = client.DescribeLogStreams(new DescribeLogStreamsRequest {
                OrderBy = OrderBy.LogStreamName, LogGroupName = groupName, LogStreamNamePrefix = streamName, Limit = 1
            });
            var streamInfo = response.LogStreams.FirstOrDefault();

            if (streamInfo == null || streamInfo.LogStreamName != streamName)
            {
                return(CreateStream(client, groupName, streamName));
            }

            return(new CloudWatchLogger(client, groupName, streamName, streamInfo.UploadSequenceToken));
        }
Esempio n. 3
0
        private static List <LogStream> GetAllLogStreams(string functionName, AmazonCloudWatchLogsClient cwClient)
        {
            var logStreams = new List <LogStream>();
            DescribeLogStreamsResponse lResponse;
            string nextToken = null;

            do
            {
                lResponse =
                    cwClient.DescribeLogStreams(new DescribeLogStreamsRequest("/aws/lambda/" + functionName)
                {
                    NextToken = nextToken
                });
                logStreams.AddRange(lResponse.LogStreams);
            } while (!string.IsNullOrEmpty(nextToken = lResponse.NextToken));
            return(logStreams);
        }
Esempio n. 4
0
 /// <summary>
 /// Get all the LogStream from the chosen LogGroup
 /// </summary>
 /// <param name="awsLogGroupName"></param>
 /// <returns>return all LogStreams from the chosen LogGroup</returns>
 public DescribeLogStreamsResponse GetLogStreams(string awsLogGroupName)
 {
     awsLogStream = new DescribeLogStreamsRequest();
     awsLogStream.LogGroupName = awsLogGroupName;
     return(awsCloudwatchClient.DescribeLogStreams(awsLogStream));
 }
        void Writer(object sender, ElapsedEventArgs e)
        {
            var logEvents = new List <InputLogEvent>();

            try
            {
                var more = true;
                while (more)
                {
                    InputLogEvent item;
                    more = items.TryTake(out item);
                    if (more)
                    {
                        logEvents.Add(item);
                    }
                }

                if (logEvents.Count == 0)
                {
                    return;
                }

                if (!Settings.Default.SendUsageData)
                {
                    return;
                }

                using (var logs = new AmazonCloudWatchLogsClient(AwsKeys.AccessKey, AwsKeys.SecretKey, RegionEndpoint.APSoutheast2))
                {
                    var request = new PutLogEventsRequest(AwsKeys.GroupName, LogStreamName, logEvents);

                    var describeLogStreamsRequest = new DescribeLogStreamsRequest(AwsKeys.GroupName)
                    {
                        LogStreamNamePrefix = trackingId,
                        Descending          = true
                    };
                    var describeLogStreamsResponse = logs.DescribeLogStreams(describeLogStreamsRequest);
                    var logStreams = describeLogStreamsResponse.LogStreams;
                    var logStream  = logStreams.FirstOrDefault(ls => ls.LogStreamName == LogStreamName);
                    if (logStream != null)
                    {
                        var token = logStream.UploadSequenceToken;
                        request.SequenceToken = token;
                        checkResponse(logs.PutLogEvents(request));
                    }
                    else
                    {
                        var createRequest = new CreateLogStreamRequest(AwsKeys.GroupName, LogStreamName);
                        checkResponse(logs.CreateLogStream(createRequest));
                        checkResponse(logs.PutLogEvents(request));
                    }
                }
            }
            catch (Exception ee)
            {
                AttempToRestoreErrors(logEvents, ee);
            }
            finally
            {
                if (timer != null)
                {
                    timer.Start();
                }
            }
        }
Esempio n. 6
0
        private void Connect()
        {
            //If any of the params were not provided to the constructor, try to read them from the config file.
            if (string.IsNullOrEmpty(AccessKey) && Attributes.ContainsKey(CONFIG_ACCESSKEY))
            {
                AccessKey = Attributes[CONFIG_ACCESSKEY];
            }
            ;
            if (string.IsNullOrEmpty(SecretKey) && Attributes.ContainsKey(CONFIG_SECRETKEY))
            {
                SecretKey = Attributes[CONFIG_SECRETKEY];
            }
            ;
            if (string.IsNullOrEmpty(Region) && Attributes.ContainsKey(CONFIG_REGION))
            {
                Region = Attributes[CONFIG_REGION];
            }
            ;
            if (string.IsNullOrEmpty(GroupName) && Attributes.ContainsKey(CONFIG_GROUPNAME))
            {
                GroupName = Attributes[CONFIG_GROUPNAME];
            }
            ;
            if (string.IsNullOrEmpty(StreamName) && Attributes.ContainsKey(CONFIG_STREAMNAME))
            {
                StreamName = Attributes[CONFIG_STREAMNAME];
            }
            ;

            try{
                if (string.IsNullOrEmpty(Region) && string.IsNullOrEmpty(AccessKey))
                {
                    //No config provided, use the default configuration
                    _Client = new AmazonCloudWatchLogsClient();
                }
                else if (string.IsNullOrEmpty(Region))
                {
                    //Use explicit access/secret key but default region
                    _Client = new AmazonCloudWatchLogsClient(AccessKey, SecretKey);
                }
                else if (string.IsNullOrEmpty(AccessKey))
                {
                    //Use explicit region and default access/secret key
                    _Client = new AmazonCloudWatchLogsClient(Amazon.RegionEndpoint.GetBySystemName(Region));
                }
                else
                {
                    //All params were supplied explicitly
                    _Client = new AmazonCloudWatchLogsClient(AccessKey, SecretKey, Amazon.RegionEndpoint.GetBySystemName(Region));
                }

                //Let's try to connect to the AWS Cloud with Group and Steam provided
                var response = _Client.DescribeLogStreams(new DescribeLogStreamsRequest()
                {
                    LogGroupName        = GroupName,
                    LogStreamNamePrefix = StreamName
                });

                //Get the next token needed to publish
                _Token = response.LogStreams[0].UploadSequenceToken;
            }
            catch
            {
                _ConnectionFailed = true;
                if (FailOnError)
                {
                    throw;
                }
            }

            if (!_ConnectionFailed)
            {
                _Connected = true;
            }
        }