Created
November 27, 2020 14:17
-
-
Save andrewstellman/842d66448a7ba490ecb755049f9840d1 to your computer and use it in GitHub Desktop.
PoorMansLog (taking a joke tweet way to seriously)
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
// https://twitter.com/AndrewStellman/status/1332042223987388420 | |
using System; | |
using System.Collections.Generic; | |
using Microsoft.Extensions.Logging; | |
namespace LogTest | |
{ | |
public class PoorMansLog : ILogger, IDisposable | |
{ | |
private readonly List<string> _lines = new List<string>(); | |
private string _scope = "None"; | |
public IDisposable BeginScope<TState>(TState state) | |
{ | |
_scope = state?.ToString() ?? "None"; | |
return this; | |
} | |
public void Dispose() | |
{ | |
_lines.Add($"Ending scope ${_scope}"); | |
_scope = "None"; | |
} | |
public bool IsEnabled(LogLevel logLevel) => true; | |
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, | |
Exception exception, Func<TState, Exception, string> formatter) => | |
_lines.Add($"{DateTime.Now} [{logLevel}] - {eventId} {state}"); | |
public void GetLog() => string.Join(Environment.NewLine, _lines); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment