mirror of https://github.com/winsw/winsw
Merge pull request #694 from winsw/stdouterr
Leave STDOUT/STDERR open if not redirectedpull/716/head v2.10.3
commit
5a545ba54b
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue