Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit 8d6b31c

Browse files
committed
Adding support for capturing the output of a node instance for custom logging implementations.
1 parent 58bf117 commit 8d6b31c

File tree

7 files changed

+52
-9
lines changed

7 files changed

+52
-9
lines changed

src/Microsoft.AspNetCore.NodeServices/Configuration/Configuration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ private static INodeInstance CreateNodeInstance(NodeServicesOptions options)
4646
switch (options.HostingModel)
4747
{
4848
case NodeHostingModel.Http:
49-
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, /* port */ 0);
49+
return new HttpNodeInstance(options.ProjectPath, options.WatchFileExtensions, /* port */ 0, options.NodeInstanceOutputLogger);
5050
case NodeHostingModel.Socket:
5151
var pipeName = "pni-" + Guid.NewGuid().ToString("D"); // Arbitrary non-clashing string
52-
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName);
52+
return new SocketNodeInstance(options.ProjectPath, options.WatchFileExtensions, pipeName, options.NodeInstanceOutputLogger);
5353
default:
5454
throw new ArgumentException("Unknown hosting model: " + options.HostingModel);
5555
}

src/Microsoft.AspNetCore.NodeServices/Configuration/NodeServicesOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Microsoft.AspNetCore.NodeServices.HostingModels;
3+
using Microsoft.AspNetCore.NodeServices.Util;
34

45
namespace Microsoft.AspNetCore.NodeServices
56
{
@@ -19,5 +20,6 @@ public NodeServicesOptions()
1920
public Func<INodeInstance> NodeInstanceFactory { get; set; }
2021
public string ProjectPath { get; set; }
2122
public string[] WatchFileExtensions { get; set; }
23+
public INodeInstanceOutputLogger NodeInstanceOutputLogger { get; set; }
2224
}
2325
}

src/Microsoft.AspNetCore.NodeServices/HostingModels/HttpNodeInstance.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Text.RegularExpressions;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.NodeServices.Util;
78
using Newtonsoft.Json;
89
using Newtonsoft.Json.Serialization;
910

@@ -32,14 +33,15 @@ internal class HttpNodeInstance : OutOfProcessNodeInstance
3233
private bool _disposed;
3334
private int _portNumber;
3435

35-
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0)
36+
public HttpNodeInstance(string projectPath, string[] watchFileExtensions, int port = 0, INodeInstanceOutputLogger nodeInstanceOutputLogger = null)
3637
: base(
3738
EmbeddedResourceReader.Read(
3839
typeof(HttpNodeInstance),
3940
"/Content/Node/entrypoint-http.js"),
4041
projectPath,
4142
watchFileExtensions,
42-
MakeCommandLineOptions(port))
43+
MakeCommandLineOptions(port),
44+
nodeInstanceOutputLogger)
4345
{
4446
_client = new HttpClient();
4547
}

src/Microsoft.AspNetCore.NodeServices/HostingModels/OutOfProcessNodeInstance.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.NodeServices.Util;
67

78
namespace Microsoft.AspNetCore.NodeServices.HostingModels
89
{
@@ -26,13 +27,18 @@ public abstract class OutOfProcessNodeInstance : INodeInstance
2627
private readonly Process _nodeProcess;
2728
private bool _nodeProcessNeedsRestart;
2829
private readonly string[] _watchFileExtensions;
30+
private INodeInstanceOutputLogger _nodeInstanceOutputLogger;
2931

3032
public OutOfProcessNodeInstance(
3133
string entryPointScript,
3234
string projectPath,
3335
string[] watchFileExtensions,
34-
string commandLineArguments)
36+
string commandLineArguments,
37+
INodeInstanceOutputLogger nodeOutputLogger)
3538
{
39+
_nodeInstanceOutputLogger = nodeOutputLogger;
40+
if(_nodeInstanceOutputLogger == null)
41+
_nodeInstanceOutputLogger = new ConsoleNodeInstanceOutputLogger();
3642
_entryPointScript = new StringAsTempFile(entryPointScript);
3743
_nodeProcess = LaunchNodeProcess(_entryPointScript.FileName, projectPath, commandLineArguments);
3844
_watchFileExtensions = watchFileExtensions;
@@ -73,12 +79,12 @@ public void Dispose()
7379

7480
protected virtual void OnOutputDataReceived(string outputData)
7581
{
76-
Console.WriteLine("[Node] " + outputData);
82+
_nodeInstanceOutputLogger.LogOutputData(outputData);
7783
}
7884

7985
protected virtual void OnErrorDataReceived(string errorData)
8086
{
81-
Console.WriteLine("[Node] " + errorData);
87+
_nodeInstanceOutputLogger.LogErrorData(errorData);
8288
}
8389

8490
protected virtual void Dispose(bool disposing)

src/Microsoft.AspNetCore.NodeServices/HostingModels/SocketNodeInstance.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.AspNetCore.NodeServices.HostingModels.PhysicalConnections;
77
using Microsoft.AspNetCore.NodeServices.HostingModels.VirtualConnections;
8+
using Microsoft.AspNetCore.NodeServices.Util;
89
using Newtonsoft.Json;
910
using Newtonsoft.Json.Serialization;
1011

@@ -36,13 +37,14 @@ internal class SocketNodeInstance : OutOfProcessNodeInstance
3637
private string _socketAddress;
3738
private VirtualConnectionClient _virtualConnectionClient;
3839

39-
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress): base(
40+
public SocketNodeInstance(string projectPath, string[] watchFileExtensions, string socketAddress, INodeInstanceOutputLogger nodeInstanceOutputLogger = null) : base(
4041
EmbeddedResourceReader.Read(
4142
typeof(SocketNodeInstance),
4243
"/Content/Node/entrypoint-socket.js"),
4344
projectPath,
4445
watchFileExtensions,
45-
MakeNewCommandLineOptions(socketAddress))
46+
MakeNewCommandLineOptions(socketAddress),
47+
nodeInstanceOutputLogger)
4648
{
4749
_socketAddress = socketAddress;
4850
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace Microsoft.AspNetCore.NodeServices.Util
4+
{
5+
public class ConsoleNodeInstanceOutputLogger : INodeInstanceOutputLogger
6+
{
7+
public void LogOutputData(string outputData)
8+
{
9+
Console.WriteLine("[Node] " + outputData);
10+
}
11+
12+
public void LogErrorData(string errorData)
13+
{
14+
Console.WriteLine("[Node] " + errorData);
15+
}
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Microsoft.AspNetCore.NodeServices.Util
7+
{
8+
public interface INodeInstanceOutputLogger
9+
{
10+
void LogOutputData(string outputData);
11+
12+
void LogErrorData(string errorData);
13+
}
14+
}

0 commit comments

Comments
 (0)