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:
+32
-18
@@ -1,8 +1,13 @@
|
||||
using System.Diagnostics;
|
||||
using System.Xml.Linq;
|
||||
using PipeMethodCalls;
|
||||
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
|
||||
string pipeName = $"PipeDemo-{Environment.ProcessId}-{Guid.NewGuid():N}";
|
||||
@@ -12,9 +17,9 @@ PipeClient<ICalculator> client = null!;
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"Starting server on pipe {pipeName}...");
|
||||
Log($"Starting server on pipe {pipeName}...");
|
||||
await StartServerAndConnectAsync(pipeName);
|
||||
Console.WriteLine("Connected to server.");
|
||||
Log("Connected to server.");
|
||||
|
||||
await BasicCallsExampleAsync();
|
||||
await ServerExceptionsExampleAsync();
|
||||
@@ -26,9 +31,9 @@ catch (Exception ex) when (ex is not PipeInvokeFailedException)
|
||||
{
|
||||
// Communication failed: check whether the server process died
|
||||
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
|
||||
Console.WriteLine($"Communication error: {ex.GetType().Name}: {ex.Message}");
|
||||
Log($"Communication error: {ex.GetType().Name}: {ex.Message}");
|
||||
}
|
||||
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()
|
||||
{
|
||||
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)));
|
||||
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));
|
||||
Console.WriteLine($"Divide(10, 4) = {quotient}");
|
||||
Log($"Divide(10, 4) = {quotient}");
|
||||
|
||||
string echo = await client.InvokeAsync(c => c.EchoAsync("Hallo"));
|
||||
Console.WriteLine(echo);
|
||||
Log(echo);
|
||||
}
|
||||
|
||||
// Server-side exceptions are forwarded to the client
|
||||
@@ -68,7 +82,7 @@ async Task ServerExceptionsExampleAsync()
|
||||
}
|
||||
catch (PipeInvokeFailedException ex)
|
||||
{
|
||||
Console.WriteLine($"Server exception (Divide): {ex.Message}");
|
||||
Log($"Server exception (Divide): {ex.Message}");
|
||||
}
|
||||
|
||||
try
|
||||
@@ -77,7 +91,7 @@ async Task ServerExceptionsExampleAsync()
|
||||
}
|
||||
catch (PipeInvokeFailedException ex)
|
||||
{
|
||||
Console.WriteLine($"Server exception (Echo): {ex.Message}");
|
||||
Log($"Server exception (Echo): {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,12 +105,12 @@ async Task CancellationExampleAsync()
|
||||
try
|
||||
{
|
||||
int result = await client.InvokeAsync(c => c.SlowOperationAsync(2000, operationId), invokeCts.Token);
|
||||
Console.WriteLine($"SlowOperationAsync = {result}");
|
||||
Log($"SlowOperationAsync = {result}");
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
await serverProcess.WaitForExitAsync();
|
||||
serverProcess.Dispose();
|
||||
client.Dispose();
|
||||
|
||||
Console.WriteLine("Restarting server...");
|
||||
Log("Restarting server...");
|
||||
await StartServerAndConnectAsync(pipeName);
|
||||
Console.WriteLine("Reconnected to server.");
|
||||
Log("Reconnected to server.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,7 +146,7 @@ async Task StartServerAndConnectAsync(string pipeName)
|
||||
process.Exited += (_, _) =>
|
||||
{
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user