Amazon.CloudWatchLogs.AmazonCloudWatchLogsClient.PutLogEvents C# (CSharp) Method

PutLogEvents() public method

Uploads a batch of log events to the specified log stream.

You must include the sequence token obtained from the response of the previous call. An upload in a newly created log stream does not require a sequence token. You can also get the sequence token using DescribeLogStreams.

The batch of events must satisfy the following constraints:

  • The maximum batch size is 1,048,576 bytes, and this size is calculated as the sum of all event messages in UTF-8, plus 26 bytes for each log event.

  • None of the log events in the batch can be more than 2 hours in the future.

  • None of the log events in the batch can be older than 14 days or the retention period of the log group.

  • The log events in the batch must be in chronological ordered by their timestamp (the time the event occurred, expressed as the number of milliseconds since Jan 1, 1970 00:00:00 UTC).

  • The maximum number of log events in a batch is 10,000.

  • A batch of log events in a single request cannot span more than 24 hours. Otherwise, the operation fails.

/// The event was already logged. /// /// A parameter is specified incorrectly. /// /// The sequence token is not valid. /// /// The specified resource does not exist. /// /// The service cannot complete the request. ///
public PutLogEvents ( PutLogEventsRequest request ) : PutLogEventsResponse
request Amazon.CloudWatchLogs.Model.PutLogEventsRequest Container for the necessary parameters to execute the PutLogEvents service method.
return Amazon.CloudWatchLogs.Model.PutLogEventsResponse
        public PutLogEventsResponse PutLogEvents(PutLogEventsRequest request)
        {
            var marshaller = new PutLogEventsRequestMarshaller();
            var unmarshaller = PutLogEventsResponseUnmarshaller.Instance;

            return Invoke<PutLogEventsRequest,PutLogEventsResponse>(request, marshaller, unmarshaller);
        }

Usage Example

        void Writer(object sender, ElapsedEventArgs e)
        {
            try
            {
                var logEvents = new List<InputLogEvent>();

                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);
                    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)
            {
                TraceInfo.WriteLine(ee.Message);
                TraceDebug.WriteLine(ee.StackTrace);
            }
            finally
            {
                if(timer != null)
                    timer.Start();
            }
        }
AmazonCloudWatchLogsClient