Skip to content

Commit

Permalink
Ensure basic console logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nxtn committed Nov 25, 2020
1 parent 12525cc commit 2ab7908
Show file tree
Hide file tree
Showing 34 changed files with 279 additions and 293 deletions.
60 changes: 30 additions & 30 deletions src/WinSW.Core/Configuration/XmlServiceConfig.cs
Expand Up @@ -102,7 +102,7 @@ public static XmlServiceConfig FromXml(string xml)

private static int SingleIntElement(XmlNode parent, string tagName, int defaultValue)
{
XmlNode? e = parent.SelectSingleNode(tagName);
var e = parent.SelectSingleNode(tagName);

return e is null ? defaultValue : int.Parse(e.InnerText, NumberFormatInfo.InvariantInfo);
}
Expand All @@ -128,13 +128,13 @@ private string SingleElement(string tagName)

private string? SingleElementOrNull(string tagName)
{
XmlNode? n = this.root.SelectSingleNode(tagName);
var n = this.root.SelectSingleNode(tagName);
return n is null ? null : Environment.ExpandEnvironmentVariables(n.InnerText);
}

private bool SingleBoolElementOrDefault(string tagName, bool defaultValue)
{
XmlNode? e = this.root.SelectSingleNode(tagName);
var e = this.root.SelectSingleNode(tagName);

return e is null ? defaultValue : bool.Parse(e.InnerText);
}
Expand Down Expand Up @@ -208,14 +208,14 @@ public List<string> ExtensionIds
{
get
{
XmlNode? argumentNode = this.ExtensionsConfiguration;
XmlNodeList? extensions = argumentNode?.SelectNodes("extension");
var argumentNode = this.ExtensionsConfiguration;
var extensions = argumentNode?.SelectNodes("extension");
if (extensions is null)
{
return new List<string>(0);
}

List<string> result = new List<string>(extensions.Count);
var result = new List<string>(extensions.Count);
for (int i = 0; i < extensions.Count; i++)
{
result.Add(XmlHelper.SingleAttribute<string>((XmlElement)extensions[i]!, "id"));
Expand All @@ -239,7 +239,7 @@ public override string LogMode
string? mode = null;

// first, backward compatibility with older configuration
XmlElement? e = (XmlElement?)this.root.SelectSingleNode("logmode");
var e = (XmlElement?)this.root.SelectSingleNode("logmode");
if (e != null)
{
mode = e.InnerText;
Expand Down Expand Up @@ -272,7 +272,7 @@ public LogHandler LogHandler
{
get
{
XmlElement? e = (XmlElement?)this.root.SelectSingleNode("logmode");
var e = (XmlElement?)this.root.SelectSingleNode("logmode");

// this is more modern way, to support nested elements as configuration
e ??= (XmlElement?)this.root.SelectSingleNode("log")!; // WARNING: NRE
Expand All @@ -293,13 +293,13 @@ public LogHandler LogHandler
return new RollingLogAppender(this.LogDirectory, this.LogName, this.OutFileDisabled, this.ErrFileDisabled, this.OutFilePattern, this.ErrFilePattern);

case "roll-by-time":
XmlNode? patternNode = e.SelectSingleNode("pattern");
var patternNode = e.SelectSingleNode("pattern");
if (patternNode is null)
{
throw new InvalidDataException("Time Based rolling policy is specified but no pattern can be found in configuration XML.");
}

var pattern = patternNode.InnerText;
string? pattern = patternNode.InnerText;
int period = SingleIntElement(e, "period", 1);
return new TimeBasedRollingLogAppender(this.LogDirectory, this.LogName, this.OutFileDisabled, this.ErrFileDisabled, this.OutFilePattern, this.ErrFilePattern, pattern, period);

Expand All @@ -313,26 +313,26 @@ public LogHandler LogHandler

case "roll-by-size-time":
sizeThreshold = SingleIntElement(e, "sizeThreshold", 10 * 1024) * RollingSizeTimeLogAppender.BytesPerKB;
XmlNode? filePatternNode = e.SelectSingleNode("pattern");
var filePatternNode = e.SelectSingleNode("pattern");
if (filePatternNode is null)
{
throw new InvalidDataException("Roll-Size-Time Based rolling policy is specified but no pattern can be found in configuration XML.");
}

XmlNode? autoRollAtTimeNode = e.SelectSingleNode("autoRollAtTime");
var autoRollAtTimeNode = e.SelectSingleNode("autoRollAtTime");
TimeSpan? autoRollAtTime = null;
if (autoRollAtTimeNode != null)
{
// validate it
if (!TimeSpan.TryParse(autoRollAtTimeNode.InnerText, out TimeSpan autoRollAtTimeValue))
if (!TimeSpan.TryParse(autoRollAtTimeNode.InnerText, out var autoRollAtTimeValue))
{
throw new InvalidDataException("Roll-Size-Time Based rolling policy is specified but autoRollAtTime does not match the TimeSpan format HH:mm:ss found in configuration XML.");
}

autoRollAtTime = autoRollAtTimeValue;
}

XmlNode? zipolderthannumdaysNode = e.SelectSingleNode("zipOlderThanNumDays");
var zipolderthannumdaysNode = e.SelectSingleNode("zipOlderThanNumDays");
int? zipolderthannumdays = null;
if (zipolderthannumdaysNode != null)
{
Expand All @@ -345,7 +345,7 @@ public LogHandler LogHandler
zipolderthannumdays = zipolderthannumdaysValue;
}

XmlNode? zipdateformatNode = e.SelectSingleNode("zipDateFormat");
var zipdateformatNode = e.SelectSingleNode("zipDateFormat");
string zipdateformat = zipdateformatNode is null ? "yyyyMM" : zipdateformatNode.InnerText;

return new RollingSizeTimeLogAppender(this.LogDirectory, this.LogName, this.OutFileDisabled, this.ErrFileDisabled, this.OutFilePattern, this.ErrFilePattern, sizeThreshold, filePatternNode.InnerText, autoRollAtTime, zipolderthannumdays, zipdateformat);
Expand All @@ -363,7 +363,7 @@ public override string[] ServiceDependencies
{
get
{
XmlNodeList? nodeList = this.root.SelectNodes("depend");
var nodeList = this.root.SelectNodes("depend");
if (nodeList is null)
{
return base.ServiceDependencies;
Expand Down Expand Up @@ -404,7 +404,7 @@ public override ServiceStartMode StartMode
}
catch (ArgumentException e)
{
StringBuilder builder = new StringBuilder();
var builder = new StringBuilder();
builder.AppendLine("Start mode in XML must be one of the following:");
foreach (string sm in Enum.GetNames(typeof(ServiceStartMode)))
{
Expand Down Expand Up @@ -457,13 +457,13 @@ public override List<Download> Downloads
{
get
{
XmlNodeList? nodeList = this.root.SelectNodes("download");
var nodeList = this.root.SelectNodes("download");
if (nodeList is null)
{
return base.Downloads;
}

List<Download> result = new List<Download>(nodeList.Count);
var result = new List<Download>(nodeList.Count);
for (int i = 0; i < nodeList.Count; i++)
{
if (nodeList[i] is XmlElement element)
Expand All @@ -480,25 +480,25 @@ public override SC_ACTION[] FailureActions
{
get
{
XmlNodeList? childNodes = this.root.SelectNodes("onfailure");
var childNodes = this.root.SelectNodes("onfailure");
if (childNodes is null)
{
return Array.Empty<SC_ACTION>();
}

SC_ACTION[] result = new SC_ACTION[childNodes.Count];
var result = new SC_ACTION[childNodes.Count];
for (int i = 0; i < childNodes.Count; i++)
{
XmlNode node = childNodes[i]!;
var node = childNodes[i]!;
string action = node.Attributes!["action"]?.Value ?? throw new InvalidDataException("'action' is missing");
SC_ACTION_TYPE type = action switch
var type = action switch
{
"restart" => SC_ACTION_TYPE.SC_ACTION_RESTART,
"none" => SC_ACTION_TYPE.SC_ACTION_NONE,
"reboot" => SC_ACTION_TYPE.SC_ACTION_REBOOT,
_ => throw new Exception("Invalid failure action: " + action)
};
XmlAttribute? delay = node.Attributes["delay"];
var delay = node.Attributes["delay"];
result[i] = new SC_ACTION(type, delay != null ? ParseTimeSpan(delay.Value) : TimeSpan.Zero);
}

Expand All @@ -510,11 +510,11 @@ public override SC_ACTION[] FailureActions

protected string? GetServiceAccountPart(string subNodeName)
{
XmlNode? node = this.root.SelectSingleNode("serviceaccount");
var node = this.root.SelectSingleNode("serviceaccount");

if (node != null)
{
XmlNode? subNode = node.SelectSingleNode(subNodeName);
var subNode = node.SelectSingleNode(subNodeName);
if (subNode != null)
{
return subNode.InnerText;
Expand Down Expand Up @@ -583,11 +583,11 @@ public override ProcessPriorityClass Priority

private Dictionary<string, string> LoadEnvironmentVariables()
{
XmlNodeList nodeList = this.root.SelectNodes("env")!;
Dictionary<string, string> environment = new Dictionary<string, string>(nodeList.Count);
var nodeList = this.root.SelectNodes("env")!;
var environment = new Dictionary<string, string>(nodeList.Count);
for (int i = 0; i < nodeList.Count; i++)
{
XmlNode node = nodeList[i]!;
var node = nodeList[i]!;
string key = node.Attributes!["name"]?.Value ?? throw new InvalidDataException("'name' is missing");
string value = Environment.ExpandEnvironmentVariables(node.Attributes["value"]?.Value ?? throw new InvalidDataException("'value' is missing"));
environment[key] = value;
Expand All @@ -600,7 +600,7 @@ public override ProcessPriorityClass Priority

private ProcessCommand GetProcessCommand(string name)
{
XmlNode? node = this.root.SelectSingleNode(name);
var node = this.root.SelectSingleNode(name);
return node is null ? default : new ProcessCommand
{
Executable = GetInnerText(Names.Executable),
Expand Down
10 changes: 5 additions & 5 deletions src/WinSW.Core/Download.cs
Expand Up @@ -121,10 +121,10 @@ private static void SetBasicAuthHeader(WebRequest request, string username, stri
/// </exception>
public async Task PerformAsync()
{
WebRequest request = WebRequest.Create(this.From);
var request = WebRequest.Create(this.From);
if (!string.IsNullOrEmpty(this.Proxy))
{
CustomProxyInformation proxyInformation = new CustomProxyInformation(this.Proxy!);
var proxyInformation = new CustomProxyInformation(this.Proxy!);
if (proxyInformation.Credentials != null)
{
request.Proxy = new WebProxy(proxyInformation.ServerAddress, false, null, proxyInformation.Credentials);
Expand Down Expand Up @@ -166,9 +166,9 @@ public async Task PerformAsync()
string tmpFilePath = this.To + ".tmp";
try
{
using (WebResponse response = await request.GetResponseAsync().ConfigureAwait(false))
using (Stream responseStream = response.GetResponseStream())
using (FileStream tmpStream = new FileStream(tmpFilePath, FileMode.Create))
using (var response = await request.GetResponseAsync().ConfigureAwait(false))
using (var responseStream = response.GetResponseStream())
using (var tmpStream = new FileStream(tmpFilePath, FileMode.Create))
{
if (supportsIfModifiedSince)
{
Expand Down
8 changes: 4 additions & 4 deletions src/WinSW.Core/Extensions/WinSWExtensionManager.cs
Expand Up @@ -25,7 +25,7 @@ private static IWinSWExtension CreateExtensionInstance(string id, string classNa

try
{
Type? t = Type.GetType(className);
var t = Type.GetType(className);
if (t is null)
{
throw new ExtensionException(id, "Class " + className + " does not exist");
Expand Down Expand Up @@ -154,8 +154,8 @@ private void LoadExtension(string id)
throw new ExtensionException(id, "Extension has been already loaded");
}

XmlNode? extensionsConfig = this.ServiceConfig.ExtensionsConfiguration;
XmlElement? configNode = extensionsConfig is null ? null : extensionsConfig.SelectSingleNode("extension[@id='" + id + "'][1]") as XmlElement;
var extensionsConfig = this.ServiceConfig.ExtensionsConfiguration;
var configNode = extensionsConfig is null ? null : extensionsConfig.SelectSingleNode("extension[@id='" + id + "'][1]") as XmlElement;
if (configNode is null)
{
throw new ExtensionException(id, "Cannot get the configuration entry");
Expand All @@ -164,7 +164,7 @@ private void LoadExtension(string id)
var descriptor = WinSWExtensionDescriptor.FromXml(configNode);
if (descriptor.Enabled)
{
IWinSWExtension extension = CreateExtensionInstance(descriptor.Id, descriptor.ClassName);
var extension = CreateExtensionInstance(descriptor.Id, descriptor.ClassName);
extension.Descriptor = descriptor;
try
{
Expand Down

0 comments on commit 2ab7908

Please sign in to comment.