adds code
This commit is contained in:
41
CloudWatchLogUploader/BaseHelper.cs
Normal file
41
CloudWatchLogUploader/BaseHelper.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using Amazon.CloudWatchLogs;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
internal abstract class BaseHelper
|
||||
{
|
||||
protected readonly AmazonCloudWatchLogsClient client;
|
||||
public BaseHelper(AmazonCloudWatchLogsClient client)
|
||||
{
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
protected bool GetYesOrNo()
|
||||
{
|
||||
ConsoleKeyInfo key;
|
||||
do
|
||||
{
|
||||
key = Console.ReadKey(true);
|
||||
} while (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Y || key.Key == ConsoleKey.N);
|
||||
|
||||
switch (key.Key)
|
||||
{
|
||||
case ConsoleKey.Y:
|
||||
return true;
|
||||
case ConsoleKey.N:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@
|
||||
<AssemblyName>CloudWatchLogUploader</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.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\AWSSDK.CloudWatchLogs.3.3.1.3\lib\net45\AWSSDK.CloudWatchLogs.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="AWSSDK.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=885c28607f98e604, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\AWSSDK.Core.3.3.6.1\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,32 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BaseHelper.cs" />
|
||||
<Compile Include="CommandOptions.cs" />
|
||||
<Compile Include="DebugLogger.cs" />
|
||||
<Compile Include="LogGroupHelper.cs" />
|
||||
<Compile Include="LogStreamHelper.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.3.1.3\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.29.4\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.1.29.4\build\dotnet\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.29.4\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.1.29.4\build\dotnet\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">
|
||||
@@ -56,4 +89,7 @@
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
<Target AfterTargets="AfterBuild;NonWinFodyTarget" Name="CleanReferenceCopyLocalPaths">
|
||||
<Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />
|
||||
</Target>
|
||||
</Project>
|
||||
35
CloudWatchLogUploader/CommandOptions.cs
Normal file
35
CloudWatchLogUploader/CommandOptions.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using CommandLine;
|
||||
using CommandLine.Text;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
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', "inputFile", HelpText = "The file to output the logs to")]
|
||||
[ValueOption(2)]
|
||||
public string InputFilePath { get; set; }
|
||||
|
||||
[Option("overwrite", HelpText = "Deletes and recreates log stream if exists")]
|
||||
public bool OverwriteStream { get; set; }
|
||||
|
||||
[Option('d', "debug", HelpText = "print additional logs to console")]
|
||||
public bool Debug { get; set; }
|
||||
|
||||
[ParserState]
|
||||
public IParserState ParserState { get; set; }
|
||||
|
||||
[HelpOption]
|
||||
public string GetUsage()
|
||||
{
|
||||
return HelpText.AutoBuild(this, (current) => HelpText.DefaultParsingErrorsHandler(this, current));
|
||||
}
|
||||
}
|
||||
}
|
||||
87
CloudWatchLogUploader/DebugLogger.cs
Normal file
87
CloudWatchLogUploader/DebugLogger.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
internal static class DebugLogger
|
||||
{
|
||||
private static bool _debug;
|
||||
public static bool Debug
|
||||
{
|
||||
get { return _debug; }
|
||||
set
|
||||
{
|
||||
if (value && !_debug)
|
||||
{
|
||||
WriteLine("debug mode active");
|
||||
}
|
||||
_debug = value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Write(string str)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.Write(str);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Write(object obj)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.Write(obj);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Write(string format, params object[] args)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.Write(format, args);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteLine(string str)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.WriteLine(str);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteLine(object obj)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.WriteLine(obj);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteLine(string format, params object[] args)
|
||||
{
|
||||
if (_debug)
|
||||
{
|
||||
var oldcolor = Console.ForegroundColor;
|
||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||
Console.WriteLine(format, args);
|
||||
Console.ForegroundColor = oldcolor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
CloudWatchLogUploader/FodyWeavers.xml
Normal file
5
CloudWatchLogUploader/FodyWeavers.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Weavers>
|
||||
<Costura/>
|
||||
|
||||
</Weavers>
|
||||
88
CloudWatchLogUploader/LogGroupHelper.cs
Normal file
88
CloudWatchLogUploader/LogGroupHelper.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using Amazon.CloudWatchLogs;
|
||||
using Amazon.CloudWatchLogs.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
internal class LogGroupHelper : BaseHelper
|
||||
{
|
||||
public LogGroupHelper(AmazonCloudWatchLogsClient client) : base(client)
|
||||
{
|
||||
}
|
||||
|
||||
public LogGroup GetLogGroup(string logGroup = null)
|
||||
{
|
||||
List<LogGroup> allGroups = new List<LogGroup>();
|
||||
DescribeLogGroupsResponse lgResponse = null;
|
||||
do
|
||||
{
|
||||
DebugLogger.WriteLine("Getting logGroups...");
|
||||
lgResponse = client.DescribeLogGroups(new DescribeLogGroupsRequest { NextToken = (lgResponse != null ? lgResponse.NextToken : null) });
|
||||
allGroups.AddRange(lgResponse.LogGroups);
|
||||
DebugLogger.WriteLine("Got logGroups, have {0}, {1} more pages", allGroups.Count, (!string.IsNullOrWhiteSpace(lgResponse.NextToken) ? "still" : "no"));
|
||||
} while (!string.IsNullOrWhiteSpace(lgResponse.NextToken));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(logGroup) || logGroup[logGroup.Length - 1] == '*')
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(logGroup))
|
||||
{
|
||||
if (logGroup.EndsWith("*"))
|
||||
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 (-1 to create one): ", -1, allGroups.Count - 1);
|
||||
|
||||
if (num == -1)
|
||||
{
|
||||
if (logGroup[logGroup.Length - 1] == '*')
|
||||
return CreateLogGroup();
|
||||
}
|
||||
|
||||
Console.Clear();
|
||||
Console.WriteLine("You choose LogGroup: " + allGroups[num].LogGroupName);
|
||||
return allGroups[num];
|
||||
}
|
||||
|
||||
var lg = allGroups.FirstOrDefault(x => x.LogGroupName == logGroup);
|
||||
if (lg == null)
|
||||
{
|
||||
Console.WriteLine("The log group '" + logGroup + "' does not exist.");
|
||||
lg = CreateLogGroup();
|
||||
}
|
||||
|
||||
Console.WriteLine("You choose LogGroup: " + lg.LogGroupName);
|
||||
return lg;
|
||||
}
|
||||
|
||||
private LogGroup CreateLogGroup(string lgName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(lgName))
|
||||
{
|
||||
lgName = GetLogGroupNameToCreate();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Do you want to create the log group [Y/N] (default: N): " + lgName);
|
||||
if (!GetYesOrNo())
|
||||
{
|
||||
lgName = GetLogGroupNameToCreate();
|
||||
}
|
||||
}
|
||||
|
||||
client.CreateLogGroup(new CreateLogGroupRequest(lgName));
|
||||
|
||||
return client.DescribeLogGroups(new DescribeLogGroupsRequest { LogGroupNamePrefix = lgName }).LogGroups.FirstOrDefault();
|
||||
}
|
||||
|
||||
private string GetLogGroupNameToCreate()
|
||||
{
|
||||
Console.Write("Input log group name (Ctrl+C to abort): ");
|
||||
return Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
125
CloudWatchLogUploader/LogStreamHelper.cs
Normal file
125
CloudWatchLogUploader/LogStreamHelper.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using Amazon.CloudWatchLogs;
|
||||
using Amazon.CloudWatchLogs.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
internal class LogStreamHelper : BaseHelper
|
||||
{
|
||||
public LogStreamHelper(AmazonCloudWatchLogsClient client) : base(client)
|
||||
{
|
||||
}
|
||||
|
||||
public LogStream GetLogStream(LogGroup logGroup, string logStream = null)
|
||||
{
|
||||
List<LogStream> allStreams = new List<LogStream>();
|
||||
DescribeLogStreamsResponse lsResponse = null;
|
||||
do
|
||||
{
|
||||
DebugLogger.WriteLine("Getting logStreams...");
|
||||
lsResponse = client.DescribeLogStreams(new DescribeLogStreamsRequest
|
||||
{
|
||||
NextToken = (lsResponse != null ? lsResponse.NextToken : null),
|
||||
LogGroupName = logGroup.LogGroupName
|
||||
});
|
||||
allStreams.AddRange(lsResponse.LogStreams);
|
||||
DebugLogger.WriteLine("Got logStreams, have {0}, {1} more pages", allStreams.Count, (!string.IsNullOrWhiteSpace(lsResponse.NextToken) ? "still" : "no"));
|
||||
} while (!string.IsNullOrWhiteSpace(lsResponse.NextToken));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(logStream) || logStream[logStream.Length - 1] == '*')
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(logStream))
|
||||
{
|
||||
logStream = logStream.Substring(0, logStream.Length - 1);
|
||||
allStreams = allStreams.Where(x => x.LogStreamName.StartsWith(logStream)).ToList();
|
||||
}
|
||||
|
||||
allStreams = allStreams.OrderByDescending(x => x.CreationTime).ToList();
|
||||
|
||||
for (int i = 0, len = allStreams.Count; i < len; ++i)
|
||||
Console.WriteLine(i + ") " + allStreams[i].LogStreamName);
|
||||
int num = ReadIntBetween("Choose log stream: ", -1, allStreams.Count - 1);
|
||||
|
||||
if (num == -1)
|
||||
{
|
||||
if (logStream[logStream.Length - 1] == '*')
|
||||
return CreateLogStream(logGroup);
|
||||
}
|
||||
|
||||
Console.Clear();
|
||||
Console.WriteLine("You choose LogGroup: " + logGroup.LogGroupName + Environment.NewLine + "You choose LogStream: " + allStreams[num].LogStreamName);
|
||||
return allStreams[num];
|
||||
}
|
||||
|
||||
var ls = allStreams.FirstOrDefault(x => x.LogStreamName == logStream);
|
||||
if (ls == null)
|
||||
{
|
||||
Console.WriteLine("The log stream '" + logGroup + "' does not exist.");
|
||||
ls = CreateLogStream(logGroup);
|
||||
}
|
||||
|
||||
Console.WriteLine("You choose LogStream: " + ls.LogStreamName);
|
||||
return ls;
|
||||
}
|
||||
|
||||
public void UploadLogs(LogGroup logGroup, LogStream logStream, bool overwriteStream, string inputFilePath = null)
|
||||
{
|
||||
string input = inputFilePath;
|
||||
while (string.IsNullOrWhiteSpace(input))
|
||||
{
|
||||
Console.WriteLine("Choose input file: ");
|
||||
var possibleInput = Console.ReadLine();
|
||||
if (File.Exists(possibleInput) && !File.GetAttributes(possibleInput).HasFlag(FileAttributes.Directory))
|
||||
input = possibleInput;
|
||||
else
|
||||
Console.WriteLine(possibleInput + "doesn't exist or directory");
|
||||
}
|
||||
|
||||
Console.Clear();
|
||||
Console.WriteLine(string.Format("Log stream: {1}{0}Log group: {2}{0}Overwrite stream: {3}{0}Input file: {4}{0}Do you want to start the upload? [Y/N] (default:N)", Environment.NewLine, logGroup.LogGroupName, logStream.LogStreamName, overwriteStream, input));
|
||||
if (!GetYesOrNo())
|
||||
return;
|
||||
|
||||
using(var sr = new StreamReader(input))
|
||||
{
|
||||
List<InputLogEvent> events = new List<InputLogEvent>();
|
||||
|
||||
while (!sr.EndOfStream)
|
||||
{
|
||||
events.Add(new InputLogEvent { Message = sr.ReadLine(), Timestamp = DateTime.UtcNow });
|
||||
}
|
||||
|
||||
client.PutLogEvents(new PutLogEventsRequest(logGroup.LogGroupName, logStream.LogStreamName, events));
|
||||
}
|
||||
}
|
||||
|
||||
private LogStream CreateLogStream(LogGroup lg, string lsName = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(lsName))
|
||||
{
|
||||
lsName = GetLogStreamNameToCreate();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Do you want to create the log group [Y/N] (default: N): " + lsName);
|
||||
if (!GetYesOrNo())
|
||||
{
|
||||
lsName = GetLogStreamNameToCreate();
|
||||
}
|
||||
}
|
||||
|
||||
client.CreateLogStream(new CreateLogStreamRequest(lg.LogGroupName, lsName));
|
||||
|
||||
return client.DescribeLogStreams(new DescribeLogStreamsRequest { LogStreamNamePrefix = lsName, LogGroupName = lg.LogGroupName }).LogStreams.FirstOrDefault();
|
||||
}
|
||||
|
||||
private string GetLogStreamNameToCreate()
|
||||
{
|
||||
Console.Write("Input log stream name (Ctrl+C to abort): ");
|
||||
return Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Amazon.CloudWatchLogs;
|
||||
|
||||
namespace CloudWatchLogUploader
|
||||
{
|
||||
@@ -10,6 +7,20 @@ namespace CloudWatchLogUploader
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var opt = new CommandOptions();
|
||||
if (CommandLine.Parser.Default.ParseArguments(args, opt))
|
||||
{
|
||||
DebugLogger.Debug = opt.Debug;
|
||||
|
||||
var client = new AmazonCloudWatchLogsClient();
|
||||
var logGroup = new LogGroupHelper(client).GetLogGroup(opt.LogGroup);
|
||||
var logStreamHelper = new LogStreamHelper(client);
|
||||
var logStream = logStreamHelper.GetLogStream(logGroup, opt.LogStream);
|
||||
logStreamHelper.UploadLogs(logGroup, logStream, opt.OverwriteStream, opt.InputFilePath);
|
||||
|
||||
Console.Write("Upload complete, press any key to continue...");
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
8
CloudWatchLogUploader/packages.config
Normal file
8
CloudWatchLogUploader/packages.config
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="AWSSDK.CloudWatchLogs" version="3.3.1.3" targetFramework="net45" />
|
||||
<package id="AWSSDK.Core" version="3.3.6.1" 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.29.4" targetFramework="net45" developmentDependency="true" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user