Log client execution to XML report

Replace console output with synchronized XML logging, including
timestamps, startup and shutdown events, errors, cancellations,
timeouts, restarts, and abnormal server exits. Save the completed
report as `ClientReport.xml` in the executable directory.
This commit is contained in:
Matthias Heil
2026-11-10 10:20:18 +01:00
parent 04e94edd64
commit 8fbb2e2e46
+32 -18
View File
@@ -1,8 +1,13 @@
using System.Diagnostics; using System.Diagnostics;
using System.Xml.Linq;
using PipeMethodCalls; using PipeMethodCalls;
using ClientServerShared; using ClientServerShared;
Console.WriteLine("Client started."); // All output is written to an XML report next to the executable
string reportPath = Path.Combine(AppContext.BaseDirectory, "ClientReport.xml");
var report = new XElement("Report", new XAttribute("Started", DateTime.Now.ToString("O")));
Log("Client started.");
// Unique pipe name per client run, so multiple clients/servers do not interfere // Unique pipe name per client run, so multiple clients/servers do not interfere
string pipeName = $"PipeDemo-{Environment.ProcessId}-{Guid.NewGuid():N}"; string pipeName = $"PipeDemo-{Environment.ProcessId}-{Guid.NewGuid():N}";
@@ -12,9 +17,9 @@ PipeClient<ICalculator> client = null!;
try try
{ {
Console.WriteLine($"Starting server on pipe {pipeName}..."); Log($"Starting server on pipe {pipeName}...");
await StartServerAndConnectAsync(pipeName); await StartServerAndConnectAsync(pipeName);
Console.WriteLine("Connected to server."); Log("Connected to server.");
await BasicCallsExampleAsync(); await BasicCallsExampleAsync();
await ServerExceptionsExampleAsync(); await ServerExceptionsExampleAsync();
@@ -26,9 +31,9 @@ catch (Exception ex) when (ex is not PipeInvokeFailedException)
{ {
// Communication failed: check whether the server process died // Communication failed: check whether the server process died
if (serverProcess is not null && serverProcess.WaitForExit(2000)) if (serverProcess is not null && serverProcess.WaitForExit(2000))
Console.WriteLine($"Connection lost, server terminated with exit code {serverProcess.ExitCode}: {ex.GetType().Name}: {ex.Message}"); Log($"Connection lost, server terminated with exit code {serverProcess.ExitCode}: {ex.GetType().Name}: {ex.Message}");
else else
Console.WriteLine($"Communication error: {ex.GetType().Name}: {ex.Message}"); Log($"Communication error: {ex.GetType().Name}: {ex.Message}");
} }
finally finally
{ {
@@ -42,21 +47,30 @@ finally
} }
} }
Console.WriteLine("Client finished."); Log("Client finished.");
lock (report)
new XDocument(report).Save(reportPath);
// Thread-safe: also called from the Process.Exited event
void Log(string message)
{
lock (report)
report.Add(new XElement("Entry", new XAttribute("Time", DateTime.Now.ToString("O")), message));
}
async Task BasicCallsExampleAsync() async Task BasicCallsExampleAsync()
{ {
int sum = await client.InvokeAsync(c => c.Add(2, 3)); int sum = await client.InvokeAsync(c => c.Add(2, 3));
Console.WriteLine($"Add(2, 3) = {sum}"); Log($"Add(2, 3) = {sum}");
var vector = await client.InvokeAsync(c => c.AddVector(new System.Numerics.Vector3(1, 2, 3), new System.Numerics.Vector3(4, 5, 6))); var vector = await client.InvokeAsync(c => c.AddVector(new System.Numerics.Vector3(1, 2, 3), new System.Numerics.Vector3(4, 5, 6)));
Console.WriteLine($"AddVector((1,2,3), (4,5,6)) = {vector}"); Log($"AddVector((1,2,3), (4,5,6)) = {vector}");
double quotient = await client.InvokeAsync(c => c.Divide(10, 4)); double quotient = await client.InvokeAsync(c => c.Divide(10, 4));
Console.WriteLine($"Divide(10, 4) = {quotient}"); Log($"Divide(10, 4) = {quotient}");
string echo = await client.InvokeAsync(c => c.EchoAsync("Hallo")); string echo = await client.InvokeAsync(c => c.EchoAsync("Hallo"));
Console.WriteLine(echo); Log(echo);
} }
// Server-side exceptions are forwarded to the client // Server-side exceptions are forwarded to the client
@@ -68,7 +82,7 @@ async Task ServerExceptionsExampleAsync()
} }
catch (PipeInvokeFailedException ex) catch (PipeInvokeFailedException ex)
{ {
Console.WriteLine($"Server exception (Divide): {ex.Message}"); Log($"Server exception (Divide): {ex.Message}");
} }
try try
@@ -77,7 +91,7 @@ async Task ServerExceptionsExampleAsync()
} }
catch (PipeInvokeFailedException ex) catch (PipeInvokeFailedException ex)
{ {
Console.WriteLine($"Server exception (Echo): {ex.Message}"); Log($"Server exception (Echo): {ex.Message}");
} }
} }
@@ -91,12 +105,12 @@ async Task CancellationExampleAsync()
try try
{ {
int result = await client.InvokeAsync(c => c.SlowOperationAsync(2000, operationId), invokeCts.Token); int result = await client.InvokeAsync(c => c.SlowOperationAsync(2000, operationId), invokeCts.Token);
Console.WriteLine($"SlowOperationAsync = {result}"); Log($"SlowOperationAsync = {result}");
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
await client.InvokeAsync(c => c.CancelOperation(operationId)); await client.InvokeAsync(c => c.CancelOperation(operationId));
Console.WriteLine("SlowOperationAsync canceled after 200 ms."); Log("SlowOperationAsync canceled after 200 ms.");
} }
} }
@@ -110,15 +124,15 @@ async Task KillOnTimeoutExampleAsync()
} }
catch (OperationCanceledException) when (serverProcess is not null) catch (OperationCanceledException) when (serverProcess is not null)
{ {
Console.WriteLine("Server did not respond within 1 s. Killing server process..."); Log("Server did not respond within 1 s. Killing server process...");
serverProcess.Kill(entireProcessTree: true); serverProcess.Kill(entireProcessTree: true);
await serverProcess.WaitForExitAsync(); await serverProcess.WaitForExitAsync();
serverProcess.Dispose(); serverProcess.Dispose();
client.Dispose(); client.Dispose();
Console.WriteLine("Restarting server..."); Log("Restarting server...");
await StartServerAndConnectAsync(pipeName); await StartServerAndConnectAsync(pipeName);
Console.WriteLine("Reconnected to server."); Log("Reconnected to server.");
} }
} }
@@ -132,7 +146,7 @@ async Task StartServerAndConnectAsync(string pipeName)
process.Exited += (_, _) => process.Exited += (_, _) =>
{ {
if (process.ExitCode != 0) if (process.ExitCode != 0)
Console.WriteLine($"Server process exited abnormally (exit code {process.ExitCode})."); Log($"Server process exited abnormally (exit code {process.ExitCode}).");
}; };
serverProcess = process; serverProcess = process;