-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using Microsoft.Extensions.Logging; | ||
using PicoProfiler.Logging; | ||
using PicoProfiler.Tests.TestLogger; | ||
|
||
namespace PicoProfiler.Tests; | ||
|
||
public class PicoProfilerLoggerTests | ||
{ | ||
[Fact] | ||
public void Outputs_To_Log() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler()) | ||
{ | ||
} | ||
|
||
Assert.Equal(1, fixture.LogEntries.Count); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Provided_LogLevel() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler(logLevel: LogLevel.Debug)) | ||
{ | ||
} | ||
|
||
Assert.Equal(LogLevel.Debug, fixture.LogEntries.Single().Level); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Default_ActionName() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler()) | ||
{ | ||
} | ||
|
||
Assert.Contains(nameof(Uses_Default_ActionName), fixture.LogEntries.Single().Message); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Provided_ActionName() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler("fetching")) | ||
{ | ||
} | ||
|
||
Assert.Contains("fetching", fixture.LogEntries.Single().Message); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Configured_ActionName() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
LoggerOutputConfiguration.Instance.DefaultLogLevel = LogLevel.Critical; | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler()) | ||
{ | ||
} | ||
|
||
Assert.Equal(LogLevel.Critical, fixture.LogEntries.Single().Level); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Default_Message() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler()) | ||
{ | ||
} | ||
|
||
Assert.StartsWith($"{nameof(Uses_Default_Message)} finished in", fixture.LogEntries.Single().Message); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Provided_Message() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler(messageFactory: (name, time) => ("action {act} done!", new object[]{name}))) | ||
{ | ||
} | ||
|
||
Assert.Equal($"action {nameof(Uses_Provided_Message)} done!", fixture.LogEntries.Single().Message); | ||
} | ||
|
||
[Fact] | ||
public void Uses_Configured_Message() | ||
{ | ||
var fixture = new TestLoggerFixture(); | ||
LoggerOutputConfiguration.Instance.DefaultActionMessageFactory = | ||
(name, time) => ("action {act} done!", new object[] { name }); | ||
|
||
var logger = fixture.CreateLogger(); | ||
using (logger.StartProfiler()) | ||
{ | ||
} | ||
|
||
Assert.Equal($"action {nameof(Uses_Configured_Message)} done!", fixture.LogEntries.Single().Message); | ||
} | ||
} |
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,22 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace PicoProfiler.Tests.TestLogger; | ||
|
||
internal class TestLogger : ILogger | ||
{ | ||
private readonly TestLoggerProvider _provider; | ||
|
||
public TestLogger(TestLoggerProvider provider) | ||
{ | ||
_provider = provider; | ||
} | ||
|
||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
{ | ||
_provider.Add(logLevel, formatter(state, exception)); | ||
} | ||
|
||
public bool IsEnabled(LogLevel logLevel) => true; | ||
|
||
public IDisposable BeginScope<TState>(TState state) => default; | ||
Check warning on line 21 in src/PicoProfiler.Tests/TestLogger/TestLogger.cs GitHub Actions / build
|
||
} |
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,32 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace PicoProfiler.Tests.TestLogger; | ||
|
||
internal class TestLoggerFixture | ||
{ | ||
private readonly TestLoggerProvider _provider = new(); | ||
private ILoggerFactory _factory = null!; | ||
|
||
public IReadOnlyCollection<TestLoggerProvider.LogEntry> LogEntries | ||
{ | ||
get | ||
{ | ||
// disposing of factory is the only way to flush. | ||
RecreateFactory(); | ||
return _provider.LogEntries.AsReadOnly(); | ||
} | ||
} | ||
|
||
public TestLoggerFixture() | ||
{ | ||
RecreateFactory(); | ||
} | ||
|
||
private void RecreateFactory() | ||
{ | ||
_factory?.Dispose(); | ||
_factory = LoggerFactory.Create(x => x.SetMinimumLevel(LogLevel.Trace).AddProvider(_provider)); | ||
} | ||
|
||
public ILogger CreateLogger() => _factory.CreateLogger<TestLoggerFixture>(); | ||
} |
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,21 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace PicoProfiler.Tests.TestLogger; | ||
|
||
internal class TestLoggerProvider : ILoggerProvider | ||
{ | ||
public record struct LogEntry(LogLevel Level, string Message); | ||
|
||
public List<LogEntry> LogEntries { get; } = new(); | ||
|
||
public void Add(LogLevel level, string message) => LogEntries.Add(new LogEntry(level, message)); | ||
|
||
public void Dispose() | ||
{ | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new TestLogger(this); | ||
} | ||
} |