There are multiple options to log data to AWS CloudWatch using .NET. The following is using the AWS SDK Amazon.CloudWatchLogs to directly add log data. The code will:
- Check to ensure the Log Group exists (optional)
- Create the Log Group if it doesn't exist (optional)
- Check if the Log Stream exists
- Create the Log Stream if it doesn't exist
- Log the message
AmazonCloudWatchLogsClient client = null;
//using access and secret keys
var credentials = new BasicAWSCredentials("MY_ACCESS_KEY", "MY_SECRET_KEY");
client = new AmazonCloudWatchLogsClient(credentials, Amazon.RegionEndpoint.USEast1);
//using local profile
var credentials = new StoredProfileAWSCredentials("PROFILE_NAME");
client = new AmazonCloudWatchLogsClient(credentials, Amazon.RegionEndpoint.USEast1);
//using execution context (ex: Lambda permissions);
client = new AmazonCloudWatchLogsClient();
//var the message to log
var message = "Something I want to log";
//log_group_name
var log_group_name = "/my-project/my-api/errors";
//log stream name - multiple logs can be added to this stream
var log_stream_name = DateTime.UtcNow.ToString("yyyy_MM_dd_HH_mm");
//the put request - used at the end but constructing it now
var put_log_events_request = new Amazon.CloudWatchLogs.Model.PutLogEventsRequest()
{
LogEvents = new List<Amazon.CloudWatchLogs.Model.InputLogEvent>()
{
new Amazon.CloudWatchLogs.Model.InputLogEvent()
{
Message = message,
Timestamp = DateTime.UtcNow
}
},
LogGroupName = log_group_name,
LogStreamName = log_stream_name
};
//check for log group
var describe_log_groups_request = new Amazon.CloudWatchLogs.Model.DescribeLogGroupsRequest()
{
LogGroupNamePrefix = log_group_name,
Limit = 1
};
var describe_log_groups_response = await client.DescribeLogGroupsAsync(describe_log_groups_request);
//create this log group?
if (describe_log_groups_response.LogGroups.Count == 0)
{
var create_log_group_request = new Amazon.CloudWatchLogs.Model.CreateLogGroupRequest()
{
LogGroupName = log_group_name
};
var create_log_group_response = await client.CreateLogGroupAsync(create_log_group_request);
}
//get previous stream
var describe_log_streams_request = new Amazon.CloudWatchLogs.Model.DescribeLogStreamsRequest()
{
LogGroupName = log_group_name,
Descending = true,
Limit = 1,
LogStreamNamePrefix = log_stream_name,
//nextToken: 'STRING_VALUE',
//orderBy: "LastEventTime"
};
var describe_log_streams_response = await client.DescribeLogStreamsAsync(describe_log_streams_request);
if (describe_log_streams_response.LogStreams.Count == 0)
{
//create new stream
var create_log_stream_request = new Amazon.CloudWatchLogs.Model.CreateLogStreamRequest()
{
LogGroupName = log_group_name,
LogStreamName = log_stream_name
};
var create_log_stream_response = await client.CreateLogStreamAsync(create_log_stream_request);
}
else
{
//use current log stream
put_log_events_request.SequenceToken = describe_log_streams_response.LogStreams[0].UploadSequenceToken;
}
//and finally,after all that, log the message
var put_log_events_response = await client.PutLogEventsAsync(put_log_events_request);