-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Capture server logs in tests (#2029)
- Loading branch information
Showing
12 changed files
with
263 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<Project> | ||
<Project> | ||
<Import Project="dependencies.props" /> | ||
|
||
<PropertyGroup> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 87 additions & 35 deletions
122
test/Microsoft.AspNetCore.SignalR.Client.FunctionalTests/HubConnectionTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
test/Microsoft.AspNetCore.SignalR.Tests.Utils/ServerLogScope.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Tests | ||
{ | ||
public class ServerLogScope : IDisposable | ||
{ | ||
private readonly ServerFixture _serverFixture; | ||
private readonly ILoggerFactory _loggerFactory; | ||
private readonly IDisposable _wrappedDisposable; | ||
private readonly ConcurrentDictionary<string, ILogger> _serverLoggers; | ||
private readonly ILogger _scopeLogger; | ||
private readonly object _lock; | ||
|
||
public ServerLogScope(ServerFixture serverFixture, ILoggerFactory loggerFactory, IDisposable wrappedDisposable) | ||
{ | ||
_loggerFactory = loggerFactory; | ||
_serverFixture = serverFixture; | ||
_wrappedDisposable = wrappedDisposable; | ||
|
||
_lock = new object(); | ||
|
||
_serverLoggers = new ConcurrentDictionary<string, ILogger>(StringComparer.Ordinal); | ||
_scopeLogger = _loggerFactory.CreateLogger(nameof(ServerLogScope)); | ||
|
||
// Attach last after everything else is initialized because a logged error can happen at any time | ||
_serverFixture.ServerLogged += ServerFixtureOnServerLogged; | ||
|
||
_scopeLogger.LogInformation("Server log scope started."); | ||
} | ||
|
||
private void ServerFixtureOnServerLogged(LogRecord logRecord) | ||
{ | ||
var write = logRecord.Write; | ||
|
||
if (write == null) | ||
{ | ||
_scopeLogger.LogWarning("Server log has no data."); | ||
return; | ||
} | ||
|
||
ILogger logger; | ||
|
||
// There maybe thready safety issues in logging when creating multiple loggers at the same time | ||
// https://github.com/aspnet/Logging/issues/810 | ||
lock (_lock) | ||
{ | ||
// Create (or get) a logger with the same name as the server logger | ||
logger = _serverLoggers.GetOrAdd(write.LoggerName, loggerName => _loggerFactory.CreateLogger(loggerName)); | ||
} | ||
|
||
logger.Log(write.LogLevel, write.EventId, write.State, write.Exception, write.Formatter); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_serverFixture.ServerLogged -= ServerFixtureOnServerLogged; | ||
|
||
_scopeLogger.LogInformation("Server log scope stopped."); | ||
|
||
_wrappedDisposable?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
test/Microsoft.AspNetCore.SignalR.Tests.Utils/VerifiableServerLoggedTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// 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. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Testing; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Tests | ||
{ | ||
public class VerifiableServerLoggedTest : VerifiableLoggedTest | ||
{ | ||
public ServerFixture ServerFixture { get; } | ||
|
||
public VerifiableServerLoggedTest(ServerFixture serverFixture, ITestOutputHelper output) : base(output) | ||
{ | ||
ServerFixture = serverFixture; | ||
} | ||
|
||
public override IDisposable StartVerifableLog(out ILoggerFactory loggerFactory, LogLevel minLogLevel, [CallerMemberName] string testName = null, Func<WriteContext, bool> expectedErrorsFilter = null) | ||
{ | ||
var disposable = base.StartVerifableLog(out loggerFactory, minLogLevel, testName, expectedErrorsFilter); | ||
return new ServerLogScope(ServerFixture, loggerFactory, disposable); | ||
} | ||
|
||
public override IDisposable StartVerifableLog(out ILoggerFactory loggerFactory, [CallerMemberName] string testName = null, Func<WriteContext, bool> expectedErrorsFilter = null) | ||
{ | ||
var disposable = base.StartVerifableLog(out loggerFactory, testName, expectedErrorsFilter); | ||
return new ServerLogScope(ServerFixture, loggerFactory, disposable); | ||
} | ||
} | ||
} |
Oops, something went wrong.