Skip to content

Commit 5b9e384

Browse files
committed
Some updates to the console app
1 parent c96532e commit 5b9e384

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

.vscode/tasks.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@
2626

2727
{
2828
"taskName": "SampleConsole: Dotnet Build",
29-
"args": [ "dotnet build ${workspaceRoot}/src/Samples/Exceptionless.SampleConsole -c Debug" ],
29+
"args": [ "dotnet build src/Samples/Exceptionless.SampleConsole -c Debug" ],
3030
"isBuildCommand": true,
3131
"problemMatcher": "$msCompile"
3232
},
3333
{
3434
"taskName": "SampleConsole: Dotnet Run",
35-
"args": [ "dotnet run ${workspaceRoot}/src/Samples/Exceptionless.SampleConsole -c Debug" ],
35+
"args": [ "dotnet run src/Samples/Exceptionless.SampleConsole -c Debug" ],
3636
"isBuildCommand": true,
3737
"problemMatcher": "$msCompile"
3838
},
3939
{
4040
"taskName": "SampleAspNetCore: Dotnet Build",
41-
"args": [ "run ${workspaceRoot}/src/Samples/Exceptionless.SampleAspNetCore -c Debug" ],
41+
"args": [ "dotnet run src/Samples/Exceptionless.SampleAspNetCore -c Debug" ],
4242
"isBuildCommand": true,
4343
"problemMatcher": "$msCompile"
4444
},
4545
{
4646
"taskName": "SampleAspNetCore: Dotnet Run",
47-
"args": [ "run ${workspaceRoot}/src/Samples/Exceptionless.SampleAspNetCore -c Debug" ],
47+
"args": [ "dotnet run src/Samples/Exceptionless.SampleAspNetCore -c Debug" ],
4848
"isBuildCommand": true,
4949
"problemMatcher": "$msCompile"
5050
}

src/Samples/Exceptionless.SampleConsole/Program.cs

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public class Program {
3535

3636
public static void Main(string[] args) {
3737
Console.CursorVisible = false;
38-
StartDisplayingLogMessages();
38+
if (!Console.IsInputRedirected)
39+
StartDisplayingLogMessages();
3940

4041
ExceptionlessClient.Default.Configuration.UpdateSettingsWhenIdleInterval = TimeSpan.FromSeconds(15);
4142
ExceptionlessClient.Default.Configuration.UseTraceLogEntriesPlugin();
@@ -49,7 +50,7 @@ public static void Main(string[] args) {
4950
GlobalDiagnosticsContext.Set("GlobalProp", "GlobalValue");
5051
//Log.Info().Message("Hi").Tag("Tag1", "Tag2").Property("LocalProp", "LocalValue").MarkUnhandled("SomeMethod").ContextProperty("Blah", new Event()).Write();
5152

52-
//ExceptionlessClient.Default.SubmitLog(typeof(Program).Name, "Trace Message", LogLevel.Trace);
53+
//ExceptionlessClient.Default.SubmitLog(typeof(Program).Name, "Trace Message", LogLevel.Trace);
5354

5455
#if NET45
5556
// test log4net
@@ -79,7 +80,7 @@ public static void Main(string[] args) {
7980

8081
while (true) {
8182
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 1);
82-
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
83+
var keyInfo = Console.IsInputRedirected ? GetKeyFromRedirectedConsole() : Console.ReadKey(true);
8384

8485
if (keyInfo.Key == ConsoleKey.D1)
8586
SendEvent();
@@ -133,6 +134,40 @@ public static void Main(string[] args) {
133134
}
134135
}
135136

137+
private static ConsoleKeyInfo GetKeyFromRedirectedConsole() {
138+
string input = Console.In.ReadLine();
139+
switch (input?.ToLower()) {
140+
case "1":
141+
return new ConsoleKeyInfo('1', ConsoleKey.D1, false, false, false);
142+
case "2":
143+
return new ConsoleKeyInfo('2', ConsoleKey.D2, false, false, false);
144+
case "3":
145+
return new ConsoleKeyInfo('3', ConsoleKey.D3, false, false, false);
146+
case "4":
147+
return new ConsoleKeyInfo('4', ConsoleKey.D4, false, false, false);
148+
case "5":
149+
return new ConsoleKeyInfo('5', ConsoleKey.D5, false, false, false);
150+
case "6":
151+
return new ConsoleKeyInfo('6', ConsoleKey.D6, false, false, false);
152+
case "7":
153+
return new ConsoleKeyInfo('7', ConsoleKey.D7, false, false, false);
154+
case "8":
155+
return new ConsoleKeyInfo('8', ConsoleKey.D8, false, false, false);
156+
case "p":
157+
return new ConsoleKeyInfo('p', ConsoleKey.P, false, false, false);
158+
case "f":
159+
return new ConsoleKeyInfo('f', ConsoleKey.F, false, false, false);
160+
case "d":
161+
return new ConsoleKeyInfo('d', ConsoleKey.D, false, false, false);
162+
case "t":
163+
return new ConsoleKeyInfo('t', ConsoleKey.T, false, false, false);
164+
case "q":
165+
return new ConsoleKeyInfo('q', ConsoleKey.Q, false, false, false);
166+
}
167+
168+
return new ConsoleKeyInfo(' ', ConsoleKey.Escape, false, false, false);
169+
}
170+
136171
private static void SampleApiUsages() {
137172
ExceptionlessClient.Default.CreateLog("SampleConsole", "Has lots of extended data").AddObject(new {
138173
myApplicationVersion = new Version(1, 0),
@@ -168,12 +203,13 @@ private static void SampleApiUsages() {
168203
}
169204
}
170205

171-
private const int OPTIONS_MENU_LINE_COUNT = 15;
206+
private const int OPTIONS_MENU_LINE_COUNT = 14;
172207

173208
private static void WriteOptionsMenu() {
174209
lock (_writeLock) {
175210
Console.SetCursorPosition(0, 0);
176-
ClearConsoleLines(0, OPTIONS_MENU_LINE_COUNT - 1);
211+
ClearConsoleLines(0, OPTIONS_MENU_LINE_COUNT + LOG_LINE_COUNT);
212+
177213
Console.WriteLine("1: Send 1");
178214
Console.WriteLine("2: Send 100");
179215
Console.WriteLine("3: Send continuous");
@@ -194,27 +230,29 @@ private static void WriteOptionsMenu() {
194230
private static void ClearOutputLines(int delay = 1000) {
195231
Task.Run(() => {
196232
Thread.Sleep(delay);
197-
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT, OPTIONS_MENU_LINE_COUNT + 4);
233+
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT, OPTIONS_MENU_LINE_COUNT + LOG_LINE_COUNT);
198234
});
199235
}
200236

201237
private const int LOG_LINE_COUNT = 10;
202238

203239
private static void StartDisplayingLogMessages() {
204-
Task.Factory.StartNew(() => {
240+
Task.Factory.StartNew(async () => {
205241
while (true) {
206242
var logEntries = _log.GetLogEntries(LOG_LINE_COUNT);
207243
lock (_writeLock) {
208-
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT + 5, OPTIONS_MENU_LINE_COUNT + 6 + LOG_LINE_COUNT);
209-
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 6);
244+
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT + 4, OPTIONS_MENU_LINE_COUNT + LOG_LINE_COUNT);
245+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 4);
210246
foreach (var logEntry in logEntries) {
211247
var originalColor = Console.ForegroundColor;
212248
Console.ForegroundColor = GetColor(logEntry);
213249
Console.WriteLine(logEntry);
214250
Console.ForegroundColor = originalColor;
215251
}
216252
}
217-
Thread.Sleep(250);
253+
254+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 1);
255+
await Task.Delay(250);
218256
}
219257
});
220258
}
@@ -268,7 +306,8 @@ private static void SendContinuousEvents(int delay, CancellationToken token, int
268306
SendEvent(ev, false);
269307
eventCount++;
270308
lock (_writeLock) {
271-
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 4);
309+
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT, OPTIONS_MENU_LINE_COUNT + 3);
310+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 2);
272311
Console.WriteLine("Submitted {0} events.", eventCount);
273312
}
274313

@@ -289,7 +328,8 @@ private static void SendEvent(Event ev = null, bool writeToConsole = true) {
289328

290329
if (writeToConsole) {
291330
lock (_writeLock) {
292-
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 2);
331+
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT, OPTIONS_MENU_LINE_COUNT + 3);
332+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 1);
293333
Console.WriteLine("Submitted 1 event.");
294334
Trace.WriteLine("Submitted 1 event.");
295335
}
@@ -300,7 +340,8 @@ private static void SendEvent(Event ev = null, bool writeToConsole = true) {
300340

301341
private static void SendAllCapturedEventsFromDisk() {
302342
lock (_writeLock) {
303-
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 2);
343+
ClearConsoleLines(OPTIONS_MENU_LINE_COUNT, OPTIONS_MENU_LINE_COUNT + 3);
344+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 1);
304345
Console.WriteLine("Sending captured events...");
305346
}
306347

@@ -316,7 +357,7 @@ private static void SendAllCapturedEventsFromDisk() {
316357

317358
eventCount++;
318359
lock (_writeLock) {
319-
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 3);
360+
Console.SetCursorPosition(0, OPTIONS_MENU_LINE_COUNT + 2);
320361
Console.WriteLine("Sent {0} events.", eventCount);
321362
}
322363
}

0 commit comments

Comments
 (0)