Skip to content

Commit f2c71ae

Browse files
committed
Adding project files
1 parent b53da47 commit f2c71ae

File tree

13 files changed

+1363
-12
lines changed

13 files changed

+1363
-12
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AWSSDK.CloudFormation" Version="3.7.407.7" />
12+
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="3.7.403.14" />
13+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
// snippet-start:[CloudWatchLogs.dotnetv3.CloudWatchLogsWrapper]
5+
using Amazon.CloudWatchLogs;
6+
using Amazon.CloudWatchLogs.Model;
7+
using Microsoft.Extensions.Logging;
8+
9+
namespace CloudWatchLogsActions;
10+
11+
/// <summary>
12+
/// Wrapper class for Amazon CloudWatch Logs operations.
13+
/// </summary>
14+
public class CloudWatchLogsWrapper
15+
{
16+
private readonly IAmazonCloudWatchLogs _amazonCloudWatchLogs;
17+
private readonly ILogger<CloudWatchLogsWrapper> _logger;
18+
19+
/// <summary>
20+
/// Constructor for the CloudWatchLogsWrapper class.
21+
/// </summary>
22+
/// <param name="amazonCloudWatchLogs">The injected CloudWatch Logs client.</param>
23+
/// <param name="logger">The injected logger.</param>
24+
public CloudWatchLogsWrapper(IAmazonCloudWatchLogs amazonCloudWatchLogs, ILogger<CloudWatchLogsWrapper> logger)
25+
{
26+
_amazonCloudWatchLogs = amazonCloudWatchLogs;
27+
_logger = logger;
28+
}
29+
30+
// snippet-start:[CloudWatchLogs.dotnetv3.StartQuery]
31+
/// <summary>
32+
/// Starts a CloudWatch Logs Insights query.
33+
/// </summary>
34+
/// <param name="logGroupName">The name of the log group to query.</param>
35+
/// <param name="queryString">The CloudWatch Logs Insights query string.</param>
36+
/// <param name="startTime">The start time for the query (seconds since epoch).</param>
37+
/// <param name="endTime">The end time for the query (seconds since epoch).</param>
38+
/// <param name="limit">The maximum number of results to return.</param>
39+
/// <returns>The query ID if successful, null otherwise.</returns>
40+
public async Task<string?> StartQueryAsync(
41+
string logGroupName,
42+
string queryString,
43+
long startTime,
44+
long endTime,
45+
int limit = 10000)
46+
{
47+
try
48+
{
49+
var request = new StartQueryRequest
50+
{
51+
LogGroupName = logGroupName,
52+
QueryString = queryString,
53+
StartTime = startTime,
54+
EndTime = endTime,
55+
Limit = limit
56+
};
57+
58+
var response = await _amazonCloudWatchLogs.StartQueryAsync(request);
59+
return response.QueryId;
60+
}
61+
catch (InvalidParameterException ex)
62+
{
63+
_logger.LogError($"Invalid parameter for query: {ex.Message}");
64+
return null;
65+
}
66+
catch (ResourceNotFoundException ex)
67+
{
68+
_logger.LogError($"Log group not found: {ex.Message}");
69+
return null;
70+
}
71+
catch (Exception ex)
72+
{
73+
_logger.LogError($"An error occurred while starting query: {ex.Message}");
74+
return null;
75+
}
76+
}
77+
// snippet-end:[CloudWatchLogs.dotnetv3.StartQuery]
78+
79+
// snippet-start:[CloudWatchLogs.dotnetv3.GetQueryResults]
80+
/// <summary>
81+
/// Gets the results of a CloudWatch Logs Insights query.
82+
/// </summary>
83+
/// <param name="queryId">The ID of the query.</param>
84+
/// <returns>The query results response.</returns>
85+
public async Task<GetQueryResultsResponse?> GetQueryResultsAsync(string queryId)
86+
{
87+
try
88+
{
89+
var request = new GetQueryResultsRequest
90+
{
91+
QueryId = queryId
92+
};
93+
94+
var response = await _amazonCloudWatchLogs.GetQueryResultsAsync(request);
95+
return response;
96+
}
97+
catch (ResourceNotFoundException ex)
98+
{
99+
_logger.LogError($"Query not found: {ex.Message}");
100+
return null;
101+
}
102+
catch (Exception ex)
103+
{
104+
_logger.LogError($"An error occurred while getting query results: {ex.Message}");
105+
return null;
106+
}
107+
}
108+
// snippet-end:[CloudWatchLogs.dotnetv3.GetQueryResults]
109+
110+
// snippet-start:[CloudWatchLogs.dotnetv3.PutLogEvents]
111+
/// <summary>
112+
/// Puts log events to a CloudWatch Logs log stream.
113+
/// </summary>
114+
/// <param name="logGroupName">The name of the log group.</param>
115+
/// <param name="logStreamName">The name of the log stream.</param>
116+
/// <param name="logEvents">The list of log events to put.</param>
117+
/// <returns>True if successful, false otherwise.</returns>
118+
public async Task<bool> PutLogEventsAsync(
119+
string logGroupName,
120+
string logStreamName,
121+
List<InputLogEvent> logEvents)
122+
{
123+
try
124+
{
125+
var request = new PutLogEventsRequest
126+
{
127+
LogGroupName = logGroupName,
128+
LogStreamName = logStreamName,
129+
LogEvents = logEvents
130+
};
131+
132+
await _amazonCloudWatchLogs.PutLogEventsAsync(request);
133+
return true;
134+
}
135+
catch (ResourceNotFoundException ex)
136+
{
137+
_logger.LogError($"Log group or stream not found: {ex.Message}");
138+
return false;
139+
}
140+
catch (Exception ex)
141+
{
142+
_logger.LogError($"An error occurred while putting log events: {ex.Message}");
143+
return false;
144+
}
145+
}
146+
// snippet-end:[CloudWatchLogs.dotnetv3.PutLogEvents]
147+
}
148+
// snippet-end:[CloudWatchLogs.dotnetv3.CloudWatchLogsWrapper]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchLogsActions", "Actions\CloudWatchLogsActions.csproj", "{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchLogsScenario", "Scenarios\CloudWatchLogsScenario.csproj", "{B2C3D4E5-F6A7-5B6C-9D0E-1F2A3B4C5D6E}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudWatchLogsTests", "Tests\CloudWatchLogsTests.csproj", "{C3D4E5F6-A7B8-6C7D-0E1F-2A3B4C5D6E7F}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{A1B2C3D4-E5F6-4A5B-8C9D-0E1F2A3B4C5D}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{B2C3D4E5-F6A7-5B6C-9D0E-1F2A3B4C5D6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{B2C3D4E5-F6A7-5B6C-9D0E-1F2A3B4C5D6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{B2C3D4E5-F6A7-5B6C-9D0E-1F2A3B4C5D6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{B2C3D4E5-F6A7-5B6C-9D0E-1F2A3B4C5D6E}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{C3D4E5F6-A7B8-6C7D-0E1F-2A3B4C5D6E7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{C3D4E5F6-A7B8-6C7D-0E1F-2A3B4C5D6E7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C3D4E5F6-A7B8-6C7D-0E1F-2A3B4C5D6E7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{C3D4E5F6-A7B8-6C7D-0E1F-2A3B4C5D6E7F}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# CloudWatch Logs Large Query Example
2+
3+
This folder contains a .NET feature scenario that demonstrates how to perform large-scale queries on Amazon CloudWatch Logs using recursive binary search to retrieve more than the 10,000 result limit.
4+
5+
## Project Structure
6+
7+
```
8+
LargeQuery/
9+
├── Actions/
10+
│ ├── CloudWatchLogsWrapper.cs # Wrapper class for CloudWatch Logs operations
11+
│ └── CloudWatchLogsActions.csproj # Actions project file
12+
├── Scenarios/
13+
│ ├── LargeQueryWorkflow.cs # Main workflow implementation
14+
│ ├── README.md # Detailed scenario documentation
15+
│ └── CloudWatchLogsScenario.csproj # Scenario project file
16+
├── Tests/
17+
│ ├── LargeQueryWorkflowTests.cs # Unit tests
18+
│ ├── Usings.cs # Global usings
19+
│ └── CloudWatchLogsTests.csproj # Test project file
20+
└── CloudWatchLogsLargeQuery.sln # Solution file
21+
```
22+
23+
## What This Example Demonstrates
24+
25+
- Deploying AWS resources using CloudFormation
26+
- Generating and ingesting large volumes of log data
27+
- Performing CloudWatch Logs Insights queries
28+
- Using recursive binary search to retrieve more than 10,000 results
29+
- Cleaning up resources after completion
30+
31+
## Running the Example
32+
33+
1. Navigate to the solution directory:
34+
```
35+
cd dotnetv4/CloudWatchLogs/LargeQuery
36+
```
37+
38+
2. Build the solution:
39+
```
40+
dotnet build
41+
```
42+
43+
3. Run the scenario:
44+
```
45+
dotnet run --project Scenarios/CloudWatchLogsScenario.csproj
46+
```
47+
48+
4. Run the tests:
49+
```
50+
dotnet test
51+
```
52+
53+
## Prerequisites
54+
55+
- .NET 8.0 or later
56+
- AWS credentials configured
57+
- Python 3.x (for log generation)
58+
- Permissions for CloudWatch Logs and CloudFormation
59+
60+
## Related Resources
61+
62+
- [CloudWatch Logs Documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/)
63+
- [CloudWatch Logs Insights Query Syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/CWL_QuerySyntax.html)
64+
- [AWS SDK for .NET](https://aws.amazon.com/sdk-for-net/)
65+
66+
---
67+
68+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
69+
SPDX-License-Identifier: Apache-2.0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="AWSSDK.CloudFormation" Version="3.7.407.7" />
12+
<PackageReference Include="AWSSDK.CloudWatchLogs" Version="3.7.403.14" />
13+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
15+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\Actions\CloudWatchLogsActions.csproj" />
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)