forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudWatchLogsWrapper.cs
More file actions
148 lines (138 loc) · 5.08 KB
/
CloudWatchLogsWrapper.cs
File metadata and controls
148 lines (138 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[CloudWatchLogs.dotnetv4.CloudWatchLogsWrapper]
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using Microsoft.Extensions.Logging;
namespace CloudWatchLogsActions;
/// <summary>
/// Wrapper class for Amazon CloudWatch Logs operations.
/// </summary>
public class CloudWatchLogsWrapper
{
private readonly IAmazonCloudWatchLogs _amazonCloudWatchLogs;
private readonly ILogger<CloudWatchLogsWrapper> _logger;
/// <summary>
/// Constructor for the CloudWatchLogsWrapper class.
/// </summary>
/// <param name="amazonCloudWatchLogs">The injected CloudWatch Logs client.</param>
/// <param name="logger">The injected logger.</param>
public CloudWatchLogsWrapper(IAmazonCloudWatchLogs amazonCloudWatchLogs, ILogger<CloudWatchLogsWrapper> logger)
{
_amazonCloudWatchLogs = amazonCloudWatchLogs;
_logger = logger;
}
// snippet-start:[CloudWatchLogs.dotnetv4.StartQuery]
/// <summary>
/// Starts a CloudWatch Logs Insights query.
/// </summary>
/// <param name="logGroupName">The name of the log group to query.</param>
/// <param name="queryString">The CloudWatch Logs Insights query string.</param>
/// <param name="startTime">The start time for the query (seconds since epoch).</param>
/// <param name="endTime">The end time for the query (seconds since epoch).</param>
/// <param name="limit">The maximum number of results to return.</param>
/// <returns>The query ID if successful, null otherwise.</returns>
public async Task<string?> StartQueryAsync(
string logGroupName,
string queryString,
long startTime,
long endTime,
int limit = 10000)
{
try
{
var request = new StartQueryRequest
{
LogGroupName = logGroupName,
QueryString = queryString,
StartTime = startTime,
EndTime = endTime,
Limit = limit
};
var response = await _amazonCloudWatchLogs.StartQueryAsync(request);
return response.QueryId;
}
catch (InvalidParameterException ex)
{
_logger.LogError($"Invalid parameter for query: {ex.Message}");
return null;
}
catch (ResourceNotFoundException ex)
{
_logger.LogError($"Log group not found: {ex.Message}");
return null;
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while starting query: {ex.Message}");
return null;
}
}
// snippet-end:[CloudWatchLogs.dotnetv4.StartQuery]
// snippet-start:[CloudWatchLogs.dotnetv4.GetQueryResults]
/// <summary>
/// Gets the results of a CloudWatch Logs Insights query.
/// </summary>
/// <param name="queryId">The ID of the query.</param>
/// <returns>The query results response.</returns>
public async Task<GetQueryResultsResponse?> GetQueryResultsAsync(string queryId)
{
try
{
var request = new GetQueryResultsRequest
{
QueryId = queryId
};
var response = await _amazonCloudWatchLogs.GetQueryResultsAsync(request);
return response;
}
catch (ResourceNotFoundException ex)
{
_logger.LogError($"Query not found: {ex.Message}");
return null;
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while getting query results: {ex.Message}");
return null;
}
}
// snippet-end:[CloudWatchLogs.dotnetv4.GetQueryResults]
// snippet-start:[CloudWatchLogs.dotnetv4.PutLogEvents]
/// <summary>
/// Puts log events to a CloudWatch Logs log stream.
/// </summary>
/// <param name="logGroupName">The name of the log group.</param>
/// <param name="logStreamName">The name of the log stream.</param>
/// <param name="logEvents">The list of log events to put.</param>
/// <returns>True if successful, false otherwise.</returns>
public async Task<bool> PutLogEventsAsync(
string logGroupName,
string logStreamName,
List<InputLogEvent> logEvents)
{
try
{
var request = new PutLogEventsRequest
{
LogGroupName = logGroupName,
LogStreamName = logStreamName,
LogEvents = logEvents
};
await _amazonCloudWatchLogs.PutLogEventsAsync(request);
return true;
}
catch (ResourceNotFoundException ex)
{
_logger.LogError($"Log group or stream not found: {ex.Message}");
return false;
}
catch (Exception ex)
{
_logger.LogError($"An error occurred while putting log events: {ex.Message}");
return false;
}
}
// snippet-end:[CloudWatchLogs.dotnetv4.PutLogEvents]
}
// snippet-end:[CloudWatchLogs.dotnetv4.CloudWatchLogsWrapper]