Switch to native .NET logging APIs

This commit is contained in:
SteveSandersonMS
2016-07-18 15:56:45 +01:00
parent 27ffa72e0d
commit f4efcacd40
8 changed files with 46 additions and 54 deletions

View File

@@ -4,7 +4,7 @@ using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.Util;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
@@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private bool _disposed;
private int _portNumber;
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, INodeInstanceOutputLogger nodeInstanceOutputLogger = null)
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, ILogger nodeInstanceOutputLogger, int port = 0)
: base(
EmbeddedResourceReader.Read(
typeof(HttpNodeInstance),

View File

@@ -3,7 +3,7 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.Util;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.NodeServices.HostingModels
{
@@ -19,6 +19,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
/// <seealso cref="Microsoft.AspNetCore.NodeServices.HostingModels.INodeInstance" />
public abstract class OutOfProcessNodeInstance : INodeInstance
{
protected readonly ILogger OutputLogger;
private const string ConnectionEstablishedMessage = "[Microsoft.AspNetCore.NodeServices:Listening]";
private readonly TaskCompletionSource<object> _connectionIsReadySource = new TaskCompletionSource<object>();
private bool _disposed;
@@ -27,18 +28,20 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private readonly Process _nodeProcess;
private bool _nodeProcessNeedsRestart;
private readonly string[] _watchFileExtensions;
private INodeInstanceOutputLogger _nodeInstanceOutputLogger;
public OutOfProcessNodeInstance(
string entryPointScript,
string projectPath,
string[] watchFileExtensions,
string commandLineArguments,
INodeInstanceOutputLogger nodeOutputLogger)
ILogger nodeOutputLogger)
{
_nodeInstanceOutputLogger = nodeOutputLogger;
if(_nodeInstanceOutputLogger == null)
_nodeInstanceOutputLogger = new ConsoleNodeInstanceOutputLogger();
if (nodeOutputLogger == null)
{
throw new ArgumentNullException(nameof(nodeOutputLogger));
}
OutputLogger = nodeOutputLogger;
_entryPointScript = new StringAsTempFile(entryPointScript);
var startInfo = PrepareNodeProcessStartInfo(_entryPointScript.FileName, projectPath, commandLineArguments);
@@ -112,12 +115,12 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
protected virtual void OnOutputDataReceived(string outputData)
{
_nodeInstanceOutputLogger.LogOutputData(outputData);
OutputLogger.LogInformation(outputData);
}
protected virtual void OnErrorDataReceived(string errorData)
{
_nodeInstanceOutputLogger.LogErrorData(errorData);
OutputLogger.LogError(errorData);
}
protected virtual void Dispose(bool disposing)
@@ -255,8 +258,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private void RestartDueToFileChange(string fullPath)
{
// TODO: Use proper logger
Console.WriteLine($"Node will restart because file changed: {fullPath}");
OutputLogger.LogInformation($"Node will restart because file changed: {fullPath}");
_nodeProcessNeedsRestart = true;

View File

@@ -5,7 +5,7 @@ using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.NodeServices.HostingModels.PhysicalConnections;
using Microsoft.AspNetCore.NodeServices.HostingModels.VirtualConnections;
using Microsoft.AspNetCore.NodeServices.Util;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
@@ -37,7 +37,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
private string _socketAddress;
private VirtualConnectionClient _virtualConnectionClient;
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, INodeInstanceOutputLogger nodeInstanceOutputLogger = null) : base(
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, ILogger nodeInstanceOutputLogger) : base(
EmbeddedResourceReader.Read(
typeof(SocketNodeInstance),
"/Content/Node/entrypoint-socket.js"),
@@ -125,9 +125,7 @@ namespace Microsoft.AspNetCore.NodeServices.HostingModels
// failure, this Node instance is no longer usable and should be discarded.
_connectionHasFailed = true;
// TODO: Log the exception properly. Need to change the chain of calls up to this point to supply
// an ILogger or IServiceProvider etc.
Console.WriteLine(ex.Message);
OutputLogger.LogError(0, ex, ex.Message);
};
}
}