Ocelot/test/Ocelot.UnitTests/Logging/AspDotNetLoggerTests.cs
Tom Pallister efbb950ea2
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
2018-04-07 12:03:24 +01:00

82 lines
2.8 KiB
C#

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);
}
}
}