Leave STDOUT/STDERR open if not redirected

pull/694/head
NextTurn 2020-10-07 00:00:00 +08:00
parent 272c474b1b
commit d6bcbd6e1c
No known key found for this signature in database
GPG Key ID: 17A0D50ADDE1A0C4
2 changed files with 27 additions and 24 deletions

View File

@ -25,14 +25,24 @@ namespace WinSW
/// </summary>
public abstract class LogHandler
{
public abstract void Log(StreamReader outputReader, StreamReader errorReader);
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
protected LogHandler(bool outFileDisabled, bool errFileDisabled)
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
{
this.OutFileDisabled = outFileDisabled;
this.ErrFileDisabled = errFileDisabled;
}
/// <summary>
/// Error and information about logging should be reported here.
/// </summary>
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public IEventLogger EventLogger { get; set; }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
public bool OutFileDisabled { get; }
public bool ErrFileDisabled { get; }
public abstract void Log(StreamReader outputReader, StreamReader errorReader);
/// <summary>
/// Convenience method to copy stuff from StreamReader to StreamWriter
@ -72,39 +82,26 @@ namespace WinSW
{
protected string BaseLogFileName { get; private set; }
protected bool OutFileDisabled { get; private set; }
protected bool ErrFileDisabled { get; private set; }
protected string OutFilePattern { get; private set; }
protected string ErrFilePattern { get; private set; }
protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern)
: base(outFileDisabled, errFileDisabled)
{
this.BaseLogFileName = Path.Combine(logDirectory, baseName);
this.OutFileDisabled = outFileDisabled;
this.OutFilePattern = outFilePattern;
this.ErrFileDisabled = errFileDisabled;
this.ErrFilePattern = errFilePattern;
}
public override void Log(StreamReader outputReader, StreamReader errorReader)
{
if (this.OutFileDisabled)
{
outputReader.Dispose();
}
else
if (!this.OutFileDisabled)
{
this.LogOutput(outputReader);
}
if (this.ErrFileDisabled)
{
errorReader.Dispose();
}
else
if (!this.ErrFileDisabled)
{
this.LogError(errorReader);
}
@ -165,10 +162,13 @@ namespace WinSW
/// </summary>
public class IgnoreLogAppender : LogHandler
{
public IgnoreLogAppender()
: base(true, true)
{
}
public override void Log(StreamReader outputReader, StreamReader errorReader)
{
outputReader.Dispose();
errorReader.Dispose();
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Threading;
using log4net;
@ -155,8 +156,8 @@ namespace WinSW.Util
ps.CreateNoWindow = hideWindow;
ps.UseShellExecute = false;
ps.RedirectStandardInput = redirectStdin;
ps.RedirectStandardOutput = logHandler != null;
ps.RedirectStandardError = logHandler != null;
ps.RedirectStandardOutput = logHandler?.OutFileDisabled == false;
ps.RedirectStandardError = logHandler?.ErrFileDisabled == false;
if (envVars != null)
{
@ -193,7 +194,9 @@ namespace WinSW.Util
if (logHandler != null)
{
Logger.Debug("Forwarding logs of the process " + processToStart + " to " + logHandler);
logHandler.Log(processToStart.StandardOutput, processToStart.StandardError);
logHandler.Log(
ps.RedirectStandardOutput ? processToStart.StandardOutput : StreamReader.Null,
ps.RedirectStandardError ? processToStart.StandardError : StreamReader.Null);
}
// monitor the completion of the process