mirror of https://github.com/dotnet/tye.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.1 KiB
77 lines
2.1 KiB
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System;
|
|
|
|
namespace Microsoft.Extensions.Logging.Testing
|
|
{
|
|
public class TestLogger : ILogger
|
|
{
|
|
private object _scope;
|
|
private readonly ITestSink _sink;
|
|
private readonly string _name;
|
|
private readonly Func<LogLevel, bool> _filter;
|
|
|
|
public TestLogger(string name, ITestSink sink, bool enabled)
|
|
: this(name, sink, _ => enabled)
|
|
{
|
|
}
|
|
|
|
public TestLogger(string name, ITestSink sink, Func<LogLevel, bool> filter)
|
|
{
|
|
_sink = sink;
|
|
_name = name;
|
|
_filter = filter;
|
|
}
|
|
|
|
public string Name { get; set; }
|
|
|
|
public IDisposable BeginScope<TState>(TState state)
|
|
{
|
|
_scope = state;
|
|
|
|
_sink.Begin(new BeginScopeContext()
|
|
{
|
|
LoggerName = _name,
|
|
Scope = state,
|
|
});
|
|
|
|
return TestDisposable.Instance;
|
|
}
|
|
|
|
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
|
{
|
|
if (!IsEnabled(logLevel))
|
|
{
|
|
return;
|
|
}
|
|
|
|
_sink.Write(new WriteContext()
|
|
{
|
|
LogLevel = logLevel,
|
|
EventId = eventId,
|
|
State = state,
|
|
Exception = exception,
|
|
Formatter = (s, e) => formatter((TState)s, e),
|
|
LoggerName = _name,
|
|
Scope = _scope
|
|
});
|
|
}
|
|
|
|
public bool IsEnabled(LogLevel logLevel)
|
|
{
|
|
return logLevel != LogLevel.None && _filter(logLevel);
|
|
}
|
|
|
|
private class TestDisposable : IDisposable
|
|
{
|
|
public static readonly TestDisposable Instance = new TestDisposable();
|
|
|
|
public void Dispose()
|
|
{
|
|
// intentionally does nothing
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|