Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
acf3f8c0fe | ||
|
|
ea09bd6b19 | ||
|
|
2bfa977d99 | ||
|
|
ddbfa25f8e |
@@ -17,6 +17,9 @@ namespace CloudWatchLogDownloader.Models
|
|||||||
[ValueOption(2)]
|
[ValueOption(2)]
|
||||||
public string OutputFilePath { get; set; }
|
public string OutputFilePath { get; set; }
|
||||||
|
|
||||||
|
[Option('l', "liveStream", HelpText="Keep pulling logs until Ctrl+C is applied")]
|
||||||
|
public bool LiveStream { get; set; }
|
||||||
|
|
||||||
[ParserState]
|
[ParserState]
|
||||||
public IParserState ParserState { get; set; }
|
public IParserState ParserState { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using Amazon;
|
||||||
|
|
||||||
namespace CloudWatchLogDownloader
|
namespace CloudWatchLogDownloader
|
||||||
{
|
{
|
||||||
@@ -19,7 +20,7 @@ namespace CloudWatchLogDownloader
|
|||||||
client = new AmazonCloudWatchLogsClient();
|
client = new AmazonCloudWatchLogsClient();
|
||||||
var logGroup = GetLogGroup(opt.LogGroup);
|
var logGroup = GetLogGroup(opt.LogGroup);
|
||||||
var logStream = GetLogStream(logGroup, opt.LogStream);
|
var logStream = GetLogStream(logGroup, opt.LogStream);
|
||||||
WriteLogToFile(logGroup, logStream, opt.OutputFilePath);
|
WriteLogToFile(logGroup, logStream, opt.LiveStream, opt.OutputFilePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +81,7 @@ namespace CloudWatchLogDownloader
|
|||||||
allStreams = allStreams.Where(x => x.LogStreamName.StartsWith(logStream)).ToList();
|
allStreams = allStreams.Where(x => x.LogStreamName.StartsWith(logStream)).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
allStreams = allStreams.OrderBy(x => x.CreationTime).ToList();
|
allStreams = allStreams.OrderByDescending(x => x.CreationTime).ToList();
|
||||||
|
|
||||||
for (int i = 0, len = allStreams.Count; i < len; ++i)
|
for (int i = 0, len = allStreams.Count; i < len; ++i)
|
||||||
Console.WriteLine(i + ") " + allStreams[i].LogStreamName);
|
Console.WriteLine(i + ") " + allStreams[i].LogStreamName);
|
||||||
@@ -99,7 +100,7 @@ namespace CloudWatchLogDownloader
|
|||||||
return ls;
|
return ls;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, string outputFilePath = null)
|
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, bool liveStream, string outputFilePath = null)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
|
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
|
||||||
var output = Console.ReadLine();
|
var output = Console.ReadLine();
|
||||||
@@ -108,6 +109,10 @@ namespace CloudWatchLogDownloader
|
|||||||
if (!Directory.GetParent(output).Exists)
|
if (!Directory.GetParent(output).Exists)
|
||||||
Directory.CreateDirectory(Directory.GetParent(output).FullName);
|
Directory.CreateDirectory(Directory.GetParent(output).FullName);
|
||||||
|
|
||||||
|
Console.WriteLine("Downloading log into " + output);
|
||||||
|
|
||||||
|
bool lsMessage = !liveStream;
|
||||||
|
|
||||||
using (StreamWriter sw = new StreamWriter(output))
|
using (StreamWriter sw = new StreamWriter(output))
|
||||||
{
|
{
|
||||||
GetLogEventsResponse leResponse = null;
|
GetLogEventsResponse leResponse = null;
|
||||||
@@ -125,7 +130,17 @@ namespace CloudWatchLogDownloader
|
|||||||
sw.WriteLine(ev.Message);
|
sw.WriteLine(ev.Message);
|
||||||
|
|
||||||
sw.Flush();
|
sw.Flush();
|
||||||
} while (leResponse.NextForwardToken != null && leResponse.Events.Any());
|
|
||||||
|
if (!leResponse.Events.Any() && !lsMessage)
|
||||||
|
{
|
||||||
|
lsMessage = true;
|
||||||
|
ConsoleColor oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||||
|
Console.WriteLine("Now streaming live from lg: " + logGroup.LogGroupName + " ls: " + logStream.LogStreamName + " into " + output);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
Console.WriteLine("{0}{0}Press CTRL+C to stop...", Environment.NewLine);
|
||||||
|
}
|
||||||
|
} while ((leResponse.NextForwardToken != null && leResponse.Events.Any()) || liveStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
24
README.md
Normal file
24
README.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# CloudWatchLogDownloader
|
||||||
|
Download full logs from AWS CloudWatch
|
||||||
|
|
||||||
|
## Who is it for?
|
||||||
|
For idiots like me who just want a text dump of the cloud watch log without going through S3
|
||||||
|
|
||||||
|
## Why .NET?
|
||||||
|
Because I was working with the .NET AWSSDK when writing this.
|
||||||
|
|
||||||
|
## Why not .NET CORE?
|
||||||
|
Because the AWSSDK.CloudWatchLogs nuget package doesn't support it. (As of writing this README)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
If you're still reading this means you want to use this abomination... Good.
|
||||||
|
|
||||||
|
### Command line params
|
||||||
|
|short|long|Description|
|
||||||
|
|-----|----|-----------|
|
||||||
|
|-g|--logGroup|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|
|
||||||
|
|-s|--logStream|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|
|
||||||
|
|-o|--outputFile|The file to output the logs to.|
|
||||||
|
|-l|--liveStream|After reaching the newest log keep polling until ctrl+c is pressed|
|
||||||
|
|
||||||
|
If any of the above are not specified the program will prompt for them
|
||||||
Reference in New Issue
Block a user