Skip to content

Instantly share code, notes, and snippets.

@sergiorykov
Created January 3, 2016 22:25
Show Gist options
  • Save sergiorykov/17830e22c90a30e863ae to your computer and use it in GitHub Desktop.
Save sergiorykov/17830e22c90a30e863ae to your computer and use it in GitHub Desktop.
public sealed class LibLogXUnitLogger : ILogProvider
{
private readonly ITestOutputHelper _output;
public LibLogXUnitLogger(ITestOutputHelper output)
{
// based on https://github.com/damianh/LibLog/blob/master/src/LibLog.Example.ColoredConsoleLogProvider/ColoredConsoleLogProvider.cs
_output = output;
}
public Logger GetLogger(string name)
{
return (logLevel, messageFunc, exception, formatParameters) =>
{
if (messageFunc == null)
{
// verifies logging level is enabled
return true;
}
string message = string.Format(CultureInfo.InvariantCulture, messageFunc(), formatParameters);
string record = string.Format(
CultureInfo.InvariantCulture,
"{0} {1} {2}",
DateTime.Now,
logLevel,
message);
if (exception != null)
{
record = string.Format(
CultureInfo.InvariantCulture,
"{0}:{1}{2}",
record,
Environment.NewLine,
exception);
}
_output.WriteLine(record);
return true;
};
}
public IDisposable OpenMappedContext(string key, string value)
{
return NullDisposable.Instance;
}
public IDisposable OpenNestedContext(string message)
{
return NullDisposable.Instance;
}
private class NullDisposable : IDisposable
{
internal static readonly IDisposable Instance = new NullDisposable();
public void Dispose()
{ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment