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>
|
/// </summary>
|
||||||
public abstract class LogHandler
|
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>
|
/// <summary>
|
||||||
/// Error and information about logging should be reported here.
|
/// Error and information about logging should be reported here.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable.
|
|
||||||
public IEventLogger EventLogger { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Convenience method to copy stuff from StreamReader to StreamWriter
|
/// Convenience method to copy stuff from StreamReader to StreamWriter
|
||||||
|
@ -72,39 +82,26 @@ namespace WinSW
|
||||||
{
|
{
|
||||||
protected string BaseLogFileName { get; private set; }
|
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 OutFilePattern { get; private set; }
|
||||||
|
|
||||||
protected string ErrFilePattern { get; private set; }
|
protected string ErrFilePattern { get; private set; }
|
||||||
|
|
||||||
protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern)
|
protected AbstractFileLogAppender(string logDirectory, string baseName, bool outFileDisabled, bool errFileDisabled, string outFilePattern, string errFilePattern)
|
||||||
|
: base(outFileDisabled, errFileDisabled)
|
||||||
{
|
{
|
||||||
this.BaseLogFileName = Path.Combine(logDirectory, baseName);
|
this.BaseLogFileName = Path.Combine(logDirectory, baseName);
|
||||||
this.OutFileDisabled = outFileDisabled;
|
|
||||||
this.OutFilePattern = outFilePattern;
|
this.OutFilePattern = outFilePattern;
|
||||||
this.ErrFileDisabled = errFileDisabled;
|
|
||||||
this.ErrFilePattern = errFilePattern;
|
this.ErrFilePattern = errFilePattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Log(StreamReader outputReader, StreamReader errorReader)
|
public override void Log(StreamReader outputReader, StreamReader errorReader)
|
||||||
{
|
{
|
||||||
if (this.OutFileDisabled)
|
if (!this.OutFileDisabled)
|
||||||
{
|
|
||||||
outputReader.Dispose();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
this.LogOutput(outputReader);
|
this.LogOutput(outputReader);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.ErrFileDisabled)
|
if (!this.ErrFileDisabled)
|
||||||
{
|
|
||||||
errorReader.Dispose();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
this.LogError(errorReader);
|
this.LogError(errorReader);
|
||||||
}
|
}
|
||||||
|
@ -165,10 +162,13 @@ namespace WinSW
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class IgnoreLogAppender : LogHandler
|
public class IgnoreLogAppender : LogHandler
|
||||||
{
|
{
|
||||||
|
public IgnoreLogAppender()
|
||||||
|
: base(true, true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public override void Log(StreamReader outputReader, StreamReader errorReader)
|
public override void Log(StreamReader outputReader, StreamReader errorReader)
|
||||||
{
|
{
|
||||||
outputReader.Dispose();
|
|
||||||
errorReader.Dispose();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.IO;
|
||||||
using System.Management;
|
using System.Management;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using log4net;
|
using log4net;
|
||||||
|
@ -155,8 +156,8 @@ namespace WinSW.Util
|
||||||
ps.CreateNoWindow = hideWindow;
|
ps.CreateNoWindow = hideWindow;
|
||||||
ps.UseShellExecute = false;
|
ps.UseShellExecute = false;
|
||||||
ps.RedirectStandardInput = redirectStdin;
|
ps.RedirectStandardInput = redirectStdin;
|
||||||
ps.RedirectStandardOutput = logHandler != null;
|
ps.RedirectStandardOutput = logHandler?.OutFileDisabled == false;
|
||||||
ps.RedirectStandardError = logHandler != null;
|
ps.RedirectStandardError = logHandler?.ErrFileDisabled == false;
|
||||||
|
|
||||||
if (envVars != null)
|
if (envVars != null)
|
||||||
{
|
{
|
||||||
|
@ -193,7 +194,9 @@ namespace WinSW.Util
|
||||||
if (logHandler != null)
|
if (logHandler != null)
|
||||||
{
|
{
|
||||||
Logger.Debug("Forwarding logs of the process " + processToStart + " to " + logHandler);
|
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
|
// monitor the completion of the process
|
||||||
|
|
Loading…
Reference in New Issue