Remove references to Microsoft.Extensions.Logging

This commit is contained in:
Pranav K 2016-04-28 10:01:30 -07:00
parent 3a2a789b1c
commit 54cfe01869
11 changed files with 97 additions and 155 deletions

View file

@ -12,15 +12,19 @@ namespace Microsoft.Extensions.Testing.Abstractions
public class SourceInformationProvider : ISourceInformationProvider public class SourceInformationProvider : ISourceInformationProvider
{ {
private readonly string _pdbPath; private readonly string _pdbPath;
private readonly ILogger _logger;
private readonly IPdbReader _pdbReader; private readonly IPdbReader _pdbReader;
public SourceInformationProvider(string pdbPath, ILogger logger) : public SourceInformationProvider(string pdbPath) :
this(pdbPath, logger, new PdbReaderFactory()) this(pdbPath, new PdbReaderFactory())
{ {
} }
public SourceInformationProvider(string pdbPath, ILogger logger, IPdbReaderFactory pdbReaderFactory) public SourceInformationProvider(string pdbPath, ILogger logger) :
this(pdbPath, new PdbReaderFactory())
{
}
public SourceInformationProvider(string pdbPath, IPdbReaderFactory pdbReaderFactory)
{ {
if (string.IsNullOrWhiteSpace(pdbPath) || !File.Exists(pdbPath)) if (string.IsNullOrWhiteSpace(pdbPath) || !File.Exists(pdbPath))
{ {
@ -28,7 +32,6 @@ namespace Microsoft.Extensions.Testing.Abstractions
} }
_pdbPath = pdbPath; _pdbPath = pdbPath;
_logger = logger;
_pdbReader = pdbReaderFactory.Create(pdbPath); _pdbReader = pdbReaderFactory.Create(pdbPath);
} }
@ -57,7 +60,7 @@ namespace Microsoft.Extensions.Testing.Abstractions
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger?.LogWarning("Failed to access source information in symbol.", ex); Console.WriteLine("Failed to access source information in symbol.", ex);
return null; return null;
} }
} }

View file

@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Extensions.Logging;
namespace Microsoft.Extensions.Testing.Abstractions namespace Microsoft.Extensions.Testing.Abstractions
{ {
public abstract class TestHostServices public abstract class TestHostServices
@ -12,7 +10,5 @@ namespace Microsoft.Extensions.Testing.Abstractions
public abstract ITestExecutionSink TestExecutionSink { get; } public abstract ITestExecutionSink TestExecutionSink { get; }
public abstract ISourceInformationProvider SourceInformationProvider { get; } public abstract ISourceInformationProvider SourceInformationProvider { get; }
public abstract ILoggerFactory LoggerFactory { get; }
} }
} }

View file

@ -4,7 +4,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.DotNet.ProjectModel.Server.Models; using Microsoft.DotNet.ProjectModel.Server.Models;
using Microsoft.Extensions.Logging;
namespace Microsoft.DotNet.ProjectModel.Server namespace Microsoft.DotNet.ProjectModel.Server
{ {
@ -18,13 +17,12 @@ namespace Microsoft.DotNet.ProjectModel.Server
string hostName, string hostName,
ProtocolManager protocolManager, ProtocolManager protocolManager,
WorkspaceContext workspaceContext, WorkspaceContext workspaceContext,
IDictionary<int, ProjectManager> projects, IDictionary<int, ProjectManager> projects)
ILoggerFactory loggerFactory)
{ {
_hostName = hostName; _hostName = hostName;
_projects = projects; _projects = projects;
_queue = new ProcessingQueue(new NetworkStream(acceptedSocket), loggerFactory); _queue = new ProcessingQueue(new NetworkStream(acceptedSocket));
_queue.OnReceive += message => _queue.OnReceive += message =>
{ {
if (protocolManager.IsProtocolNegotiation(message)) if (protocolManager.IsProtocolNegotiation(message))
@ -39,7 +37,6 @@ namespace Microsoft.DotNet.ProjectModel.Server
if (!_projects.TryGetValue(message.ContextId, out projectManager)) if (!_projects.TryGetValue(message.ContextId, out projectManager))
{ {
projectManager = new ProjectManager(message.ContextId, projectManager = new ProjectManager(message.ContextId,
loggerFactory,
workspaceContext, workspaceContext,
protocolManager); protocolManager);

View file

@ -4,8 +4,8 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel.Server.Models; using Microsoft.DotNet.ProjectModel.Server.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Microsoft.DotNet.ProjectModel.Server namespace Microsoft.DotNet.ProjectModel.Server
@ -14,20 +14,18 @@ namespace Microsoft.DotNet.ProjectModel.Server
{ {
private readonly BinaryReader _reader; private readonly BinaryReader _reader;
private readonly BinaryWriter _writer; private readonly BinaryWriter _writer;
private readonly ILogger _log;
public ProcessingQueue(Stream stream, ILoggerFactory loggerFactory) public ProcessingQueue(Stream stream)
{ {
_reader = new BinaryReader(stream); _reader = new BinaryReader(stream);
_writer = new BinaryWriter(stream); _writer = new BinaryWriter(stream);
_log = loggerFactory.CreateLogger<ProcessingQueue>();
} }
public event Action<Message> OnReceive; public event Action<Message> OnReceive;
public void Start() public void Start()
{ {
_log.LogInformation("Start"); Reporter.Output.WriteLine("Start");
new Thread(ReceiveMessages).Start(); new Thread(ReceiveMessages).Start();
} }
@ -43,11 +41,11 @@ namespace Microsoft.DotNet.ProjectModel.Server
catch (IOException ex) catch (IOException ex)
{ {
// swallow // swallow
_log.LogInformation($"Ignore {nameof(IOException)} during sending message: \"{ex.Message}\"."); Reporter.Output.WriteLine($"Ignore {nameof(IOException)} during sending message: \"{ex.Message}\".");
} }
catch (Exception ex) catch (Exception ex)
{ {
_log.LogWarning($"Unexpected exception {ex.GetType().Name} during sending message: \"{ex.Message}\"."); Reporter.Output.WriteLine($"Unexpected exception {ex.GetType().Name} during sending message: \"{ex.Message}\".");
throw; throw;
} }
} }
@ -59,7 +57,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
{ {
return Send(_writer => return Send(_writer =>
{ {
_log.LogInformation("OnSend ({0})", message); Reporter.Output.WriteLine($"OnSend ({message})");
_writer.Write(JsonConvert.SerializeObject(message)); _writer.Write(JsonConvert.SerializeObject(message));
}); });
} }
@ -73,17 +71,17 @@ namespace Microsoft.DotNet.ProjectModel.Server
var content = _reader.ReadString(); var content = _reader.ReadString();
var message = JsonConvert.DeserializeObject<Message>(content); var message = JsonConvert.DeserializeObject<Message>(content);
_log.LogInformation("OnReceive ({0})", message); Reporter.Output.WriteLine($"OnReceive ({message})");
OnReceive(message); OnReceive(message);
} }
} }
catch (IOException ex) catch (IOException ex)
{ {
_log.LogInformation($"Ignore {nameof(IOException)} during receiving messages: \"{ex}\"."); Reporter.Output.WriteLine($"Ignore {nameof(IOException)} during receiving messages: \"{ex}\".");
} }
catch (Exception ex) catch (Exception ex)
{ {
_log.LogError($"Unexpected exception {ex.GetType().Name} during receiving messages: \"{ex}\"."); Reporter.Error.WriteLine($"Unexpected exception {ex.GetType().Name} during receiving messages: \"{ex}\".");
} }
} }
} }

View file

@ -7,7 +7,7 @@ using System.Diagnostics;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.Extensions.Logging; using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.ProjectModel.Server namespace Microsoft.DotNet.ProjectModel.Server
{ {
@ -16,17 +16,15 @@ namespace Microsoft.DotNet.ProjectModel.Server
private readonly Dictionary<int, ProjectManager> _projects; private readonly Dictionary<int, ProjectManager> _projects;
private readonly WorkspaceContext _workspaceContext; private readonly WorkspaceContext _workspaceContext;
private readonly ProtocolManager _protocolManager; private readonly ProtocolManager _protocolManager;
private readonly ILoggerFactory _loggerFactory;
private readonly string _hostName; private readonly string _hostName;
private readonly int _port; private readonly int _port;
private Socket _listenSocket; private Socket _listenSocket;
public ProjectModelServerCommand(int port, string hostName, ILoggerFactory loggerFactory) public ProjectModelServerCommand(int port, string hostName)
{ {
_port = port; _port = port;
_hostName = hostName; _hostName = hostName;
_loggerFactory = loggerFactory; _protocolManager = new ProtocolManager(maxVersion: 4);
_protocolManager = new ProtocolManager(maxVersion: 4, loggerFactory: _loggerFactory);
_workspaceContext = WorkspaceContext.Create(designTime: true); _workspaceContext = WorkspaceContext.Create(designTime: true);
_projects = new Dictionary<int, ProjectManager>(); _projects = new Dictionary<int, ProjectManager>();
} }
@ -47,19 +45,14 @@ namespace Microsoft.DotNet.ProjectModel.Server
app.OnExecute(() => app.OnExecute(() =>
{ {
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole(verbose.HasValue() ? LogLevel.Debug : LogLevel.Information);
var logger = loggerFactory.CreateLogger<ProjectModelServerCommand>();
try try
{ {
if (!MonitorHostProcess(hostpid, logger)) if (!MonitorHostProcess(hostpid))
{ {
return 1; return 1;
} }
var intPort = CheckPort(port, logger); var intPort = CheckPort(port);
if (intPort == -1) if (intPort == -1)
{ {
return 1; return 1;
@ -67,16 +60,16 @@ namespace Microsoft.DotNet.ProjectModel.Server
if (!hostname.HasValue()) if (!hostname.HasValue())
{ {
logger.LogError($"Option \"{hostname.LongName}\" is missing."); Reporter.Error.WriteLine($"Option \"{hostname.LongName}\" is missing.");
return 1; return 1;
} }
var program = new ProjectModelServerCommand(intPort, hostname.Value(), loggerFactory); var program = new ProjectModelServerCommand(intPort, hostname.Value());
program.OpenChannel(); program.OpenChannel();
} }
catch (Exception ex) catch (Exception ex)
{ {
logger.LogCritical($"Unhandled exception in server main: {ex}"); Reporter.Error.WriteLine($"Unhandled exception in server main: {ex}");
throw; throw;
} }
@ -88,26 +81,23 @@ namespace Microsoft.DotNet.ProjectModel.Server
public void OpenChannel() public void OpenChannel()
{ {
var logger = _loggerFactory.CreateLogger($"OpenChannel");
_listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, _port)); _listenSocket.Bind(new IPEndPoint(IPAddress.Loopback, _port));
_listenSocket.Listen(10); _listenSocket.Listen(10);
logger.LogInformation($"Process ID {Process.GetCurrentProcess().Id}"); Reporter.Output.WriteLine($"Process ID {Process.GetCurrentProcess().Id}");
logger.LogInformation($"Listening on port {_port}"); Reporter.Output.WriteLine($"Listening on port {_port}");
while (true) while (true)
{ {
var acceptSocket = _listenSocket.Accept(); var acceptSocket = _listenSocket.Accept();
logger.LogInformation($"Client accepted {acceptSocket.LocalEndPoint}"); Reporter.Output.WriteLine($"Client accepted {acceptSocket.LocalEndPoint}");
var connection = new ConnectionContext(acceptSocket, var connection = new ConnectionContext(acceptSocket,
_hostName, _hostName,
_protocolManager, _protocolManager,
_workspaceContext, _workspaceContext,
_projects, _projects);
_loggerFactory);
connection.QueueStart(); connection.QueueStart();
} }
@ -121,11 +111,11 @@ namespace Microsoft.DotNet.ProjectModel.Server
} }
} }
private static int CheckPort(CommandOption port, ILogger logger) private static int CheckPort(CommandOption port)
{ {
if (!port.HasValue()) if (!port.HasValue())
{ {
logger.LogError($"Option \"{port.LongName}\" is missing."); Reporter.Error.WriteLine($"Option \"{port.LongName}\" is missing.");
} }
int result; int result;
@ -135,16 +125,16 @@ namespace Microsoft.DotNet.ProjectModel.Server
} }
else else
{ {
logger.LogError($"Option \"{port.LongName}\" is not a valid Int32 value."); Reporter.Error.WriteLine($"Option \"{port.LongName}\" is not a valid Int32 value.");
return -1; return -1;
} }
} }
private static bool MonitorHostProcess(CommandOption host, ILogger logger) private static bool MonitorHostProcess(CommandOption host)
{ {
if (!host.HasValue()) if (!host.HasValue())
{ {
logger.LogError($"Option \"{host.LongName}\" is missing."); Console.Error.WriteLine($"Option \"{host.LongName}\" is missing.");
return false; return false;
} }
@ -158,12 +148,12 @@ namespace Microsoft.DotNet.ProjectModel.Server
Process.GetCurrentProcess().Kill(); Process.GetCurrentProcess().Kill();
}; };
logger.LogDebug($"Server will exit when process {hostPID} exits."); Reporter.Output.WriteLine($"Server will exit when process {hostPID} exits.");
return true; return true;
} }
else else
{ {
logger.LogError($"Option \"{host.LongName}\" is not a valid Int32 value."); Reporter.Error.WriteLine($"Option \"{host.LongName}\" is not a valid Int32 value.");
return false; return false;
} }
} }

View file

@ -6,18 +6,16 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel.Server.Helpers; using Microsoft.DotNet.ProjectModel.Server.Helpers;
using Microsoft.DotNet.ProjectModel.Server.Messengers; using Microsoft.DotNet.ProjectModel.Server.Messengers;
using Microsoft.DotNet.ProjectModel.Server.Models; using Microsoft.DotNet.ProjectModel.Server.Models;
using Microsoft.Extensions.Logging;
using NuGet.Frameworks; using NuGet.Frameworks;
namespace Microsoft.DotNet.ProjectModel.Server namespace Microsoft.DotNet.ProjectModel.Server
{ {
internal class ProjectManager internal class ProjectManager
{ {
private readonly ILogger _log;
private readonly object _processingLock = new object(); private readonly object _processingLock = new object();
private readonly Queue<Message> _inbox = new Queue<Message>(); private readonly Queue<Message> _inbox = new Queue<Message>();
private readonly ProtocolManager _protocolManager; private readonly ProtocolManager _protocolManager;
@ -42,13 +40,12 @@ namespace Microsoft.DotNet.ProjectModel.Server
private GlobalErrorMessenger _globalErrorMessenger; private GlobalErrorMessenger _globalErrorMessenger;
private ProjectInformationMessenger _projectInforamtionMessenger; private ProjectInformationMessenger _projectInforamtionMessenger;
public ProjectManager(int contextId, public ProjectManager(
ILoggerFactory loggerFactory, int contextId,
WorkspaceContext workspaceContext, WorkspaceContext workspaceContext,
ProtocolManager protocolManager) ProtocolManager protocolManager)
{ {
Id = contextId; Id = contextId;
_log = loggerFactory.CreateLogger<ProjectManager>();
_workspaceContext = workspaceContext; _workspaceContext = workspaceContext;
_protocolManager = protocolManager; _protocolManager = protocolManager;
@ -122,7 +119,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
} }
catch (Exception ex) catch (Exception ex)
{ {
_log.LogError("A unexpected exception occurred: {0}", ex.ToString()); Reporter.Error.WriteLine($"A unexpected exception occurred: {ex}");
var error = new ErrorMessage var error = new ErrorMessage
{ {
@ -169,11 +166,11 @@ namespace Microsoft.DotNet.ProjectModel.Server
private void DrainInbox() private void DrainInbox()
{ {
_log.LogInformation("Begin draining inbox."); Reporter.Output.WriteLine("Begin draining inbox.");
while (ProcessMessage()) { } while (ProcessMessage()) { }
_log.LogInformation("Finish draining inbox."); Reporter.Output.WriteLine("Finish draining inbox.");
} }
private bool ProcessMessage() private bool ProcessMessage()
@ -191,7 +188,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
Debug.Assert(message != null); Debug.Assert(message != null);
} }
_log.LogInformation($"Received {message.MessageType}"); Reporter.Output.WriteLine($"Received {message.MessageType}");
switch (message.MessageType) switch (message.MessageType)
{ {
@ -218,7 +215,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
{ {
if (_initializedContext != null) if (_initializedContext != null)
{ {
_log.LogWarning($"Received {message.MessageType} message more than once for {_appPath.Value}"); Reporter.Output.WriteLine($"Received {message.MessageType} message more than once for {_appPath.Value}");
return; return;
} }
@ -230,7 +227,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
if (version != 0 && !_protocolManager.EnvironmentOverridden) if (version != 0 && !_protocolManager.EnvironmentOverridden)
{ {
_contextProtocolVersion = Math.Min(version, _protocolManager.MaxVersion); _contextProtocolVersion = Math.Min(version, _protocolManager.MaxVersion);
_log.LogInformation($"Set context protocol version to {_contextProtocolVersion.Value}"); Reporter.Output.WriteLine($"Set context protocol version to {_contextProtocolVersion.Value}");
} }
} }

View file

@ -2,9 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System; using System;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel.Server.Models; using Microsoft.DotNet.ProjectModel.Server.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
namespace Microsoft.DotNet.ProjectModel.Server namespace Microsoft.DotNet.ProjectModel.Server
{ {
@ -15,12 +14,9 @@ namespace Microsoft.DotNet.ProjectModel.Server
/// </summary> /// </summary>
public const string EnvDthProtocol = "DTH_PROTOCOL"; public const string EnvDthProtocol = "DTH_PROTOCOL";
private readonly ILogger _log; public ProtocolManager(int maxVersion)
public ProtocolManager(int maxVersion, ILoggerFactory loggerFactory)
{ {
MaxVersion = maxVersion; MaxVersion = maxVersion;
_log = loggerFactory.CreateLogger<ProtocolManager>();
// initialized to the highest supported version or environment overridden value // initialized to the highest supported version or environment overridden value
int? protocol = GetProtocolVersionFromEnvironment(); int? protocol = GetProtocolVersionFromEnvironment();
@ -54,18 +50,18 @@ namespace Microsoft.DotNet.ProjectModel.Server
return; return;
} }
_log.LogInformation("Initializing the protocol negotiation."); Reporter.Output.WriteLine("Initializing the protocol negotiation.");
if (EnvironmentOverridden) if (EnvironmentOverridden)
{ {
_log.LogInformation($"DTH protocol negotiation is override by environment variable {EnvDthProtocol} and set to {CurrentVersion}."); Reporter.Output.WriteLine($"DTH protocol negotiation is override by environment variable {EnvDthProtocol} and set to {CurrentVersion}.");
return; return;
} }
var tokenValue = message.Payload?["Version"]; var tokenValue = message.Payload?["Version"];
if (tokenValue == null) if (tokenValue == null)
{ {
_log.LogInformation("Protocol negotiation failed. Version property is missing in payload."); Reporter.Output.WriteLine("Protocol negotiation failed. Version property is missing in payload.");
return; return;
} }
@ -73,16 +69,16 @@ namespace Microsoft.DotNet.ProjectModel.Server
if (preferredVersion == 0) if (preferredVersion == 0)
{ {
// the preferred version can't be zero. either property is missing or the the payload is corrupted. // the preferred version can't be zero. either property is missing or the the payload is corrupted.
_log.LogInformation("Protocol negotiation failed. Protocol version 0 is invalid."); Reporter.Output.WriteLine("Protocol negotiation failed. Protocol version 0 is invalid.");
return; return;
} }
CurrentVersion = Math.Min(preferredVersion, MaxVersion); CurrentVersion = Math.Min(preferredVersion, MaxVersion);
_log.LogInformation($"Protocol negotiation successed. Use protocol {CurrentVersion}"); Reporter.Output.WriteLine($"Protocol negotiation successed. Use protocol {CurrentVersion}");
if (message.Sender != null) if (message.Sender != null)
{ {
_log.LogInformation("Respond to protocol negotiation."); Reporter.Output.WriteLine("Respond to protocol negotiation.");
message.Sender.Transmit(Message.FromPayload( message.Sender.Transmit(Message.FromPayload(
MessageTypes.ProtocolVersion, MessageTypes.ProtocolVersion,
0, 0,
@ -90,7 +86,7 @@ namespace Microsoft.DotNet.ProjectModel.Server
} }
else else
{ {
_log.LogInformation($"{nameof(Message.Sender)} is null."); Reporter.Output.WriteLine($"{nameof(Message.Sender)} is null.");
} }
} }

View file

@ -28,8 +28,6 @@
"Microsoft.DotNet.ProjectModel": "1.0.0-*", "Microsoft.DotNet.ProjectModel": "1.0.0-*",
"Microsoft.DotNet.Compiler.Common": "1.0.0-*", "Microsoft.DotNet.Compiler.Common": "1.0.0-*",
"Microsoft.DotNet.Cli.Utils": "1.0.0-*", "Microsoft.DotNet.Cli.Utils": "1.0.0-*",
"Microsoft.Extensions.Logging": "1.0.0-rc2-20581",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-20581",
"Microsoft.Extensions.Testing.Abstractions": "1.0.0-*", "Microsoft.Extensions.Testing.Abstractions": "1.0.0-*",
"Microsoft.NETCore.ConsoleHost": "1.0.0-rc2-24027", "Microsoft.NETCore.ConsoleHost": "1.0.0-rc2-24027",
"Microsoft.NETCore.TestHost": "1.0.0-rc2-24027", "Microsoft.NETCore.TestHost": "1.0.0-rc2-24027",

View file

@ -9,7 +9,6 @@ using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using Xunit; using Xunit;
@ -21,7 +20,6 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
private readonly BinaryReader _reader; private readonly BinaryReader _reader;
private readonly BinaryWriter _writer; private readonly BinaryWriter _writer;
private readonly NetworkStream _networkStream; private readonly NetworkStream _networkStream;
private readonly ILogger _logger;
private readonly BlockingCollection<DthMessage> _messageQueue; private readonly BlockingCollection<DthMessage> _messageQueue;
private readonly CancellationTokenSource _readCancellationToken; private readonly CancellationTokenSource _readCancellationToken;
@ -32,13 +30,11 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
private int _nextContextId; private int _nextContextId;
private readonly Socket _socket; private readonly Socket _socket;
public DthTestClient(DthTestServer server, ILoggerFactory loggerFactory) public DthTestClient(DthTestServer server)
{ {
// Avoid Socket exception 10006 on Linux // Avoid Socket exception 10006 on Linux
Thread.Sleep(100); Thread.Sleep(100);
_logger = loggerFactory.CreateLogger<DthTestClient>();
_socket = new Socket(AddressFamily.InterNetwork, _socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, SocketType.Stream,
ProtocolType.Tcp); ProtocolType.Tcp);
@ -238,7 +234,7 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
// Swallow this error for now. // Swallow this error for now.
// This is a temporary fix for a random failure on CI. The issue happens on Windowx x86 // This is a temporary fix for a random failure on CI. The issue happens on Windowx x86
// only. // only.
_logger.LogError($"Exception thrown durning socket shutting down: {ex.SocketErrorCode}."); Console.Error.WriteLine($"Exception thrown durning socket shutting down: {ex.SocketErrorCode}.");
} }
} }

View file

@ -5,8 +5,6 @@ using System;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using Microsoft.DotNet.ProjectModel.Server;
using Microsoft.Extensions.Logging;
namespace Microsoft.DotNet.ProjectModel.Server.Tests namespace Microsoft.DotNet.ProjectModel.Server.Tests
{ {
@ -15,14 +13,12 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
private readonly ProjectModelServerCommand _program; private readonly ProjectModelServerCommand _program;
private readonly Thread _thread; private readonly Thread _thread;
public DthTestServer(ILoggerFactory loggerFactory) public DthTestServer()
{ {
LoggerFactory = loggerFactory;
Port = FindFreePort(); Port = FindFreePort();
HostId = Guid.NewGuid().ToString(); HostId = Guid.NewGuid().ToString();
_program = new ProjectModelServerCommand(Port, HostId, LoggerFactory); _program = new ProjectModelServerCommand(Port, HostId);
_thread = new Thread(() => { _program.OpenChannel(); }) { IsBackground = true }; _thread = new Thread(() => { _program.OpenChannel(); }) { IsBackground = true };
_thread.Start(); _thread.Start();
@ -32,8 +28,6 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
public int Port { get; } public int Port { get; }
public ILoggerFactory LoggerFactory { get; }
public void Dispose() public void Dispose()
{ {
try try

View file

@ -3,7 +3,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -11,7 +10,6 @@ using System.Threading.Tasks;
using Microsoft.DotNet.ProjectModel.Graph; using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.DotNet.TestFramework; using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities; using Microsoft.DotNet.Tools.Test.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -22,30 +20,9 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
public class DthTests : TestBase public class DthTests : TestBase
{ {
private readonly TestAssetsManager _testAssetsManager; private readonly TestAssetsManager _testAssetsManager;
private readonly ILoggerFactory _loggerFactory;
public DthTests() public DthTests()
{ {
_loggerFactory = new LoggerFactory();
var testVerbose = Environment.GetEnvironmentVariable("DOTNET_TEST_VERBOSE");
if (testVerbose == "2")
{
_loggerFactory.AddConsole(LogLevel.Trace);
}
else if (testVerbose == "1")
{
_loggerFactory.AddConsole(LogLevel.Information);
}
else if (testVerbose == "0")
{
_loggerFactory.AddConsole(LogLevel.Warning);
}
else
{
_loggerFactory.AddConsole(LogLevel.Error);
}
_testAssetsManager = new TestAssetsManager( _testAssetsManager = new TestAssetsManager(
Path.Combine(RepoRoot, "TestAssets", "ProjectModelServer", "DthTestProjects", "src")); Path.Combine(RepoRoot, "TestAssets", "ProjectModelServer", "DthTestProjects", "src"));
} }
@ -56,8 +33,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, "EmptyConsoleApp"); var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, "EmptyConsoleApp");
Assert.NotNull(projectPath); Assert.NotNull(projectPath);
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
@ -86,8 +63,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, "EmptyConsoleApp"); var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, "EmptyConsoleApp");
Assert.NotNull(projectPath); Assert.NotNull(projectPath);
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
var messages = client.DrainAllMessages() var messages = client.DrainAllMessages()
@ -123,8 +100,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
[InlineData(3, 3)] [InlineData(3, 3)]
public void DthStartup_ProtocolNegotiation(int requestVersion, int expectVersion) public void DthStartup_ProtocolNegotiation(int requestVersion, int expectVersion)
{ {
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.SetProtocolVersion(requestVersion); client.SetProtocolVersion(requestVersion);
@ -138,8 +115,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
[Fact] [Fact]
public void DthStartup_ProtocolNegotiation_ZeroIsNoAllowed() public void DthStartup_ProtocolNegotiation_ZeroIsNoAllowed()
{ {
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.SetProtocolVersion(0); client.SetProtocolVersion(0);
@ -168,8 +145,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, testProjectName); var projectPath = Path.Combine(_testAssetsManager.AssetsRoot, testProjectName);
Assert.NotNull(projectPath); Assert.NotNull(projectPath);
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
@ -221,8 +198,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
[Fact] [Fact]
public void DthNegative_BrokenProjectPathInLockFile() public void DthNegative_BrokenProjectPathInLockFile()
{ {
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
// After restore the project is copied to another place so that // After restore the project is copied to another place so that
// the relative path in project lock file is invalid. // the relative path in project lock file is invalid.
@ -255,8 +232,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var projectPath = assets.CreateTestInstance("DthUpdateSearchPathSample").WithLockFiles().TestRoot; var projectPath = assets.CreateTestInstance("DthUpdateSearchPathSample").WithLockFiles().TestRoot;
Assert.True(Directory.Exists(projectPath)); Assert.True(Directory.Exists(projectPath));
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
var testProject = Path.Combine(projectPath, "home", "src", "MainProject"); var testProject = Path.Combine(projectPath, "home", "src", "MainProject");
@ -312,8 +289,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
{ {
var projectPath = _testAssetsManager.CreateTestInstance("EmptyConsoleApp").TestRoot; var projectPath = _testAssetsManager.CreateTestInstance("EmptyConsoleApp").TestRoot;
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
var messages = client.DrainAllMessages(); var messages = client.DrainAllMessages();
@ -338,8 +315,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var assetsManager = new TestAssetsManager(testAssetsPath); var assetsManager = new TestAssetsManager(testAssetsPath);
var testSource = assetsManager.CreateTestInstance("IncorrectProjectJson").TestRoot; var testSource = assetsManager.CreateTestInstance("IncorrectProjectJson").TestRoot;
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(Path.Combine(_testAssetsManager.AssetsRoot, "EmptyLibrary")); client.Initialize(Path.Combine(_testAssetsManager.AssetsRoot, "EmptyLibrary"));
client.Initialize(testSource); client.Initialize(testSource);
@ -371,8 +348,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var assetsManager = new TestAssetsManager(testAssetsPath); var assetsManager = new TestAssetsManager(testAssetsPath);
var testSource = assetsManager.CreateTestInstance("IncorrectGlobalJson"); var testSource = assetsManager.CreateTestInstance("IncorrectGlobalJson");
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(Path.Combine(testSource.TestRoot, "src", "Project1")); client.Initialize(Path.Combine(testSource.TestRoot, "src", "Project1"));
@ -390,8 +367,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
.WithLockFiles() .WithLockFiles()
.TestRoot; .TestRoot;
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
var projectFile = Path.Combine(testProject, Project.FileName); var projectFile = Path.Combine(testProject, Project.FileName);
var content = File.ReadAllText(projectFile); var content = File.ReadAllText(projectFile);
@ -417,8 +394,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
.WithLockFiles() .WithLockFiles()
.TestRoot; .TestRoot;
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
var lockFilePath = Path.Combine(testProject, LockFile.FileName); var lockFilePath = Path.Combine(testProject, LockFile.FileName);
var lockFileContent = File.ReadAllText(lockFilePath); var lockFileContent = File.ReadAllText(lockFilePath);
@ -469,8 +446,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
File.WriteAllText(projectFilePath, JsonConvert.SerializeObject(projectJson)); File.WriteAllText(projectFilePath, JsonConvert.SerializeObject(projectJson));
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
var messages = client.DrainAllMessages(); var messages = client.DrainAllMessages();
@ -495,8 +472,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
"src", "src",
"MainApp"); "MainApp");
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(testProject); client.Initialize(testProject);
var messages = client.DrainAllMessages(); var messages = client.DrainAllMessages();
@ -550,8 +527,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
.WithLockFiles() .WithLockFiles()
.TestRoot; .TestRoot;
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
@ -596,8 +573,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
var projectPath = tam.CreateTestInstance("ValidCase01").WithLockFiles().TestRoot; var projectPath = tam.CreateTestInstance("ValidCase01").WithLockFiles().TestRoot;
projectPath = Path.Combine(projectPath, "src", "MainApp"); projectPath = Path.Combine(projectPath, "src", "MainApp");
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);
@ -638,8 +615,8 @@ namespace Microsoft.DotNet.ProjectModel.Server.Tests
{ {
var projectPath = Path.Combine(RepoRoot, "TestAssets", "ProjectModelServer", "MscorlibLibraryDuplication"); var projectPath = Path.Combine(RepoRoot, "TestAssets", "ProjectModelServer", "MscorlibLibraryDuplication");
using (var server = new DthTestServer(_loggerFactory)) using (var server = new DthTestServer())
using (var client = new DthTestClient(server, _loggerFactory)) using (var client = new DthTestClient(server))
{ {
client.Initialize(projectPath); client.Initialize(projectPath);