brutal/samples/Hello/HelloMonitorHost.cs
Rian Drake d1ce547eb2 samples: bump every package pin to the latest release
Fixes #38

Co-authored-by: LesoDeVaren <268139647+LesoDeVaren@users.noreply.github.com>
2026-11-03 11:43:45 +13:00

41 lines
1.3 KiB
C#

/*
* HelloMonitorHost
*
* Starts the Monitor (a crash/log watchdog subprocess), logs a few lines to a file and prints them.
*/
#:package Ahwoo.Brutal.Monitor.Host@2026.10.3
#:package Ahwoo.Brutal.Monitor.Common@2026.10.3
// RIAN: We use this package for crash detection and dump collection at runtime.
using Brutal.Monitor.Common;
using Microsoft.Extensions.Logging;
using Monitor = Brutal.Monitor.Monitor;
Run();
static void Run()
{
var logDirectory = Path.Combine(Path.GetTempPath(), "HelloMonitor");
Directory.CreateDirectory(logDirectory);
string? logFile;
// Disposing the monitor flushes the log and stops the subprocess, so the log is read after this block.
using (var monitor = new Monitor("HelloMonitor")) // Starts the watchdog subprocess.
{
monitor.AddLogger(new LoggerOptions { LogDirectory = logDirectory });
var logger = monitor.LogProvider.CreateLogger("Hello");
logger.LogInformation("Hello from the Monitor host!");
for (var i = 0; i < 3; i++)
logger.LogWarning("Tick {Tick}", i);
logFile = monitor.LogProvider.LogFilePath;
Console.WriteLine($"Subprocess running: {monitor.SubprocessRunning}");
}
Console.WriteLine($"Log written to {logFile}:");
Console.WriteLine(File.ReadAllText(logFile!));
}