Log requests into test logs, override directory targets (#98)

This commit is contained in:
Pavel Krymets 2017-08-29 10:28:45 -07:00 committed by GitHub
parent 51ac8d53f4
commit 4601db9bf6
4 changed files with 19 additions and 7 deletions

View File

@ -0,0 +1,2 @@
<Project>
</Project>

View File

@ -11,31 +11,31 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests
{
public class LoggingInterceptor : IServiceClientTracingInterceptor
{
private readonly ILogger _logger;
private readonly ILogger _globalLogger;
public LoggingInterceptor(ILogger logger)
public LoggingInterceptor(ILogger globalLogger)
{
_logger = logger;
_globalLogger = globalLogger;
}
public void Information(string message)
{
_logger.LogInformation(message);
CurrentLogger.LogInformation(message);
}
public void TraceError(string invocationId, Exception exception)
{
_logger.LogInformation(exception, "Exception in {invocationId}", invocationId);
CurrentLogger.LogInformation(exception, "Exception in {invocationId}", invocationId);
}
public void ReceiveResponse(string invocationId, HttpResponseMessage response)
{
_logger.LogInformation(response.AsFormattedString());
CurrentLogger.LogInformation(response.AsFormattedString());
}
public void SendRequest(string invocationId, HttpRequestMessage request)
{
_logger.LogInformation(request.AsFormattedString());
CurrentLogger.LogInformation(request.AsFormattedString());
}
public void Configuration(string source, string name, string value) { }
@ -43,5 +43,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests
public void EnterMethod(string invocationId, object instance, string method, IDictionary<string, object> parameters) { }
public void ExitMethod(string invocationId, object returnValue) { }
private ILogger CurrentLogger => TestLogger.Current ?? _globalLogger;
}
}

View File

@ -10,6 +10,7 @@
<ContentWithTargetPath Include="NuGet.config.template" CopyToOutputDirectory="PreserveNewest" TargetPath="NuGet.config" />
<ContentWithTargetPath Include="global.json.template" CopyToOutputDirectory="PreserveNewest" TargetPath="global.json" />
<ContentWithTargetPath Include="Directory.Build.props.template" CopyToOutputDirectory="PreserveNewest" TargetPath="Directory.Build.props" />
<ContentWithTargetPath Include="Directory.Build.targets.template" CopyToOutputDirectory="PreserveNewest" TargetPath="Directory.Build.targets" />
</ItemGroup>
<ItemGroup>

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests
@ -12,8 +13,13 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests
private readonly ILogger _logger;
private static readonly AsyncLocal<TestLogger> _currentLogger = new AsyncLocal<TestLogger>();
public static TestLogger Current => _currentLogger.Value;
public TestLogger(ILoggerFactory factory, ILogger logger)
{
_currentLogger.Value = this;
_factory = factory;
_logger = logger;
}
@ -35,6 +41,7 @@ namespace Microsoft.AspNetCore.AzureAppServices.FunctionalTests
public void Dispose()
{
_currentLogger.Value = null;
_factory.Dispose();
}
}