adds debug logging
This commit is contained in:
@@ -56,6 +56,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DebugLogger.cs" />
|
||||
<Compile Include="Models\CommandOptions.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
87
CloudWatchLogDownloader/DebugLogger.cs
Normal file
87
CloudWatchLogDownloader/DebugLogger.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
|
||||
namespace CloudWatchLogDownloader
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,9 @@ namespace CloudWatchLogDownloader.Models
|
||||
[Option('l', "liveStream", HelpText="Keep pulling logs until Ctrl+C is applied")]
|
||||
public bool LiveStream { get; set; }
|
||||
|
||||
[Option('d',"debug", HelpText = "print additional logs to console")]
|
||||
public bool Debug { get; set; }
|
||||
|
||||
[ParserState]
|
||||
public IParserState ParserState { get; set; }
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ namespace CloudWatchLogDownloader
|
||||
var opt = new CommandOptions();
|
||||
if (CommandLine.Parser.Default.ParseArguments(args, opt))
|
||||
{
|
||||
DebugLogger.Debug = opt.Debug;
|
||||
|
||||
client = new AmazonCloudWatchLogsClient();
|
||||
var logGroup = GetLogGroup(opt.LogGroup);
|
||||
var logStream = GetLogStream(logGroup, opt.LogStream);
|
||||
@@ -29,8 +31,10 @@ namespace CloudWatchLogDownloader
|
||||
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] == '*')
|
||||
@@ -64,12 +68,14 @@ namespace CloudWatchLogDownloader
|
||||
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] == '*')
|
||||
@@ -126,6 +132,7 @@ namespace CloudWatchLogDownloader
|
||||
GetLogEventsResponse leResponse = null;
|
||||
do
|
||||
{
|
||||
DebugLogger.WriteLine("Getting events...");
|
||||
leResponse = client.GetLogEvents(new GetLogEventsRequest
|
||||
{
|
||||
LogGroupName = logGroup.LogGroupName,
|
||||
@@ -133,13 +140,14 @@ namespace CloudWatchLogDownloader
|
||||
StartFromHead = true,
|
||||
NextToken = (leResponse != null ? leResponse.NextForwardToken : null)
|
||||
});
|
||||
DebugLogger.WriteLine("Got {0} events", leResponse.Events.Count);
|
||||
|
||||
foreach (var ev in leResponse.Events)
|
||||
sw.WriteLine(ev.Message);
|
||||
|
||||
sw.Flush();
|
||||
|
||||
if (!leResponse.Events.Any() && !lsMessage)
|
||||
if (!leResponse.Events.Any() && !lsMessage && liveStream)
|
||||
{
|
||||
lsMessage = true;
|
||||
ConsoleColor oldcolor = Console.ForegroundColor;
|
||||
|
||||
Reference in New Issue
Block a user