Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Move logging to new style (remove allocs) #326

Merged
merged 1 commit into from
Nov 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/Microsoft.AspNet.Server.Kestrel/Infrastructure/KestrelTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,48 @@ namespace Microsoft.AspNet.Server.Kestrel
/// </summary>
public class KestrelTrace : IKestrelTrace
{
private static Action<ILogger, long, Exception> _connectionStart;
private static Action<ILogger, long, Exception> _connectionStop;
private static Action<ILogger, long, Exception> _connectionPause;
private static Action<ILogger, long, Exception> _connectionResume;
private static Action<ILogger, long, Exception> _connectionReadFin;
private static Action<ILogger, long, Exception> _connectionWriteFin;
private static Action<ILogger, long, int, Exception> _connectionWroteFin;
private static Action<ILogger, long, Exception> _connectionKeepAlive;
private static Action<ILogger, long, Exception> _connectionDisconnect;

protected readonly ILogger _logger;

static KestrelTrace()
{
_connectionStart = LoggerMessage.Define<long>(LogLevel.Debug, 1, @"Connection id ""{ConnectionId}"" started.");
_connectionStop = LoggerMessage.Define<long>(LogLevel.Debug, 2, @"Connection id ""{ConnectionId}"" stopped.");
// ConnectionRead: Reserved: 3
_connectionPause = LoggerMessage.Define<long>(LogLevel.Debug, 4, @"Connection id ""{ConnectionId}"" paused.");
_connectionResume = LoggerMessage.Define<long>(LogLevel.Debug, 5, @"Connection id ""{ConnectionId}"" resumed.");
_connectionReadFin = LoggerMessage.Define<long>(LogLevel.Debug, 6, @"Connection id ""{ConnectionId}"" received FIN.");
_connectionWriteFin = LoggerMessage.Define<long>(LogLevel.Debug, 7, @"Connection id ""{ConnectionId}"" sending FIN.");
_connectionWroteFin = LoggerMessage.Define<long, int>(LogLevel.Debug, 8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}"".");
_connectionKeepAlive = LoggerMessage.Define<long>(LogLevel.Debug, 9, @"Connection id ""{ConnectionId}"" completed keep alive response.");
_connectionDisconnect = LoggerMessage.Define<long>(LogLevel.Error, 10, @"Connection id ""{ConnectionId}"" disconnected.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should ApplicationError also use this patern?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd imagine so; but would need the following overload (as Exception is is a built in param)?

static Action<ILogger, Exception> Define(LogLevel logLevel, int eventId, string formatString)

in Microsoft.Extensions.Logging.Abstractions/LoggerMessage.cs which doesn't currently exist.

// ConnectionWrite: Reserved: 11
// ConnectionWriteCallback: Reserved: 12
// ApplicationError: Reserved: 13 - LoggerMessage.Define overload not present
}

public KestrelTrace(ILogger logger)
{
_logger = logger;
}

public virtual void ConnectionStart(long connectionId)
{
_logger.LogDebug(1, @"Connection id ""{ConnectionId}"" started.", connectionId);
_connectionStart(_logger, connectionId, null);
}

public virtual void ConnectionStop(long connectionId)
{
_logger.LogDebug(2, @"Connection id ""{ConnectionId}"" stopped.", connectionId);
_connectionStop(_logger, connectionId, null);
}

public virtual void ConnectionRead(long connectionId, int count)
Expand All @@ -37,37 +64,37 @@ public virtual void ConnectionRead(long connectionId, int count)

public virtual void ConnectionPause(long connectionId)
{
_logger.LogDebug(4, @"Connection id ""{ConnectionId}"" paused.", connectionId);
_connectionPause(_logger, connectionId, null);
}

public virtual void ConnectionResume(long connectionId)
{
_logger.LogDebug(5, @"Connection id ""{ConnectionId}"" resumed.", connectionId);
_connectionResume(_logger, connectionId, null);
}

public virtual void ConnectionReadFin(long connectionId)
{
_logger.LogDebug(6, @"Connection id ""{ConnectionId}"" received FIN.", connectionId);
_connectionReadFin(_logger, connectionId, null);
}

public virtual void ConnectionWriteFin(long connectionId)
{
_logger.LogDebug(7, @"Connection id ""{ConnectionId}"" sending FIN.", connectionId);
_connectionWriteFin(_logger, connectionId, null);
}

public virtual void ConnectionWroteFin(long connectionId, int status)
{
_logger.LogDebug(8, @"Connection id ""{ConnectionId}"" sent FIN with status ""{Status}"".", connectionId, status);
_connectionWroteFin(_logger, connectionId, status, null);
}

public virtual void ConnectionKeepAlive(long connectionId)
{
_logger.LogDebug(9, @"Connection id ""{ConnectionId}"" completed keep alive response.", connectionId);
_connectionKeepAlive(_logger, connectionId, null);
}

public virtual void ConnectionDisconnect(long connectionId)
{
_logger.LogDebug(10, @"Connection id ""{ConnectionId}"" disconnected.", connectionId);
_connectionDisconnect(_logger, connectionId, null);
}

public virtual void ConnectionWrite(long connectionId, int count)
Expand Down
4 changes: 2 additions & 2 deletions test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,12 +904,12 @@ private class TestApplicationErrorLogger : ILogger

public IDisposable BeginScopeImpl(object state)
{
throw new NotImplementedException();
return new Disposable(() => { });
}

public bool IsEnabled(LogLevel logLevel)
{
throw new NotImplementedException();
return true;
}

public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
Expand Down