public void MultipleProcessesCanWriteToTheLog() { var logFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); TimestampedFileLogger logger = new(logFile); // Log requests operates using PipeTransmissionMode.Message NamedPipeServerStream np1 = CreateServerPipe("np1"); PipeStreamSetupLogger pssl1 = new PipeStreamSetupLogger(np1, "np1"); NamedPipeServerStream np2 = CreateServerPipe("np2"); PipeStreamSetupLogger pssl2 = new PipeStreamSetupLogger(np2, "np2"); logger.AddNamedPipe("np1"); logger.AddNamedPipe("np2"); logger.LogMessage("Foo"); var t1 = Task.Run(() => { pssl1.Connect(); pssl1.LogMessage("Hello from np1"); }); var t2 = Task.Run(() => { pssl2.Connect(); pssl2.LogMessage("Hello from np2"); }); // Give the other threads time to connect to the logging thread. Task.WaitAll(t1, t2); logger.Dispose(); string logContent = File.ReadAllText(logFile); Assert.Contains("Hello from np1", logContent); Assert.Contains("Hello from np2", logContent); }
private void LogTask(string pipeName) { using NamedPipeServerStream serverPipe = CreateServerPipe(pipeName); PipeStreamSetupLogger logger = new PipeStreamSetupLogger(serverPipe, pipeName); logger.Connect(); for (int i = 0; i < 10; i++) { logger.LogMessage($"Hello from {pipeName} ({i})."); } }
public NetSdkMsiInstallerServer(InstallElevationContextBase elevationContext, PipeStreamSetupLogger logger) : base(elevationContext, logger) { // Establish a connection with the install client and logger. We're relying on tasks to handle // this, otherwise, the ordering needs to be lined up with how the client configures // the underlying pipe streams to avoid deadlock. Task dispatchTask = new Task(() => Dispatcher.Connect()); Task loggerTask = new Task(() => logger.Connect()); dispatchTask.Start(); loggerTask.Start(); Task.WaitAll(dispatchTask, loggerTask); }