Merge branch 'drussilla/add-logging' into dev

Conflicts:
	src/Microsoft.AspNet.Server.Kestrel/Http/Connection.cs
	src/Microsoft.AspNet.Server.Kestrel/Http/ListenerSecondary.cs
This commit is contained in:
Stephen Halter 2015-09-09 11:28:09 -07:00
commit dc08062545
39 changed files with 304 additions and 165 deletions

View File

@ -4,13 +4,18 @@
using System; using System;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.Logging;
namespace SampleApp namespace SampleApp
{ {
public class Startup public class Startup
{ {
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{ {
loggerFactory.MinimumLevel = LogLevel.Debug;
loggerFactory.AddConsole(LogLevel.Debug);
app.Run(context => app.Run(context =>
{ {
Console.WriteLine("{0} {1}{2}{3}", Console.WriteLine("{0} {1}{2}{3}",

View File

@ -1,7 +1,8 @@
{ {
"version": "1.0.0-*", "version": "1.0.0-*",
"dependencies": { "dependencies": {
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*" "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
"Microsoft.Framework.Logging.Console": "1.0.0-*"
}, },
"frameworks": { "frameworks": {
"dnx451": { }, "dnx451": { },

View File

@ -2,9 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics;
using Microsoft.AspNet.Server.Kestrel.Infrastructure; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Http namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
@ -28,10 +28,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
public void Start() public void Start()
{ {
KestrelTrace.Log.ConnectionStart(_connectionId); Log.ConnectionStart(_connectionId);
SocketInput = new SocketInput(Memory); SocketInput = new SocketInput(Memory);
SocketOutput = new SocketOutput(Thread, _socket); SocketOutput = new SocketOutput(Thread, _socket, Log);
_frame = new Frame(this); _frame = new Frame(this);
_socket.ReadStart(_allocCallback, _readCallback, this); _socket.ReadStart(_allocCallback, _readCallback, this);
} }
@ -63,17 +63,17 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (normalRead) if (normalRead)
{ {
KestrelTrace.Log.ConnectionRead(_connectionId, status); Log.ConnectionRead(_connectionId, status);
} }
else if (normalDone || errorDone) else if (normalDone || errorDone)
{ {
KestrelTrace.Log.ConnectionReadFin(_connectionId); Log.ConnectionReadFin(_connectionId);
SocketInput.RemoteIntakeFin = true; SocketInput.RemoteIntakeFin = true;
_socket.ReadStop(); _socket.ReadStop();
if (errorDone && error != null) if (errorDone && error != null)
{ {
Trace.WriteLine("Connection.OnRead " + error.ToString()); Log.LogError("Connection.OnRead", error);
} }
} }
@ -84,19 +84,19 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("Connection._frame.Consume " + ex.ToString()); Log.LogError("Connection._frame.Consume ", ex);
} }
} }
void IConnectionControl.Pause() void IConnectionControl.Pause()
{ {
KestrelTrace.Log.ConnectionPause(_connectionId); Log.ConnectionPause(_connectionId);
_socket.ReadStop(); _socket.ReadStop();
} }
void IConnectionControl.Resume() void IConnectionControl.Resume()
{ {
KestrelTrace.Log.ConnectionResume(_connectionId); Log.ConnectionResume(_connectionId);
_socket.ReadStart(_allocCallback, _readCallback, this); _socket.ReadStart(_allocCallback, _readCallback, this);
} }
@ -113,17 +113,17 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
_connectionState = ConnectionState.Shutdown; _connectionState = ConnectionState.Shutdown;
KestrelTrace.Log.ConnectionWriteFin(_connectionId, 0); Log.ConnectionWriteFin(_connectionId, 0);
Thread.Post( Thread.Post(
state => state =>
{ {
KestrelTrace.Log.ConnectionWriteFin(_connectionId, 1); Log.ConnectionWriteFin(_connectionId, 1);
var self = (Connection)state; var self = (Connection)state;
var shutdown = new UvShutdownReq(); var shutdown = new UvShutdownReq(Log);
shutdown.Init(self.Thread.Loop); shutdown.Init(self.Thread.Loop);
shutdown.Shutdown(self._socket, (req, status, _) => shutdown.Shutdown(self._socket, (req, status, _) =>
{ {
KestrelTrace.Log.ConnectionWriteFin(_connectionId, 1); Log.ConnectionWriteFin(_connectionId, 1);
req.Dispose(); req.Dispose();
}, null); }, null);
}, },
@ -135,7 +135,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
return; return;
} }
KestrelTrace.Log.ConnectionKeepAlive(_connectionId); Log.ConnectionKeepAlive(_connectionId);
_frame = new Frame(this); _frame = new Frame(this);
Thread.Post( Thread.Post(
state => ((Frame)state).Consume(), state => ((Frame)state).Consume(),
@ -148,11 +148,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
_connectionState = ConnectionState.Disconnected; _connectionState = ConnectionState.Disconnected;
KestrelTrace.Log.ConnectionDisconnect(_connectionId); Log.ConnectionDisconnect(_connectionId);
Thread.Post( Thread.Post(
state => state =>
{ {
KestrelTrace.Log.ConnectionStop(_connectionId); Log.ConnectionStop(_connectionId);
((UvHandle)state).Dispose(); ((UvHandle)state).Dispose();
}, },
_socket); _socket);

View File

@ -3,11 +3,11 @@
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.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Primitives; using Microsoft.Framework.Primitives;
// ReSharper disable AccessToModifiedClosure // ReSharper disable AccessToModifiedClosure
@ -276,7 +276,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
if (error != null) if (error != null)
{ {
Trace.WriteLine("WriteChunkPrefix" + error.ToString()); Log.LogError("WriteChunkPrefix", error);
} }
}, },
null, null,
@ -290,7 +290,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
if (error != null) if (error != null)
{ {
Trace.WriteLine("WriteChunkSuffix" + error.ToString()); Log.LogError("WriteChunkSuffix", error);
} }
}, },
null, null,
@ -304,7 +304,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
if (error != null) if (error != null)
{ {
Trace.WriteLine("WriteChunkedResponseSuffix" + error.ToString()); Log.LogError("WriteChunkedResponseSuffix", error);
} }
}, },
null, null,
@ -338,7 +338,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
if (error != null) if (error != null)
{ {
Trace.WriteLine("ProduceContinue " + error.ToString()); Log.LogError("ProduceContinue ", error);
} }
}, },
null); null);
@ -361,7 +361,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
if (error != null) if (error != null)
{ {
Trace.WriteLine("ProduceStart " + error.ToString()); Log.LogError("ProduceStart ", error);
} }
((IDisposable)state).Dispose(); ((IDisposable)state).Dispose();
}, },

View File

@ -3,8 +3,8 @@
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using System; using System;
using System.Diagnostics;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Http namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
@ -52,13 +52,14 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
protected static void ConnectionCallback(UvStreamHandle stream, int status, Exception error, object state) protected static void ConnectionCallback(UvStreamHandle stream, int status, Exception error, object state)
{ {
var listener = (Listener) state;
if (error != null) if (error != null)
{ {
Trace.WriteLine("Listener.ConnectionCallback " + error.ToString()); listener.Log.LogError("Listener.ConnectionCallback ", error);
} }
else else
{ {
((Listener)state).OnConnection(stream, status); listener.OnConnection(stream, status);
} }
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Http namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
@ -13,6 +14,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
public ListenerContext(ServiceContext serviceContext) public ListenerContext(ServiceContext serviceContext)
{ {
Memory = serviceContext.Memory; Memory = serviceContext.Memory;
Log = serviceContext.Log;
} }
public ListenerContext(ListenerContext listenerContext) public ListenerContext(ListenerContext listenerContext)
@ -20,6 +22,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
Thread = listenerContext.Thread; Thread = listenerContext.Thread;
Application = listenerContext.Application; Application = listenerContext.Application;
Memory = listenerContext.Memory; Memory = listenerContext.Memory;
Log = listenerContext.Log;
} }
public KestrelThread Thread { get; set; } public KestrelThread Thread { get; set; }
@ -27,5 +30,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
public Func<Frame, Task> Application { get; set; } public Func<Frame, Task> Application { get; set; }
public IMemoryPool Memory { get; set; } public IMemoryPool Memory { get; set; }
public IKestrelTrace Log { get; }
} }
} }

View File

@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
await Thread.PostAsync(_ => await Thread.PostAsync(_ =>
{ {
ListenPipe = new UvPipeHandle(); ListenPipe = new UvPipeHandle(Log);
ListenPipe.Init(Thread.Loop, false); ListenPipe.Init(Thread.Loop, false);
ListenPipe.Bind(pipeName); ListenPipe.Bind(pipeName);
ListenPipe.Listen(Constants.ListenBacklog, OnListenPipe, null); ListenPipe.Listen(Constants.ListenBacklog, OnListenPipe, null);
@ -54,7 +54,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
return; return;
} }
var dispatchPipe = new UvPipeHandle(); var dispatchPipe = new UvPipeHandle(Log);
dispatchPipe.Init(Thread.Loop, true); dispatchPipe.Init(Thread.Loop, true);
try try
{ {
@ -78,7 +78,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
else else
{ {
var dispatchPipe = _dispatchPipes[index]; var dispatchPipe = _dispatchPipes[index];
var write = new UvWriteReq(); var write = new UvWriteReq(Log);
write.Init(Thread.Loop); write.Init(Thread.Loop);
write.Write2( write.Write2(
dispatchPipe, dispatchPipe,

View File

@ -2,11 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Server.Kestrel.Infrastructure; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Http namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
@ -30,7 +30,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
Thread = thread; Thread = thread;
Application = application; Application = application;
DispatchPipe = new UvPipeHandle(); DispatchPipe = new UvPipeHandle(Log);
var tcs = new TaskCompletionSource<int>(); var tcs = new TaskCompletionSource<int>();
Thread.Post(_ => Thread.Post(_ =>
@ -38,7 +38,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
try try
{ {
DispatchPipe.Init(Thread.Loop, true); DispatchPipe.Init(Thread.Loop, true);
var connect = new UvConnectRequest(); var connect = new UvConnectRequest(Log);
connect.Init(Thread.Loop); connect.Init(Thread.Loop);
connect.Connect( connect.Connect(
DispatchPipe, DispatchPipe,
@ -67,8 +67,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
{ {
Exception ex; Exception ex;
Thread.Loop.Libuv.Check(status2, out ex); Thread.Loop.Libuv.Check(status2, out ex);
// TODO: Replace Trace.WriteLine with real logging Log.LogError("DispatchPipe.ReadStart", ex);
Trace.WriteLine("DispatchPipe.ReadStart " + ex.Message);
} }
DispatchPipe.Dispose(); DispatchPipe.Dispose();
@ -89,7 +88,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("DispatchPipe.Accept " + ex.Message); Log.LogError("DispatchPipe.Accept", ex);
acceptSocket.Dispose(); acceptSocket.Dispose();
return; return;
} }

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateListenSocket(string host, int port) protected override UvStreamHandle CreateListenSocket(string host, int port)
{ {
var socket = new UvPipeHandle(); var socket = new UvPipeHandle(Log);
socket.Init(Thread.Loop, false); socket.Init(Thread.Loop, false);
socket.Bind(host); socket.Bind(host);
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this); socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// <param name="status">Connection status</param> /// <param name="status">Connection status</param>
protected override void OnConnection(UvStreamHandle listenSocket, int status) protected override void OnConnection(UvStreamHandle listenSocket, int status)
{ {
var acceptSocket = new UvPipeHandle(); var acceptSocket = new UvPipeHandle(Log);
acceptSocket.Init(Thread.Loop, false); acceptSocket.Init(Thread.Loop, false);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);

View File

@ -20,7 +20,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateListenSocket(string host, int port) protected override UvStreamHandle CreateListenSocket(string host, int port)
{ {
var socket = new UvPipeHandle(); var socket = new UvPipeHandle(Log);
socket.Init(Thread.Loop, false); socket.Init(Thread.Loop, false);
socket.Bind(host); socket.Bind(host);
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this); socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// <param name="status">Connection status</param> /// <param name="status">Connection status</param>
protected override void OnConnection(UvStreamHandle listenSocket, int status) protected override void OnConnection(UvStreamHandle listenSocket, int status)
{ {
var acceptSocket = new UvPipeHandle(); var acceptSocket = new UvPipeHandle(Log);
acceptSocket.Init(Thread.Loop, false); acceptSocket.Init(Thread.Loop, false);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateAcceptSocket() protected override UvStreamHandle CreateAcceptSocket()
{ {
var acceptSocket = new UvPipeHandle(); var acceptSocket = new UvPipeHandle(Log);
acceptSocket.Init(Thread.Loop, false); acceptSocket.Init(Thread.Loop, false);
return acceptSocket; return acceptSocket;
} }

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
namespace Microsoft.AspNet.Server.Kestrel.Http namespace Microsoft.AspNet.Server.Kestrel.Http
@ -16,6 +17,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private readonly KestrelThread _thread; private readonly KestrelThread _thread;
private readonly UvStreamHandle _socket; private readonly UvStreamHandle _socket;
private readonly IKestrelTrace _log;
// This locks access to to all of the below fields // This locks access to to all of the below fields
private readonly object _lockObj = new object(); private readonly object _lockObj = new object();
@ -29,10 +31,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
private WriteContext _nextWriteContext; private WriteContext _nextWriteContext;
private readonly Queue<CallbackContext> _callbacksPending; private readonly Queue<CallbackContext> _callbacksPending;
public SocketOutput(KestrelThread thread, UvStreamHandle socket) public SocketOutput(KestrelThread thread, UvStreamHandle socket, IKestrelTrace log)
{ {
_thread = thread; _thread = thread;
_socket = socket; _socket = socket;
_log = log;
_callbacksPending = new Queue<CallbackContext>(); _callbacksPending = new Queue<CallbackContext>();
} }
@ -43,7 +46,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
Array.Copy(buffer.Array, buffer.Offset, copy, 0, buffer.Count); Array.Copy(buffer.Array, buffer.Offset, copy, 0, buffer.Count);
buffer = new ArraySegment<byte>(copy); buffer = new ArraySegment<byte>(copy);
KestrelTrace.Log.ConnectionWrite(0, buffer.Count); _log.ConnectionWrite(0, buffer.Count);
bool triggerCallbackNow = false; bool triggerCallbackNow = false;
@ -127,7 +130,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
buffers[i++] = buffer; buffers[i++] = buffer;
} }
var writeReq = new UvWriteReq(); var writeReq = new UvWriteReq(_log);
writeReq.Init(_thread.Loop); writeReq.Init(_thread.Loop);
writeReq.Write(_socket, new ArraySegment<ArraySegment<byte>>(buffers), (r, status, error, state) => writeReq.Write(_socket, new ArraySegment<ArraySegment<byte>>(buffers), (r, status, error, state) =>
@ -152,7 +155,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
// This is called on the libuv event loop // This is called on the libuv event loop
private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, UvWriteReq req, int status, Exception error) private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, UvWriteReq req, int status, Exception error)
{ {
KestrelTrace.Log.ConnectionWriteCallback(0, status); _log.ConnectionWriteCallback(0, status);
lock (_lockObj) lock (_lockObj)
{ {

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateListenSocket(string host, int port) protected override UvStreamHandle CreateListenSocket(string host, int port)
{ {
var socket = new UvTcpHandle(); var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle); socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.Bind(new IPEndPoint(IPAddress.Any, port)); socket.Bind(new IPEndPoint(IPAddress.Any, port));
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this); socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// <param name="status">Connection status</param> /// <param name="status">Connection status</param>
protected override void OnConnection(UvStreamHandle listenSocket, int status) protected override void OnConnection(UvStreamHandle listenSocket, int status)
{ {
var acceptSocket = new UvTcpHandle(); var acceptSocket = new UvTcpHandle(Log);
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);

View File

@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateListenSocket(string host, int port) protected override UvStreamHandle CreateListenSocket(string host, int port)
{ {
var socket = new UvTcpHandle(); var socket = new UvTcpHandle(Log);
socket.Init(Thread.Loop, Thread.QueueCloseHandle); socket.Init(Thread.Loop, Thread.QueueCloseHandle);
socket.Bind(new IPEndPoint(IPAddress.Any, port)); socket.Bind(new IPEndPoint(IPAddress.Any, port));
socket.Listen(Constants.ListenBacklog, ConnectionCallback, this); socket.Listen(Constants.ListenBacklog, ConnectionCallback, this);
@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// <param name="status">Connection status</param> /// <param name="status">Connection status</param>
protected override void OnConnection(UvStreamHandle listenSocket, int status) protected override void OnConnection(UvStreamHandle listenSocket, int status)
{ {
var acceptSocket = new UvTcpHandle(); var acceptSocket = new UvTcpHandle(Log);
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
listenSocket.Accept(acceptSocket); listenSocket.Accept(acceptSocket);

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
/// </summary> /// </summary>
protected override UvStreamHandle CreateAcceptSocket() protected override UvStreamHandle CreateAcceptSocket()
{ {
var acceptSocket = new UvTcpHandle(); var acceptSocket = new UvTcpHandle(Log);
acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle); acceptSocket.Init(Thread.Loop, Thread.QueueCloseHandle);
return acceptSocket; return acceptSocket;
} }

View File

@ -0,0 +1,29 @@
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
{
public interface IKestrelTrace : ILogger
{
void ConnectionStart(long connectionId);
void ConnectionStop(long connectionId);
void ConnectionRead(long connectionId, int status);
void ConnectionPause(long connectionId);
void ConnectionResume(long connectionId);
void ConnectionReadFin(long connectionId);
void ConnectionWriteFin(long connectionId, int step);
void ConnectionKeepAlive(long connectionId);
void ConnectionDisconnect(long connectionId);
void ConnectionWrite(long connectionId, int count);
void ConnectionWriteCallback(long connectionId, int status);
}
}

View File

@ -4,11 +4,12 @@
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.ExceptionServices; using System.Runtime.ExceptionServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel namespace Microsoft.AspNet.Server.Kestrel
{ {
@ -30,13 +31,15 @@ namespace Microsoft.AspNet.Server.Kestrel
private object _workSync = new Object(); private object _workSync = new Object();
private bool _stopImmediate = false; private bool _stopImmediate = false;
private ExceptionDispatchInfo _closeError; private ExceptionDispatchInfo _closeError;
private IKestrelTrace _log;
public KestrelThread(KestrelEngine engine, ServiceContext serviceContext) public KestrelThread(KestrelEngine engine, ServiceContext serviceContext)
{ {
_engine = engine; _engine = engine;
_appShutdown = serviceContext.AppShutdown; _appShutdown = serviceContext.AppShutdown;
_loop = new UvLoopHandle(); _log = serviceContext.Log;
_post = new UvAsyncHandle(); _loop = new UvLoopHandle(_log);
_post = new UvAsyncHandle(_log);
_thread = new Thread(ThreadStart); _thread = new Thread(ThreadStart);
QueueCloseHandle = PostCloseHandle; QueueCloseHandle = PostCloseHandle;
} }
@ -272,7 +275,7 @@ namespace Microsoft.AspNet.Server.Kestrel
} }
else else
{ {
Trace.WriteLine("KestrelThread.DoPostWork " + ex.ToString()); _log.LogError("KestrelThread.DoPostWork", ex);
} }
} }
} }
@ -295,7 +298,7 @@ namespace Microsoft.AspNet.Server.Kestrel
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("KestrelThread.DoPostCloseHandle " + ex.ToString()); _log.LogError("KestrelThread.DoPostCloseHandle", ex);
} }
} }
} }

View File

@ -1,85 +1,92 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
//using System.Diagnostics.Tracing; using System;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel namespace Microsoft.AspNet.Server.Kestrel
{ {
/// <summary> /// <summary>
/// Summary description for KestrelTrace /// Summary description for KestrelTrace
/// </summary> /// </summary>
public class KestrelTrace //: EventSource public class KestrelTrace : IKestrelTrace
{ {
public static KestrelTrace Log = new KestrelTrace(); private readonly ILogger _logger;
// static EventTask Connection = (EventTask)1;
// static EventTask Frame = (EventTask)1;
public KestrelTrace(ILogger logger)
{
_logger = logger;
}
// [Event(13, Level = EventLevel.Informational, Message = "Id {0}")]
public void ConnectionStart(long connectionId) public void ConnectionStart(long connectionId)
{ {
// WriteEvent(13, connectionId); _logger.LogDebug(13, $"{nameof(ConnectionStart)} -> Id: {connectionId}");
} }
// [Event(14, Level = EventLevel.Informational, Message = "Id {0}")]
public void ConnectionStop(long connectionId) public void ConnectionStop(long connectionId)
{ {
// WriteEvent(14, connectionId); _logger.LogDebug(14, $"{nameof(ConnectionStop)} -> Id: {connectionId}");
} }
public void ConnectionRead(long connectionId, int status)
// [Event(4, Message = "Id {0} Status {1}")]
internal void ConnectionRead(long connectionId, int status)
{ {
// WriteEvent(4, connectionId, status); _logger.LogDebug(4, $"{nameof(ConnectionRead)} -> Id: {connectionId}, Status: {status}");
} }
// [Event(5, Message = "Id {0}")] public void ConnectionPause(long connectionId)
internal void ConnectionPause(long connectionId)
{ {
// WriteEvent(5, connectionId); _logger.LogDebug(5, $"{nameof(ConnectionPause)} -> Id: {connectionId}");
} }
// [Event(6, Message = "Id {0}")] public void ConnectionResume(long connectionId)
internal void ConnectionResume(long connectionId)
{ {
// WriteEvent(6, connectionId); _logger.LogDebug(6, $"{nameof(ConnectionResume)} -> Id: {connectionId}");
} }
// [Event(7, Message = "Id {0}")] public void ConnectionReadFin(long connectionId)
internal void ConnectionReadFin(long connectionId)
{ {
// WriteEvent(7, connectionId); _logger.LogDebug(7, $"{nameof(ConnectionReadFin)} -> Id: {connectionId}");
} }
// [Event(8, Message = "Id {0} Step {1}")] public void ConnectionWriteFin(long connectionId, int step)
internal void ConnectionWriteFin(long connectionId, int step)
{ {
// WriteEvent(8, connectionId, step); _logger.LogDebug(8, $"{nameof(ConnectionWriteFin)} -> Id: {connectionId}, Step: {step}");
} }
// [Event(9, Message = "Id {0}")] public void ConnectionKeepAlive(long connectionId)
internal void ConnectionKeepAlive(long connectionId)
{ {
// WriteEvent(9, connectionId); _logger.LogDebug(9, $"{nameof(ConnectionKeepAlive)} -> Id: {connectionId}");
} }
// [Event(10, Message = "Id {0}")] public void ConnectionDisconnect(long connectionId)
internal void ConnectionDisconnect(long connectionId)
{ {
// WriteEvent(10, connectionId); _logger.LogDebug(10, $"{nameof(ConnectionDisconnect)} -> Id: {connectionId}");
} }
// [Event(11, Message = "Id {0} Count {1}")] public void ConnectionWrite(long connectionId, int count)
internal void ConnectionWrite(long connectionId, int count)
{ {
// WriteEvent(11, connectionId, count); _logger.LogDebug(11, $"{nameof(ConnectionWrite)} -> Id: {connectionId}, Count: {count}");
} }
// [Event(12, Message = "Id {0} Status {1}")] public void ConnectionWriteCallback(long connectionId, int status)
internal void ConnectionWriteCallback(long connectionId, int status)
{ {
// WriteEvent(12, connectionId, status); _logger.LogDebug(12, $"{nameof(ConnectionWriteCallback)} -> Id: {connectionId}, Status: {status}");
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
_logger.Log(logLevel, eventId, state, exception, formatter);
}
public bool IsEnabled(LogLevel logLevel)
{
return _logger.IsEnabled(logLevel);
}
public IDisposable BeginScopeImpl(object state)
{
return _logger.BeginScopeImpl(state);
} }
} }
} }

View File

@ -9,6 +9,7 @@ using Microsoft.AspNet.Server.Kestrel.Http;
using Microsoft.AspNet.Server.Kestrel.Infrastructure; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel namespace Microsoft.AspNet.Server.Kestrel
{ {
@ -16,8 +17,8 @@ namespace Microsoft.AspNet.Server.Kestrel
{ {
private readonly ServiceContext _serviceContext; private readonly ServiceContext _serviceContext;
public KestrelEngine(ILibraryManager libraryManager, IApplicationShutdown appShutdownService) public KestrelEngine(ILibraryManager libraryManager, IApplicationShutdown appShutdownService, ILogger logger)
: this(appShutdownService) : this(appShutdownService, logger)
{ {
Libuv = new Libuv(); Libuv = new Libuv();
@ -62,18 +63,19 @@ namespace Microsoft.AspNet.Server.Kestrel
} }
// For testing // For testing
internal KestrelEngine(Libuv uv, IApplicationShutdown appShutdownService) internal KestrelEngine(Libuv uv, IApplicationShutdown appShutdownService, ILogger logger)
: this(appShutdownService) : this(appShutdownService, logger)
{ {
Libuv = uv; Libuv = uv;
} }
private KestrelEngine(IApplicationShutdown appShutdownService) private KestrelEngine(IApplicationShutdown appShutdownService, ILogger logger)
{ {
_serviceContext = new ServiceContext _serviceContext = new ServiceContext
{ {
AppShutdown = appShutdownService, AppShutdown = appShutdownService,
Memory = new MemoryPool() Memory = new MemoryPool(),
Log = new KestrelTrace(logger)
}; };
Threads = new List<KestrelThread>(); Threads = new List<KestrelThread>();

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -10,6 +11,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
private static Libuv.uv_async_cb _uv_async_cb = AsyncCb; private static Libuv.uv_async_cb _uv_async_cb = AsyncCb;
private Action _callback; private Action _callback;
public UvAsyncHandle(IKestrelTrace logger) : base(logger)
{
}
public void Init(UvLoopHandle loop, Action callback) public void Init(UvLoopHandle loop, Action callback)
{ {
CreateMemory( CreateMemory(

View File

@ -2,9 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Collections.Generic; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using System.Diagnostics; using Microsoft.Framework.Logging;
using System.Runtime.InteropServices;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -18,6 +17,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
private Action<UvConnectRequest, int, Exception, object> _callback; private Action<UvConnectRequest, int, Exception, object> _callback;
private object _state; private object _state;
public UvConnectRequest(IKestrelTrace logger) : base (logger)
{
}
public void Init(UvLoopHandle loop) public void Init(UvLoopHandle loop)
{ {
var requestSize = loop.Libuv.req_size(Libuv.RequestType.CONNECT); var requestSize = loop.Libuv.req_size(Libuv.RequestType.CONNECT);
@ -63,7 +66,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("UvConnectRequest " + ex.ToString()); req._log.LogError("UvConnectRequest", ex);
} }
} }
} }

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -11,6 +12,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
private static Libuv.uv_close_cb _destroyMemory = DestroyMemory; private static Libuv.uv_close_cb _destroyMemory = DestroyMemory;
private Action<Action<IntPtr>, IntPtr> _queueCloseHandle; private Action<Action<IntPtr>, IntPtr> _queueCloseHandle;
protected UvHandle(IKestrelTrace logger) : base (logger)
{
}
unsafe protected void CreateHandle( unsafe protected void CreateHandle(
Libuv uv, Libuv uv,
int threadId, int threadId,

View File

@ -3,11 +3,16 @@
using System; using System;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
public class UvLoopHandle : UvHandle public class UvLoopHandle : UvHandle
{ {
public UvLoopHandle(IKestrelTrace logger) : base(logger)
{
}
public void Init(Libuv uv) public void Init(Libuv uv)
{ {
CreateMemory( CreateMemory(

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -15,9 +16,11 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
protected Libuv _uv; protected Libuv _uv;
protected int _threadId; protected int _threadId;
protected IKestrelTrace _log;
public UvMemory() : base(IntPtr.Zero, true) protected UvMemory(IKestrelTrace logger) : base(IntPtr.Zero, true)
{ {
_log = logger;
} }
public Libuv Libuv { get { return _uv; } } public Libuv Libuv { get { return _uv; } }

View File

@ -3,11 +3,16 @@
using System; using System;
using System.Net; using System.Net;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
public class UvPipeHandle : UvStreamHandle public class UvPipeHandle : UvStreamHandle
{ {
public UvPipeHandle(IKestrelTrace logger) : base(logger)
{
}
public void Init(UvLoopHandle loop, bool ipc) public void Init(UvLoopHandle loop, bool ipc)
{ {
CreateMemory( CreateMemory(

View File

@ -1,8 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -10,6 +8,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
private GCHandle _pin; private GCHandle _pin;
protected UvRequest(IKestrelTrace logger) : base (logger)
{
}
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {
DestroyMemory(handle); DestroyMemory(handle);

View File

@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Runtime.InteropServices; using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -16,6 +16,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
private Action<UvShutdownReq, int, object> _callback; private Action<UvShutdownReq, int, object> _callback;
private object _state; private object _state;
public UvShutdownReq(IKestrelTrace logger) : base (logger)
{
}
public void Init(UvLoopHandle loop) public void Init(UvLoopHandle loop)
{ {
CreateMemory( CreateMemory(

View File

@ -2,8 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -22,6 +23,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
public object _readState; public object _readState;
private GCHandle _readVitality; private GCHandle _readVitality;
protected UvStreamHandle(IKestrelTrace logger) : base(logger)
{
}
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {
if (_listenVitality.IsAllocated) if (_listenVitality.IsAllocated)
@ -127,7 +132,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("UvConnectionCb " + ex.ToString()); stream._log.LogError("UvConnectionCb", ex);
} }
} }
@ -141,7 +146,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("UvAllocCb " + ex.ToString()); stream._log.LogError("UvAllocCb", ex);
buf = stream.Libuv.buf_init(IntPtr.Zero, 0); buf = stream.Libuv.buf_init(IntPtr.Zero, 0);
throw; throw;
} }
@ -166,7 +171,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("UbReadCb " + ex.ToString()); stream._log.LogError("UbReadCb", ex);
} }
} }

View File

@ -3,11 +3,16 @@
using System; using System;
using System.Net; using System.Net;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
public class UvTcpHandle : UvStreamHandle public class UvTcpHandle : UvStreamHandle
{ {
public UvTcpHandle(IKestrelTrace logger) : base(logger)
{
}
public void Init(UvLoopHandle loop) public void Init(UvLoopHandle loop)
{ {
CreateMemory( CreateMemory(

View File

@ -3,8 +3,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel.Networking namespace Microsoft.AspNet.Server.Kestrel.Networking
{ {
@ -23,6 +24,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
private List<GCHandle> _pins = new List<GCHandle>(); private List<GCHandle> _pins = new List<GCHandle>();
public UvWriteReq(IKestrelTrace logger) : base(logger)
{
}
public void Init(UvLoopHandle loop) public void Init(UvLoopHandle loop)
{ {
var requestSize = loop.Libuv.req_size(Libuv.RequestType.WRITE); var requestSize = loop.Libuv.req_size(Libuv.RequestType.WRITE);
@ -161,7 +166,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Networking
} }
catch (Exception ex) catch (Exception ex)
{ {
Trace.WriteLine("UvWriteCb " + ex.ToString()); req._log.LogError("UvWriteCb", ex);
} }
} }
} }

View File

@ -8,6 +8,7 @@ using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration; using Microsoft.Framework.Configuration;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.Kestrel namespace Microsoft.AspNet.Server.Kestrel
{ {
@ -18,11 +19,13 @@ namespace Microsoft.AspNet.Server.Kestrel
{ {
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
private readonly IApplicationShutdown _appShutdownService; private readonly IApplicationShutdown _appShutdownService;
private readonly ILogger _logger;
public ServerFactory(ILibraryManager libraryManager, IApplicationShutdown appShutdownService) public ServerFactory(ILibraryManager libraryManager, IApplicationShutdown appShutdownService, ILoggerFactory loggerFactory)
{ {
_libraryManager = libraryManager; _libraryManager = libraryManager;
_appShutdownService = appShutdownService; _appShutdownService = appShutdownService;
_logger = loggerFactory.CreateLogger("Microsoft.AspNet.Server.Kestrel");
} }
public IFeatureCollection Initialize(IConfiguration configuration) public IFeatureCollection Initialize(IConfiguration configuration)
@ -48,7 +51,7 @@ namespace Microsoft.AspNet.Server.Kestrel
try try
{ {
var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>(); var information = (KestrelServerInformation)serverFeatures.Get<IKestrelServerInformation>();
var engine = new KestrelEngine(_libraryManager, _appShutdownService); var engine = new KestrelEngine(_libraryManager, _appShutdownService, _logger);
disposables.Push(engine); disposables.Push(engine);

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Server.Kestrel.Http; using Microsoft.AspNet.Server.Kestrel.Http;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
namespace Microsoft.AspNet.Server.Kestrel namespace Microsoft.AspNet.Server.Kestrel
@ -11,5 +12,7 @@ namespace Microsoft.AspNet.Server.Kestrel
public IApplicationShutdown AppShutdown { get; set; } public IApplicationShutdown AppShutdown { get; set; }
public IMemoryPool Memory { get; set; } public IMemoryPool Memory { get; set; }
public IKestrelTrace Log { get; set; }
} }
} }

View File

@ -8,6 +8,7 @@
"dependencies": { "dependencies": {
"Microsoft.AspNet.Hosting": "1.0.0-*", "Microsoft.AspNet.Hosting": "1.0.0-*",
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*", "Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
"Microsoft.Framework.Logging.Abstractions": "1.0.0-*",
"Microsoft.StandardsPolice": { "Microsoft.StandardsPolice": {
"version": "1.0.0-*", "version": "1.0.0-*",
"type": "build" "type": "build"

View File

@ -86,7 +86,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void EngineCanStartAndStop() public void EngineCanStartAndStop()
{ {
var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
engine.Start(1); engine.Start(1);
engine.Dispose(); engine.Dispose();
} }
@ -94,7 +94,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void ListenerCanCreateAndDispose() public void ListenerCanCreateAndDispose()
{ {
var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
engine.Start(1); engine.Start(1);
var started = engine.CreateServer("http", "localhost", 54321, App); var started = engine.CreateServer("http", "localhost", 54321, App);
started.Dispose(); started.Dispose();
@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void ConnectionCanReadAndWrite() public void ConnectionCanReadAndWrite()
{ {
var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
engine.Start(1); engine.Start(1);
var started = engine.CreateServer("http", "localhost", 54321, App); var started = engine.CreateServer("http", "localhost", 54321, App);

View File

@ -4,6 +4,7 @@ using System.Net.Sockets;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel; using Microsoft.AspNet.Server.Kestrel;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Runtime.Infrastructure; using Microsoft.Dnx.Runtime.Infrastructure;
@ -13,10 +14,11 @@ namespace Microsoft.AspNet.Server.KestrelTests
{ {
public class MultipleLoopTests public class MultipleLoopTests
{ {
Libuv _uv; private readonly Libuv _uv;
private readonly IKestrelTrace _logger = new KestrelTrace(new TestLogger());
public MultipleLoopTests() public MultipleLoopTests()
{ {
var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
_uv = engine.Libuv; _uv = engine.Libuv;
} }
@ -41,8 +43,8 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void InitAndCloseServerPipe() public void InitAndCloseServerPipe()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
var pipe = new UvPipeHandle(); var pipe = new UvPipeHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
pipe.Init(loop, true); pipe.Init(loop, true);
@ -59,15 +61,15 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void ServerPipeListenForConnections() public void ServerPipeListenForConnections()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
var serverListenPipe = new UvPipeHandle(); var serverListenPipe = new UvPipeHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
serverListenPipe.Init(loop, false); serverListenPipe.Init(loop, false);
serverListenPipe.Bind(@"\\.\pipe\ServerPipeListenForConnections"); serverListenPipe.Bind(@"\\.\pipe\ServerPipeListenForConnections");
serverListenPipe.Listen(128, (_1, status, error, _2) => serverListenPipe.Listen(128, (_1, status, error, _2) =>
{ {
var serverConnectionPipe = new UvPipeHandle(); var serverConnectionPipe = new UvPipeHandle(_logger);
serverConnectionPipe.Init(loop, true); serverConnectionPipe.Init(loop, true);
try try
{ {
@ -79,7 +81,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
return; return;
} }
var writeRequest = new UvWriteReq(); var writeRequest = new UvWriteReq(new KestrelTrace(new TestLogger()));
writeRequest.Init(loop); writeRequest.Init(loop);
writeRequest.Write( writeRequest.Write(
serverConnectionPipe, serverConnectionPipe,
@ -96,9 +98,9 @@ namespace Microsoft.AspNet.Server.KestrelTests
var worker = new Thread(() => var worker = new Thread(() =>
{ {
var loop2 = new UvLoopHandle(); var loop2 = new UvLoopHandle(_logger);
var clientConnectionPipe = new UvPipeHandle(); var clientConnectionPipe = new UvPipeHandle(_logger);
var connect = new UvConnectRequest(); var connect = new UvConnectRequest(new KestrelTrace(new TestLogger()));
loop2.Init(_uv); loop2.Init(_uv);
clientConnectionPipe.Init(loop2, true); clientConnectionPipe.Init(loop2, true);
@ -134,19 +136,19 @@ namespace Microsoft.AspNet.Server.KestrelTests
{ {
var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n"); var pipeName = @"\\.\pipe\ServerPipeDispatchConnections" + Guid.NewGuid().ToString("n");
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var serverConnectionPipe = default(UvPipeHandle); var serverConnectionPipe = default(UvPipeHandle);
var serverConnectionPipeAcceptedEvent = new ManualResetEvent(false); var serverConnectionPipeAcceptedEvent = new ManualResetEvent(false);
var serverConnectionTcpDisposedEvent = new ManualResetEvent(false); var serverConnectionTcpDisposedEvent = new ManualResetEvent(false);
var serverListenPipe = new UvPipeHandle(); var serverListenPipe = new UvPipeHandle(_logger);
serverListenPipe.Init(loop, false); serverListenPipe.Init(loop, false);
serverListenPipe.Bind(pipeName); serverListenPipe.Bind(pipeName);
serverListenPipe.Listen(128, (_1, status, error, _2) => serverListenPipe.Listen(128, (_1, status, error, _2) =>
{ {
serverConnectionPipe = new UvPipeHandle(); serverConnectionPipe = new UvPipeHandle(_logger);
serverConnectionPipe.Init(loop, true); serverConnectionPipe.Init(loop, true);
try try
{ {
@ -161,18 +163,18 @@ namespace Microsoft.AspNet.Server.KestrelTests
} }
}, null); }, null);
var serverListenTcp = new UvTcpHandle(); var serverListenTcp = new UvTcpHandle(_logger);
serverListenTcp.Init(loop); serverListenTcp.Init(loop);
serverListenTcp.Bind(new IPEndPoint(0, 54321)); serverListenTcp.Bind(new IPEndPoint(0, 54321));
serverListenTcp.Listen(128, (_1, status, error, _2) => serverListenTcp.Listen(128, (_1, status, error, _2) =>
{ {
var serverConnectionTcp = new UvTcpHandle(); var serverConnectionTcp = new UvTcpHandle(_logger);
serverConnectionTcp.Init(loop); serverConnectionTcp.Init(loop);
serverListenTcp.Accept(serverConnectionTcp); serverListenTcp.Accept(serverConnectionTcp);
serverConnectionPipeAcceptedEvent.WaitOne(); serverConnectionPipeAcceptedEvent.WaitOne();
var writeRequest = new UvWriteReq(); var writeRequest = new UvWriteReq(new KestrelTrace(new TestLogger()));
writeRequest.Init(loop); writeRequest.Init(loop);
writeRequest.Write2( writeRequest.Write2(
serverConnectionPipe, serverConnectionPipe,
@ -192,9 +194,9 @@ namespace Microsoft.AspNet.Server.KestrelTests
var worker = new Thread(() => var worker = new Thread(() =>
{ {
var loop2 = new UvLoopHandle(); var loop2 = new UvLoopHandle(_logger);
var clientConnectionPipe = new UvPipeHandle(); var clientConnectionPipe = new UvPipeHandle(_logger);
var connect = new UvConnectRequest(); var connect = new UvConnectRequest(new KestrelTrace(new TestLogger()));
loop2.Init(_uv); loop2.Init(_uv);
clientConnectionPipe.Init(loop2, true); clientConnectionPipe.Init(loop2, true);
@ -216,7 +218,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
clientConnectionPipe.Dispose(); clientConnectionPipe.Dispose();
return; return;
} }
var clientConnectionTcp = new UvTcpHandle(); var clientConnectionTcp = new UvTcpHandle(_logger);
clientConnectionTcp.Init(loop2); clientConnectionTcp.Init(loop2);
clientConnectionPipe.Accept(clientConnectionTcp); clientConnectionPipe.Accept(clientConnectionTcp);
var buf2 = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64); var buf2 = loop2.Libuv.buf_init(Marshal.AllocHGlobal(64), 64);

View File

@ -7,6 +7,7 @@ using System.Net.Sockets;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Server.Kestrel; using Microsoft.AspNet.Server.Kestrel;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Dnx.Runtime.Infrastructure; using Microsoft.Dnx.Runtime.Infrastructure;
@ -19,10 +20,11 @@ namespace Microsoft.AspNet.Server.KestrelTests
/// </summary> /// </summary>
public class NetworkingTests public class NetworkingTests
{ {
Libuv _uv; private readonly Libuv _uv;
private readonly IKestrelTrace _logger = new KestrelTrace(new TestLogger());
public NetworkingTests() public NetworkingTests()
{ {
var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
_uv = engine.Libuv; _uv = engine.Libuv;
} }
@ -47,7 +49,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void LoopCanBeInitAndClose() public void LoopCanBeInitAndClose()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
loop.Run(); loop.Run();
loop.Dispose(); loop.Dispose();
@ -56,9 +58,9 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void AsyncCanBeSent() public void AsyncCanBeSent()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var trigger = new UvAsyncHandle(); var trigger = new UvAsyncHandle(_logger);
var called = false; var called = false;
trigger.Init(loop, () => trigger.Init(loop, () =>
{ {
@ -74,9 +76,9 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public void SocketCanBeInitAndClose() public void SocketCanBeInitAndClose()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var tcp = new UvTcpHandle(); var tcp = new UvTcpHandle(_logger);
tcp.Init(loop); tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 0)); tcp.Bind(new IPEndPoint(IPAddress.Loopback, 0));
tcp.Dispose(); tcp.Dispose();
@ -88,14 +90,14 @@ namespace Microsoft.AspNet.Server.KestrelTests
[Fact] [Fact]
public async Task SocketCanListenAndAccept() public async Task SocketCanListenAndAccept()
{ {
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var tcp = new UvTcpHandle(); var tcp = new UvTcpHandle(_logger);
tcp.Init(loop); tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321)); tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Listen(10, (stream, status, error, state) => tcp.Listen(10, (stream, status, error, state) =>
{ {
var tcp2 = new UvTcpHandle(); var tcp2 = new UvTcpHandle(_logger);
tcp2.Init(loop); tcp2.Init(loop);
stream.Accept(tcp2); stream.Accept(tcp2);
tcp2.Dispose(); tcp2.Dispose();
@ -125,15 +127,15 @@ namespace Microsoft.AspNet.Server.KestrelTests
public async Task SocketCanRead() public async Task SocketCanRead()
{ {
int bytesRead = 0; int bytesRead = 0;
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var tcp = new UvTcpHandle(); var tcp = new UvTcpHandle(_logger);
tcp.Init(loop); tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321)); tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Listen(10, (_, status, error, state) => tcp.Listen(10, (_, status, error, state) =>
{ {
Console.WriteLine("Connected"); Console.WriteLine("Connected");
var tcp2 = new UvTcpHandle(); var tcp2 = new UvTcpHandle(_logger);
tcp2.Init(loop); tcp2.Init(loop);
tcp.Accept(tcp2); tcp.Accept(tcp2);
var data = Marshal.AllocCoTaskMem(500); var data = Marshal.AllocCoTaskMem(500);
@ -181,15 +183,15 @@ namespace Microsoft.AspNet.Server.KestrelTests
public async Task SocketCanReadAndWrite() public async Task SocketCanReadAndWrite()
{ {
int bytesRead = 0; int bytesRead = 0;
var loop = new UvLoopHandle(); var loop = new UvLoopHandle(_logger);
loop.Init(_uv); loop.Init(_uv);
var tcp = new UvTcpHandle(); var tcp = new UvTcpHandle(_logger);
tcp.Init(loop); tcp.Init(loop);
tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321)); tcp.Bind(new IPEndPoint(IPAddress.Loopback, 54321));
tcp.Listen(10, (_, status, error, state) => tcp.Listen(10, (_, status, error, state) =>
{ {
Console.WriteLine("Connected"); Console.WriteLine("Connected");
var tcp2 = new UvTcpHandle(); var tcp2 = new UvTcpHandle(_logger);
tcp2.Init(loop); tcp2.Init(loop);
tcp.Accept(tcp2); tcp.Accept(tcp2);
var data = Marshal.AllocCoTaskMem(500); var data = Marshal.AllocCoTaskMem(500);
@ -206,7 +208,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
{ {
for (var x = 0; x != 2; ++x) for (var x = 0; x != 2; ++x)
{ {
var req = new UvWriteReq(); var req = new UvWriteReq(new KestrelTrace(new TestLogger()));
req.Init(loop); req.Init(loop);
req.Write( req.Write(
tcp2, tcp2,

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Threading; using System.Threading;
using Microsoft.AspNet.Server.Kestrel; using Microsoft.AspNet.Server.Kestrel;
using Microsoft.AspNet.Server.Kestrel.Http; using Microsoft.AspNet.Server.Kestrel.Http;
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
using Microsoft.AspNet.Server.Kestrel.Networking; using Microsoft.AspNet.Server.Kestrel.Networking;
using Microsoft.AspNet.Server.KestrelTests.TestHelpers; using Microsoft.AspNet.Server.KestrelTests.TestHelpers;
using Xunit; using Xunit;
@ -31,13 +32,14 @@ namespace Microsoft.AspNet.Server.KestrelTests
} }
}; };
using (var kestrelEngine = new KestrelEngine(mockLibuv, new ShutdownNotImplemented())) using (var kestrelEngine = new KestrelEngine(mockLibuv, new ShutdownNotImplemented(), new TestLogger()))
{ {
kestrelEngine.Start(count: 1); kestrelEngine.Start(count: 1);
var kestrelThread = kestrelEngine.Threads[0]; var kestrelThread = kestrelEngine.Threads[0];
var socket = new MockSocket(kestrelThread.Loop.ThreadId); var socket = new MockSocket(kestrelThread.Loop.ThreadId, new KestrelTrace(new TestLogger()));
var socketOutput = new SocketOutput(kestrelThread, socket); var trace = new KestrelTrace(new TestLogger());
var socketOutput = new SocketOutput(kestrelThread, socket, trace);
// I doubt _maxBytesPreCompleted will ever be over a MB. If it is, we should change this test. // I doubt _maxBytesPreCompleted will ever be over a MB. If it is, we should change this test.
var bufferSize = 1048576; var bufferSize = 1048576;
@ -75,13 +77,14 @@ namespace Microsoft.AspNet.Server.KestrelTests
} }
}; };
using (var kestrelEngine = new KestrelEngine(mockLibuv, new ShutdownNotImplemented())) using (var kestrelEngine = new KestrelEngine(mockLibuv, new ShutdownNotImplemented(), new TestLogger()))
{ {
kestrelEngine.Start(count: 1); kestrelEngine.Start(count: 1);
var kestrelThread = kestrelEngine.Threads[0]; var kestrelThread = kestrelEngine.Threads[0];
var socket = new MockSocket(kestrelThread.Loop.ThreadId); var socket = new MockSocket(kestrelThread.Loop.ThreadId, new KestrelTrace(new TestLogger()));
var socketOutput = new SocketOutput(kestrelThread, socket); var trace = new KestrelTrace(new TestLogger());
var socketOutput = new SocketOutput(kestrelThread, socket, trace);
var bufferSize = maxBytesPreCompleted; var bufferSize = maxBytesPreCompleted;
var buffer = new ArraySegment<byte>(new byte[bufferSize], 0, bufferSize); var buffer = new ArraySegment<byte>(new byte[bufferSize], 0, bufferSize);
@ -115,7 +118,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
private class MockSocket : UvStreamHandle private class MockSocket : UvStreamHandle
{ {
public MockSocket(int threadId) public MockSocket(int threadId, IKestrelTrace logger) : base(logger)
{ {
// Set the handle to something other than IntPtr.Zero // Set the handle to something other than IntPtr.Zero
// so handle.Validate doesn't fail in Libuv.write // so handle.Validate doesn't fail in Libuv.write

View File

@ -0,0 +1,23 @@
using System;
using Microsoft.AspNet.Server.Kestrel;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Server.KestrelTests
{
public class TestLogger : ILogger
{
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
}
public bool IsEnabled(LogLevel logLevel)
{
return false;
}
public IDisposable BeginScopeImpl(object state)
{
return new Disposable(() => { });
}
}
}

View File

@ -45,7 +45,7 @@ namespace Microsoft.AspNet.Server.KestrelTests
public void Create(Func<Frame, Task> app) public void Create(Func<Frame, Task> app)
{ {
_engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented()); _engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented(), new TestLogger());
_engine.Start(1); _engine.Start(1);
_server = _engine.CreateServer( _server = _engine.CreateServer(
"http", "http",