Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
556 lines (460 loc) · 26.8 KB
/
Copy pathProgram.cs
File metadata and controls
556 lines (460 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// =============================================================================
// GPAL - Generally Positive Automation Library
// Copyright © 2026 Software Decisions, Inc. All rights reserved.
//
// This file is part of GPAL.
// Licensed under the Business Source License 1.1
//
// Primary development, architecture, and vision by Michael B. Vederman,
// CEO of Software Decisions, Inc., Texas.
//
// Internal development maintained privately.
// Public releases appear on GitHub: https://github.com/SoftwareDecisionsInc/GPAL.
//
// See LICENSE for full terms, including Additional Use Grant.
// =============================================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
using GenerallyPositive;
using GenerallyPositive.Browser;
using GenerallyPositive.GPALForm;
using static GenerallyPositive.Enums;
namespace InputGridChaining
{
/// <summary>
/// Program that shows how to link workflows using separate Units of Work and using the CallAfterFillIn and CallIfFound handlers.<br/>
/// </summary>
// the eBay search workflow run once for every browser and engine ticked, from a form so it can run on a hidden
// desktop and be peeked at while it does. Everything GPAL publishes goes to the Log tab.
class Program
{
// the browser the current pass is using
static IBrowser browser { get; set; }
static bool running { get; set; }
// Set by Stop. No pass starts after it, and the running pass is ended by closing its browser.
static bool stopRequested { get; set; }
// ── Run tab ───────────────────────────────────────────────────────────
static GPALTab runTab = GPAL.Tab.WithName("runTab").WithText("Run").ToGPALObject();
static GPALTextArea aboutBlurb = (GPALTextArea)GPAL.TextArea
.WithReadOnly(true)
.WithHeight(130)
.WithName("aboutBlurb")
.WithText("Chains three workflows on eBay on the browsers and engines checked below, each browser on its own signed-in profile or, as set on the Profiles tab, a temporary one. Three random Star Wars search terms are filled into the search box from an input grid. After each fill, CallAfterFillIn clicks Search, and CallIfFound opens the first results in new tabs and reads each item's description, seller, feedback and link into one grid. A persistent selector dismisses the eBay popup that shows once per session. The grid is saved with headers beside the program as all.<engine>.<browser>.<mode>.txt. PuppeteerPort and PuppeteerPipe have no Firefox, so those pairs are skipped." + Environment.NewLine + Environment.NewLine +
"Every pass runs in a window. Headless adds a second, headless round of the same passes. On a hidden desktop the browsers never appear; Peek shows the running one for a minute, or until Escape. The Log tab shows everything GPAL publishes, with run times at the end.")
.ToGPALObject();
static GPALCheckbox hiddenDesktopCheck = GPAL.CheckboxFor("Run on a hidden desktop", true);
// GPAL's own tracing, where a click says how it was dispatched
static GPALCheckbox debugCheck = GPAL.CheckboxFor("Include DEBUG in the log", true);
// adds a headless round after the windowed one, it never replaces it
static GPALCheckbox headlessCheck = GPAL.CheckboxFor("Add headless", false);
static GPALCheckbox chromeCheck = GPAL.CheckboxFor("Chrome", true);
static GPALCheckbox edgeCheck = GPAL.CheckboxFor("Edge", true);
static GPALCheckbox firefoxCheck = GPAL.CheckboxFor("Firefox", true);
static GPALCheckbox seleniumCheck = GPAL.CheckboxFor("Selenium", true);
static GPALCheckbox puppeteerCheck = GPAL.CheckboxFor("PuppeteerPort", true);
static GPALCheckbox puppeteerPipeCheck = GPAL.CheckboxFor("PuppeteerPipe", true);
static GPALCheckbox ottoMagicCheck = GPAL.CheckboxFor("OttoMagic", false);
// runs SeleniumHW, PuppeteerPortHW, PuppeteerPipeHW and OttoMagicHW in place of the four above
static GPALCheckbox hardwareCheck = GPAL.CheckboxFor("Use the hardware variants", false);
// Three groups side by side: run options, browsers, engines. A splitter holds two, so browsers and engines
// share a second splitter right of the first group. Declared after the checkboxes because static fields
// initialize in order.
static GPALSplitter browsersAndEngines = (GPALSplitter)GPAL.Splitter.HSplit(0.5)
.WithFormControl(chromeCheck)
.WithFormControl(edgeCheck)
.WithFormControl(firefoxCheck)
.WithFormControl(GPAL.SplitRight)
.WithFormControl(seleniumCheck)
.WithFormControl(puppeteerCheck)
.WithFormControl(puppeteerPipeCheck)
.WithFormControl(ottoMagicCheck)
.WithFormControl(hardwareCheck)
.WithHeight(145)
.ToGPALObject();
static GPALSplitter runOptions = (GPALSplitter)GPAL.Splitter.HSplit(0.34)
.WithFormControl(hiddenDesktopCheck)
.WithFormControl(debugCheck)
.WithFormControl(headlessCheck)
.WithFormControl(GPAL.SplitRight)
.WithFormControl(browsersAndEngines)
.WithHeight(145)
.ToGPALObject();
static GPALButton runBtn = GPAL.Button
.WithName("run")
.WithText("▶ Run every browser and engine")
.WithCallback<EventHandler>((s, e) => runBtn.Enabled = false)
.WithCallbackOffUIThread<EventHandler>(OnRun)
.ToGPALObject();
static GPALButton stopBtn = GPAL.Button
.WithEnabled(false)
.WithName("stop")
.WithText("⏹ Stop")
.WithCallback<EventHandler>((s, e) => stopBtn.Enabled = false)
.WithCallbackOffUIThread<EventHandler>(OnStop)
.ToGPALObject();
// shows the running browser's hidden desktop for a minute, or until Escape or its Return button
static GPALButton peekBtn = GPAL.Button
.WithEnabled(false)
.WithName("peek")
.WithText("👁 Peek")
.WithCallback<EventHandler>(OnPeek)
.ToGPALObject();
static GPALButton exitBtn = GPAL.Button
.WithName("exit")
.WithText("🚪 Exit")
.WithCallback<EventHandler>((s, e) => System.Windows.Forms.Application.Exit())
.ToGPALObject();
// ── Profiles tab ──────────────────────────────────────────────────────
static GPALTab profilesTab = GPAL.Tab.WithName("profilesTab").WithText("Profiles").ToGPALObject();
// Use profiles off, or a blank folder, runs that browser on a temporary profile GPAL creates and deletes.
static GPALCheckbox useProfilesCheck = GPAL.CheckboxFor("Use profiles", true);
static GPALLabel profilesHint = GPAL.LabelFor("A browser with a blank folder, or every browser when Use profiles is off, runs on a temporary profile.");
static GPALFileSelector chromeProfile = (GPALFileSelector)GPAL.FileSelector
.WithSelectFolder(true)
.WithCompact(true)
.WithTitle("Chrome profile folder")
.WithPlaceholder("Chrome profile folder")
.WithSelectedPath(@"C:\profiles\chrome\signedin")
.WithName("chromeProfile")
.ToGPALObject();
static GPALFileSelector edgeProfile = (GPALFileSelector)GPAL.FileSelector
.WithSelectFolder(true)
.WithCompact(true)
.WithTitle("Edge profile folder")
.WithPlaceholder("Edge profile folder")
.WithSelectedPath(@"C:\profiles\edge\signedin")
.WithName("edgeProfile")
.ToGPALObject();
static GPALFileSelector firefoxProfile = (GPALFileSelector)GPAL.FileSelector
.WithSelectFolder(true)
.WithCompact(true)
.WithTitle("Firefox profile folder")
.WithPlaceholder("Firefox profile folder")
.WithSelectedPath(@"C:\profiles\firefox\signedin")
.WithName("firefoxProfile")
.ToGPALObject();
// ── Log tab ───────────────────────────────────────────────────────────
static GPALTab logTab = GPAL.Tab.WithName("logTab").WithText("Log").ToGPALObject();
// The Log tab shows every event GPAL publishes. The status strip shows only what is running now.
static GPALTextArea logArea = (GPALTextArea)GPAL.TextArea.WithReadOnly(true).WithHeight(420).WithName("log");
// Every Log tab line since the last Clear Log. The box keeps only the last 2000 lines, which drops the start of
// a long run; this keeps all of it for Save Log.
static List<string> fullLog = new List<string>();
static GPALButton clearLogBtn = GPAL.Button
.WithName("clearLog")
.WithText("🧹 Clear Log")
.WithCallback<EventHandler>(OnClearLog)
.ToGPALObject();
// writes every line since the last Clear Log, not only the 2000 the box is showing
static GPALButton saveLogBtn = GPAL.Button
.WithName("saveLog")
.WithText("💾 Save Log")
.WithCallback<EventHandler>(OnSaveLog)
.ToGPALObject();
static GPALStatusStrip statusBar = GPAL.StatusStrip
.WithText("Ready")
.WithName("status")
.ToGPALObject();
[STAThread]
static void Main()
{
#region <GPAL Global Settings>
// Hook into GPAL's event system. You do not handle exceptions; you handle events about them.
GPAL
.WithDriverLocation(System.AppDomain.CurrentDomain.BaseDirectory)
.WithAutoUpdateWebDriver()
.WithPublishStackTrace()
//.WithNoFallbackActions()
.WithSimulateMouseMovement()
;
// Leave out DEBUG and DEEPDEBUG; they are GPAL's internal tracing, not for a person to read.
GPAL.WithHandlerEvents(GPALEventType.All & ~(GPALEventType.DEBUG | GPALEventType.DEEPDEBUG))
.WithExceptionHandler(OnGPALEvent)
.WithInformationHandler(OnGPALEvent);
#endregion <GPAL Global Settings>
var form = GPAL.Form
.WithTitle("InputGridChaining | every browser and engine")
.WithWidth(760)
.WithHeight(640)
.WithFormControl(statusBar)
.WithFormControl(runTab)
.WithFormControl(aboutBlurb)
.WithFormControl(runOptions)
.WithFormControl(GPAL.LabelFor("OttoMagic needs its browser extension and GPALRestAPI installed, both from:"))
.WithFormControl(GPAL.LinkLabelFor("https://gpal.sditx.com/Ottomagic"))
.WithFormControl(GPAL.SpacerFor(18))
.WithFormControl(runBtn)
.WithFormControl(stopBtn)
.WithFormControl(peekBtn)
.WithFormControl(GPAL.SpacerFor(18))
.WithFormControl(exitBtn)
.WithFormControl(profilesTab)
.WithFormControl(useProfilesCheck)
.WithFormControl(profilesHint)
.WithFormControl(GPAL.SpacerFor(12))
.WithFormControl(GPAL.LabelFor("Chrome"))
.WithFormControl(chromeProfile)
.WithFormControl(GPAL.LabelFor("Edge"))
.WithFormControl(edgeProfile)
.WithFormControl(GPAL.LabelFor("Firefox"))
.WithFormControl(firefoxProfile)
.WithFormControl(logTab)
.WithFormControl(clearLogBtn)
.WithFormControl(saveLogBtn)
.WithFormControl(logArea)
.ToGPALObject();
form.ShowDialog();
browser?.Close(true);
}
static void OnRun(object s, EventArgs e)
{
if (true == running)
return;
running = true;
stopRequested = false;
runBtn.Enabled = false;
stopBtn.Enabled = true;
bool hidden = hiddenDesktopCheck.Checked;
bool headless = headlessCheck.Checked;
// Read the controls once, at the start of the run.
List<BrowserType> browsers = new List<BrowserType>();
if (true == chromeCheck.Checked) browsers.Add(BrowserType.Chrome);
if (true == edgeCheck.Checked) browsers.Add(BrowserType.Edge);
if (true == firefoxCheck.Checked) browsers.Add(BrowserType.FireFox);
bool hardware = hardwareCheck.Checked;
List<AutomationEngine> engines = new List<AutomationEngine>();
if (true == seleniumCheck.Checked) engines.Add(true == hardware ? AutomationEngine.SeleniumHW : AutomationEngine.Selenium);
if (true == puppeteerCheck.Checked) engines.Add(true == hardware ? AutomationEngine.PuppeteerPortHW : AutomationEngine.PuppeteerPort);
if (true == puppeteerPipeCheck.Checked) engines.Add(true == hardware ? AutomationEngine.PuppeteerPipeHW : AutomationEngine.PuppeteerPipe);
if (true == ottoMagicCheck.Checked) engines.Add(true == hardware ? AutomationEngine.OttoMagicHW : AutomationEngine.OttoMagic);
// each browser's profile folder, or null for a temporary profile
bool useProfiles = useProfilesCheck.Checked;
Dictionary<BrowserType, string> profiles = new Dictionary<BrowserType, string>
{
{ BrowserType.Chrome, true == useProfiles && false == string.IsNullOrWhiteSpace(chromeProfile.SelectedPath) ? chromeProfile.SelectedPath.Trim() : null },
{ BrowserType.Edge, true == useProfiles && false == string.IsNullOrWhiteSpace(edgeProfile.SelectedPath) ? edgeProfile.SelectedPath.Trim() : null },
{ BrowserType.FireFox, true == useProfiles && false == string.IsNullOrWhiteSpace(firefoxProfile.SelectedPath) ? firefoxProfile.SelectedPath.Trim() : null },
};
// DEEPDEBUG stays out either way; raw protocol traffic buries everything else.
GPAL.WithHandlerEvents(true == debugCheck.Checked
? GPALEventType.All & ~GPALEventType.DEEPDEBUG
: GPALEventType.All & ~(GPALEventType.DEBUG | GPALEventType.DEEPDEBUG));
try
{
RunEverything(hidden, headless, browsers, engines, profiles);
}
catch (Exception ex)
{
// Each pass catches its own exceptions, so this only catches the run itself failing, and keeps the
// form usable.
GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, "The run ended in an exception", ex: ex);
}
finally
{
browser = null;
running = false;
stopBtn.Enabled = false;
runBtn.Enabled = true;
// Each browser's hidden desktop is released when it closes, so none is left to peek at.
peekBtn.Enabled = false;
SetStatus(true == stopRequested
? "Stopped. The run times for the passes that ran are at the end of the Log tab."
: "Done. The run times are at the end of the Log tab.");
}
}
// No pass starts after this. The running pass is ended by closing its browser: its next step fails, the pass
// catches it, and the run stops. Closed off the UI thread because closing can take seconds.
static void OnStop(object s, EventArgs e)
{
stopRequested = true;
stopBtn.Enabled = false;
SetStatus("Stopping: closing the browser that is running now...");
GPAL.PublishSimpleEvent(GPALEventType.NOTICE, "Stop pressed, closing the running browser and starting no more passes");
IBrowser closing = browser;
try
{
closing?.Close(true);
}
catch (Exception ex)
{
GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, "Stop could not close the running browser. It closes when the pass ends.", ex: ex);
}
}
static void RunEverything(bool hidden, bool headless, List<BrowserType> browsers, List<AutomationEngine> engines, Dictionary<BrowserType, string> profiles)
{
// the search terms every pass fills in, picked once per run
Workflows.PrepareInputGrid();
// Ends any browser an earlier run left holding a profile. A temporary profile is new each pass, so
// nothing holds it.
foreach (string profile in profiles.Values)
if (null != profile)
EndWhatIsLeftOn(profile, "an earlier run");
// the windowed round always runs. headless adds a second round after it
bool[] rounds = true == headless ? new[] { false, true } : new[] { false };
foreach (bool asHeadless in rounds)
{
if (true == stopRequested)
break;
RunRound(hidden, asHeadless, browsers, engines, profiles);
}
}
// one round: every checked browser with every checked engine, windowed or headless
static void RunRound(bool hidden, bool asHeadless, List<BrowserType> browsers, List<AutomationEngine> engines, Dictionary<BrowserType, string> profiles)
{
string mode = true == asHeadless ? "headless" : "windowed";
// accumulate wall-clock runtime per (browser, engine) for the end-of-run table
var runtimes = new Dictionary<(BrowserType bt, AutomationEngine ae), TimeSpan>();
foreach (BrowserType bt in browsers)
{
if (true == stopRequested)
break;
foreach (AutomationEngine ae in engines)
{
//AutomationEngine ae = AutomationEngine.OttoMagic;
if (true == stopRequested)
break;
// puppeteer drives chromium only
if (BrowserType.FireFox == bt && (AutomationEngine.PuppeteerPort == ae || AutomationEngine.PuppeteerPortHW == ae ||
AutomationEngine.PuppeteerPipe == ae || AutomationEngine.PuppeteerPipeHW == ae))
continue;
// null is a temporary profile, which GPAL makes for the pass and deletes when it closes
string profile = profiles[bt];
string onProfile = null == profile ? "a temporary profile" : $"profile [{profile}]";
SetStatus($"Running [{bt}] [{ae}] {mode}{(true == hidden ? " on a hidden desktop" : string.Empty)}...");
GPAL.PublishSimpleEvent(GPALEventType.NOTICE, $"Starting [{bt}] [{ae}] [{mode}] on {onProfile}");
// our main workflow just enters in the search terms into the input, then once the first row of tokens are entered, CallAfterFillIn is invoked
// CallAfterFillIn clicks search, then CallAfterFillIn method chains to CallIfFound to go to the product page and retrieve the results
Stopwatch runStopwatch = Stopwatch.StartNew();
// A browser and engine pair that throws is its own result; the rest still run.
try
{
// Stored before navigating, so a pass that fails while the browser starts still has a browser
// to close.
browser = GPAL.Browser
.WithBrowserType(bt)
.WithUseStealth(StealthType.PatchDriver)
.WithAutomationEngine(ae)
.WithLoadImages(false)
.WithHiddenDesktop(hidden)
.WithProfileDataDirectory(profile) // null means a temporary profile
.ToGPALObject();
// a hidden desktop exists from here on, so Peek has one to show
if (true == hidden)
peekBtn.Enabled = true;
Workflows.StartWorkflow(browser, bt, ae, mode, asHeadless);
browser
.Close(true); // close the browser and [true] kill the webdriver that keeps running
}
catch (Exception ex)
{
GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, $"[{bt}] [{ae}] [{mode}] ended in an exception, going on to the next", ex: ex);
// Close this pass's browser so the next pass does not start beside it.
try { browser?.Close(true); } catch (Exception) { }
}
finally
{
browser = null;
runStopwatch.Stop();
runtimes[(bt, ae)] = runStopwatch.Elapsed;
if (null != profile)
EndWhatIsLeftOn(profile, $"[{bt}] [{ae}] [{mode}]");
}
}
}
GPAL.EmitRuntimeTable($"{(true == hidden ? "Hidden desktop" : "Window")}, {mode}", runtimes);
}
// Browsers still running on the profile after a pass closed its browser. One left there locks the profile for
// every later pass, so it is logged with the pass that leaked it and killed. Only the command line shows the
// profile, so WMI is queried.
static void EndWhatIsLeftOn(string profile, string who)
{
// Give a browser already shutting down a moment to release its profile lock.
System.Threading.Thread.Sleep(2_000);
List<int> leftBehind = new List<int>();
// Runs from each pass's finally, so a WMI that does not answer is logged and the run goes on.
try
{
using (var search = new System.Management.ManagementObjectSearcher(
"SELECT ProcessId, CommandLine FROM Win32_Process WHERE Name = 'chrome.exe' OR Name = 'msedge.exe' OR Name = 'firefox.exe'"))
foreach (System.Management.ManagementObject found in search.Get())
{
string commandLine = found["CommandLine"]?.ToString() ?? string.Empty;
// Main process only; its children exit with it. GPAL writes the profile with forward slashes on
// Chromium's command line, so slashes are normalized before comparing.
bool onProfile = commandLine.Replace('/', '\\').IndexOf(profile.TrimEnd('\\'), StringComparison.OrdinalIgnoreCase) >= 0;
if (true == onProfile && false == commandLine.Contains("--type="))
leftBehind.Add(Convert.ToInt32(found["ProcessId"]));
}
if (0 < leftBehind.Count)
{
GPAL.PublishSimpleEvent(GPALEventType.CAUTION, $"[{who}] left [{leftBehind.Count}] browser(s) running on [{profile}], process [{string.Join(", ", leftBehind)}]. Ending them so the next pass can use the profile.");
foreach (int pid in leftBehind)
using (Process taskkill = Process.Start(new ProcessStartInfo("taskkill", $"/PID {pid} /T /F") { CreateNoWindow = true, UseShellExecute = false }))
taskkill?.WaitForExit(5_000);
}
}
catch (Exception ex)
{
GPAL.PublishSimpleEvent(GPALEventType.EXCEPTION, $"Could not check what {who} left running on [{profile}]", ex: ex);
}
}
// puts the running browser's hidden desktop on the screen for a minute, or until Escape or the Return button on it
static void OnPeek(object s, EventArgs e)
{
string failed = GPAL.HiddenDesktop.Peek(60_000);
SetStatus(null == failed
? "Showing the hidden desktop for a minute. Press Escape or click Return to come back sooner."
: $"Cannot show the hidden desktop: {failed}");
}
static void OnGPALEvent(object sender, GPAL.GPALEventArgs e)
{
string tag = e.GPALEventType == GPALEventType.WARNING ? "WARN"
: e.GPALEventType == GPALEventType.ERROR ? "ERR "
: e.GPALEventType == GPALEventType.EXCEPTION ? "EXCP"
: e.GPALEventType == GPALEventType.FAILURE ? "FAIL"
: e.GPALEventType == GPALEventType.NOTICE ? "NOTE"
: e.GPALEventType == GPALEventType.DEBUG ? "DBG "
: "INFO";
string line = $"[{e.DateTimeStamp:HH:mm:ss}] [{tag}] {e.Message}";
if (e.ExceptionRaised != null) line += $" ({e.ExceptionRaised.Message})";
Debug.WriteLine(line);
// events arrive from the run's thread and the ui thread both
lock (fullLog)
fullLog.Add(line);
// AppendLine marshals to the ui thread itself and keeps the last 2000 lines
if (true == logArea.IsReady)
logArea.AppendLine(line, 2_000);
}
static void OnClearLog(object s, EventArgs e)
{
lock (fullLog)
fullLog.Clear();
logArea.Text = string.Empty;
}
// Asks where to save, then writes every line since the last Clear Log, up to the moment of the click.
static void OnSaveLog(object s, EventArgs e)
{
using (System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog
{
Title = "Save the log",
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",
FileName = $"{System.Diagnostics.Process.GetCurrentProcess().ProcessName}_{DateTime.Now:yyyyMMdd_HHmmss}.txt"
})
{
if (System.Windows.Forms.DialogResult.OK == dialog.ShowDialog())
{
string[] lines;
lock (fullLog)
lines = fullLog.ToArray();
System.IO.File.WriteAllLines(dialog.FileName, lines);
SetStatus($"Saved [{lines.Length}] log lines to [{dialog.FileName}]");
}
}
}
static void SetStatus(string text)
{
statusBar.Text = text;
}
}
}