btcpayserver/BTCPayServer.Tests/Logging/Logs.cs

81 lines
1.9 KiB
C#
Raw Normal View History

2017-09-13 08:47:34 +02:00
using System;
using System.Text;
2020-06-28 10:55:27 +02:00
using Microsoft.Extensions.Logging;
2017-09-13 08:47:34 +02:00
using Xunit.Abstractions;
namespace BTCPayServer.Tests.Logging
{
public interface ILog
{
void LogInformation(string msg);
}
2017-09-13 08:47:34 +02:00
public class XUnitLogProvider : ILoggerProvider
{
readonly ITestOutputHelper _Helper;
public XUnitLogProvider(ITestOutputHelper helper)
{
_Helper = helper;
}
public ILogger CreateLogger(string categoryName)
{
return new XUnitLog(_Helper) { Name = categoryName };
}
2017-09-13 08:47:34 +02:00
public void Dispose()
{
2017-09-13 08:47:34 +02:00
}
}
public class XUnitLog : ILog, ILogger, IDisposable
{
readonly ITestOutputHelper _Helper;
public XUnitLog(ITestOutputHelper helper)
{
_Helper = helper;
}
2017-09-13 08:47:34 +02:00
public string Name
{
get; set;
}
2017-09-13 08:47:34 +02:00
public IDisposable BeginScope<TState>(TState state)
{
return this;
}
2017-09-13 08:47:34 +02:00
public void Dispose()
{
2017-09-13 08:47:34 +02:00
}
2017-09-13 08:47:34 +02:00
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
2017-09-13 08:47:34 +02:00
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
StringBuilder builder = new StringBuilder();
builder.Append(formatter(state, exception));
if (exception != null)
{
builder.AppendLine();
builder.Append(exception.ToString());
}
LogInformation(builder.ToString());
}
public void LogInformation(string msg)
{
if (msg != null)
try
{
_Helper.WriteLine(DateTimeOffset.UtcNow + " :" + Name + ": " + msg);
}
catch { }
}
}
2017-09-13 08:47:34 +02:00
}