Add get of log group

This commit is contained in:
Gardient
2016-09-16 22:58:49 +03:00
parent ce84aa0089
commit f343dc6b48
5 changed files with 135 additions and 3 deletions

View File

@@ -11,6 +11,8 @@
<AssemblyName>CloudWatchLogDownloader</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -32,6 +34,18 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AWSSDK.CloudWatchLogs, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.CloudWatchLogs.3.1.3.1\lib\net45\AWSSDK.CloudWatchLogs.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="AWSSDK.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
<HintPath>..\packages\AWSSDK.Core.3.1.11.0\lib\net45\AWSSDK.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -42,13 +56,28 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\CommandOptions.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\AWSSDK.CloudWatchLogs.3.1.3.1\analyzers\dotnet\cs\AWSSDK.CloudWatchLogs.CodeAnalysis.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="FodyWeavers.xml" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Fody.1.28.3\build\Fody.targets" Condition="Exists('..\packages\Fody.1.28.3\build\Fody.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Fody.1.28.3\build\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.1.28.3\build\Fody.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers>
<Costura/>
</Weavers>

View File

@@ -0,0 +1,29 @@
using CommandLine;
using CommandLine.Text;
namespace CloudWatchLogDownloader.Models
{
internal class CommandOptions
{
[Option('g', "logGroup", HelpText = "The log group you want to select the stream from, can end with '*' in which case the option to choose from all groups starting with this will be given")]
[ValueOption(0)]
public string LogGroup { get; set; }
[Option('s', "logStream", HelpText = "The log stream you want to save, can end with '*' in which case the option to choose from all groups starting with this will be given")]
[ValueOption(1)]
public string LogStream { get; set; }
[Option('o', "outputFile", HelpText = "The file to output the logs to")]
[ValueOption(2)]
public string OutputFilePath { get; set; }
[ParserState]
public IParserState ParserState { get; set; }
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (current) => HelpText.DefaultParsingErrorsHandler(this, current));
}
}
}

View File

@@ -1,15 +1,76 @@
using System;
using Amazon.CloudWatchLogs;
using Amazon.CloudWatchLogs.Model;
using CloudWatchLogDownloader.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudWatchLogDownloader
{
class Program
{
private static AmazonCloudWatchLogsClient client;
static void Main(string[] args)
{
var opt = new CommandOptions();
if (CommandLine.Parser.Default.ParseArguments(args, opt))
{
client = new AmazonCloudWatchLogsClient();
var logGroup = GetLogGroup(opt.LogGroup);
var logStream = GetLogStream(logGroup, opt.LogStream);
WriteLogToFile(logGroup, logStream, opt.OutputFilePath);
}
}
private static LogGroup GetLogGroup(string logGroup = null)
{
List<LogGroup> allGroups = new List<LogGroup>();
DescribeLogGroupsResponse lgResponse = null;
do
{
lgResponse = client.DescribeLogGroups(new DescribeLogGroupsRequest { NextToken = (lgResponse != null ? lgResponse.NextToken : null) });
allGroups.AddRange(lgResponse.LogGroups);
} while (!string.IsNullOrWhiteSpace(lgResponse.NextToken));
if (string.IsNullOrWhiteSpace(logGroup) || logGroup[logGroup.Length - 1] == '*')
{
if (!string.IsNullOrWhiteSpace(logGroup))
{
logGroup = logGroup.Substring(0, logGroup.Length - 1);
allGroups = allGroups.Where(x => x.LogGroupName.StartsWith(logGroup)).ToList();
}
for (int i = 0, len = allGroups.Count; i < len; ++i)
Console.WriteLine(i + ") " + allGroups[i].LogGroupName);
int num = ReadIntBetween("Choose log group: ", 0, allGroups.Count - 1);
return allGroups[num];
}
var lg = allGroups.FirstOrDefault(x => x.LogGroupName == logGroup);
if (lg == null)
throw new Exception("The log group '" + logGroup + "' does not exist.");
return lg;
}
private static LogStream GetLogStream(LogGroup logGroup, string logStream = null)
{
client.DescribeLogStreams(new DescribeLogStreamsRequest(""));
}
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, string outputFilePath = null)
{
client.GetLogEvents(new GetLogEventsRequest("", ""));
}
private static int ReadIntBetween(string message, int min, int max)
{
Console.Write(message);
int num;
while (!int.TryParse(Console.ReadLine(), out num) && num >= min && num <= max)
Console.Write(Environment.NewLine + "Please enter an integer between " + min + " and " + max);
return num;
}
}
}

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AWSSDK.CloudWatchLogs" version="3.1.3.1" targetFramework="net45" />
<package id="AWSSDK.Core" version="3.1.11.0" targetFramework="net45" />
<package id="CommandLineParser" version="1.9.71" targetFramework="net45" />
<package id="Costura.Fody" version="1.3.3.0" targetFramework="net45" developmentDependency="true" />
<package id="Fody" version="1.28.3" targetFramework="net45" developmentDependency="true" />
</packages>