Feature/another look at logging (#303)

* #212 - hacked websockets proxy together

* faffing around

* #212 hacking away :(

* #212 websockets proxy middleware working

* #212 map when for webockets working

* #212 some test refactor

* #212 temp commit

* #212 websockets proxy working, tests passing...need to do some tidying and write docs

* #212 more code coverage

* #212 docs for websockets

* #212 updated readme

* #212 tidying up after websockets refactoring

* #212 tidying up after websockets refactoring

* #212 tidying up after websockets refactoring

* changing logging levels and logging like ms reccomends with structured data rather than strings

* more faffing

* more fafin

* #287 ocelot logger now only takes strings as it did take params then just turned them to strings, misleading, unit tests for logger and diagnosticlogger

* #287 errors now logged as they happen

* #287 more detail for logs requested in issue

* #287 tidy up

* #287 renamed

* #287 always log context id

* #287 fixed me being an idiot

* #287 removed crap websockets unit test that isnt a unit test

* #287 removed crap websockets unit test that isnt a unit test
This commit is contained in:
Tom Pallister
2018-04-07 12:03:24 +01:00
committed by GitHub
parent 0c380239a9
commit efbb950ea2
60 changed files with 600 additions and 387 deletions

View File

@ -0,0 +1,81 @@
namespace Ocelot.UnitTests.Logging
{
using Moq;
using Xunit;
using Ocelot.Logging;
using Microsoft.Extensions.Logging;
using Ocelot.Infrastructure.RequestData;
using System;
public class AspDotNetLoggerTests
{
private readonly Mock<ILogger<object>> _coreLogger;
private readonly AspDotNetLogger _logger;
private Mock<IRequestScopedDataRepository> _repo;
private readonly string _b;
private readonly string _a;
private readonly Exception _ex;
public AspDotNetLoggerTests()
{
_a = "tom";
_b = "laura";
_ex = new Exception("oh no");
_coreLogger = new Mock<ILogger<object>>();
_repo = new Mock<IRequestScopedDataRepository>();
_logger = new AspDotNetLogger(_coreLogger.Object, _repo.Object);
}
[Fact]
public void should_log_trace()
{
_logger.LogTrace($"a message from {_a} to {_b}");
ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Trace);
}
[Fact]
public void should_log_info()
{
_logger.LogInformation($"a message from {_a} to {_b}");
ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Information);
}
[Fact]
public void should_log_warning()
{
_logger.LogWarning($"a message from {_a} to {_b}");
ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura", LogLevel.Warning);
}
[Fact]
public void should_log_error()
{
_logger.LogError($"a message from {_a} to {_b}", _ex);
ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura, exception: System.Exception: oh no", LogLevel.Error);
}
[Fact]
public void should_log_critical()
{
_logger.LogCritical($"a message from {_a} to {_b}", _ex);
ThenLevelIsLogged("requestId: no request id, previousRequestId: no previous request id, message: a message from tom to laura, exception: System.Exception: oh no", LogLevel.Critical);
}
private void ThenLevelIsLogged(string expected, LogLevel expectedLogLevel)
{
_coreLogger.Verify(
x => x.Log(
expectedLogLevel,
It.IsAny<EventId>(),
It.Is<object>(o => o.ToString() == expected),
It.IsAny<Exception>(),
It.IsAny<Func<object, Exception, string>>()), Times.Once);
}
}
}

View File

@ -0,0 +1,145 @@
using Ocelot.Logging;
using Moq;
using TestStack.BDDfy;
using Butterfly.Client.Tracing;
using Ocelot.Requester;
using Xunit;
using Ocelot.Middleware;
using Microsoft.AspNetCore.Http;
using System;
namespace Ocelot.UnitTests.Logging
{
public class OcelotDiagnosticListenerTests
{
private readonly OcelotDiagnosticListener _listener;
private Mock<IOcelotLoggerFactory> _factory;
private readonly Mock<IOcelotLogger> _logger;
private IServiceTracer _tracer;
private DownstreamContext _downstreamContext;
private string _name;
private Exception _exception;
public OcelotDiagnosticListenerTests()
{
_factory = new Mock<IOcelotLoggerFactory>();
_logger = new Mock<IOcelotLogger>();
_tracer = new FakeServiceTracer();
_factory.Setup(x => x.CreateLogger<OcelotDiagnosticListener>()).Returns(_logger.Object);
_listener = new OcelotDiagnosticListener(_factory.Object, _tracer);
}
[Fact]
public void should_trace_ocelot_middleware_started()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.When(_ => WhenOcelotMiddlewareStartedCalled())
.Then(_ => ThenTheLogIs($"Ocelot.MiddlewareStarted: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
.BDDfy();
}
[Fact]
public void should_trace_ocelot_middleware_finished()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.When(_ => WhenOcelotMiddlewareFinishedCalled())
.Then(_ => ThenTheLogIs($"Ocelot.MiddlewareFinished: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
.BDDfy();
}
[Fact]
public void should_trace_ocelot_middleware_exception()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.And(_ => GivenAException(new Exception("oh no")))
.When(_ => WhenOcelotMiddlewareExceptionCalled())
.Then(_ => ThenTheLogIs($"Ocelot.MiddlewareException: {_name}; {_exception.Message};"))
.BDDfy();
}
[Fact]
public void should_trace_middleware_started()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.When(_ => WhenMiddlewareStartedCalled())
.Then(_ => ThenTheLogIs($"MiddlewareStarting: {_name}; {_downstreamContext.HttpContext.Request.Path}"))
.BDDfy();
}
[Fact]
public void should_trace_middleware_finished()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.When(_ => WhenMiddlewareFinishedCalled())
.Then(_ => ThenTheLogIs($"MiddlewareFinished: {_name}; {_downstreamContext.HttpContext.Response.StatusCode}"))
.BDDfy();
}
[Fact]
public void should_trace_middleware_exception()
{
this.Given(_ => GivenAMiddlewareName())
.And(_ => GivenAContext())
.And(_ => GivenAException(new Exception("oh no")))
.When(_ => WhenMiddlewareExceptionCalled())
.Then(_ => ThenTheLogIs($"MiddlewareException: {_name}; {_exception.Message};"))
.BDDfy();
}
private void GivenAException(Exception exception)
{
_exception = exception;
}
private void WhenOcelotMiddlewareStartedCalled()
{
_listener.OcelotMiddlewareStarted(_downstreamContext, _name);
}
private void WhenOcelotMiddlewareFinishedCalled()
{
_listener.OcelotMiddlewareFinished(_downstreamContext, _name);
}
private void WhenOcelotMiddlewareExceptionCalled()
{
_listener.OcelotMiddlewareException(_exception, _downstreamContext, _name);
}
private void WhenMiddlewareStartedCalled()
{
_listener.OnMiddlewareStarting(_downstreamContext.HttpContext, _name);
}
private void WhenMiddlewareFinishedCalled()
{
_listener.OnMiddlewareFinished(_downstreamContext.HttpContext, _name);
}
private void WhenMiddlewareExceptionCalled()
{
_listener.OnMiddlewareException(_exception, _name);
}
private void GivenAContext()
{
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
}
private void GivenAMiddlewareName()
{
_name = "name";
}
private void ThenTheLogIs(string expected)
{
_logger.Verify(
x => x.LogTrace(expected));
}
}
}