Last active
December 12, 2015 05:18
-
-
Save loudej/4720114 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Diagnostics; | |
using System.IO; | |
namespace KatanaApplication33 | |
{ | |
public class Example | |
{ | |
private ILogger _logger; | |
public void Usage() | |
{ | |
string one = "1"; | |
string two = "2"; | |
_logger.Warning("Simple message"); | |
if (_logger.IsWarningEnabled()) | |
{ | |
_logger.Warning(string.Format("Lazy message no closure {0} {1}", one, two)); | |
} | |
_logger.Warning(() => string.Format("Lazy message with closure {0} {1}", one, two)); | |
try | |
{ | |
File.Delete("boom"); | |
} | |
catch (Exception ex) | |
{ | |
_logger.Warning("Error deleting file", ex); | |
} | |
} | |
} | |
public interface ILogger | |
{ | |
/// <summary> | |
/// Fundamental Log method | |
/// </summary> | |
/// <param name="traceEvent">Severity level</param> | |
/// <param name="messageFormatter">Turn state and exception into string, | |
/// if messageFormatter==null Log will return "IsEnabled" bool only</param> | |
/// <param name="state">Caller state to pass into messageFormatter</param> | |
/// <param name="exception">Caller exception to pass into messageFormatter</param> | |
/// <returns>true if event type enabled, false if not</returns> | |
bool Log( | |
TraceEventType traceEvent, | |
Func<object, Exception, string> messageFormatter, | |
object state, | |
Exception exception); | |
} | |
public class SampleLogger : ILogger | |
{ | |
public bool Log( | |
TraceEventType traceEvent, | |
Func<object, Exception, string> messageFormatter, | |
object state, | |
Exception exception) | |
{ | |
if (!IsEnabled(traceEvent)) | |
{ | |
// let the caller know this level is disabled, if it cares | |
return false; | |
} | |
if (messageFormatter == null) | |
{ | |
// let the caller know it should call back with a message | |
return true; | |
} | |
// format and send the message | |
var message = messageFormatter(state, exception); | |
Console.WriteLine(message); | |
// and let the caller know the level is enabled, if it cares | |
return true; | |
} | |
private bool IsEnabled(TraceEventType traceEvent) | |
{ | |
// return true if the level is enabled, | |
// false if the callback should be not be invoked, or if the caller | |
// should not bother producing the real message call | |
return true; | |
} | |
} | |
public static class LoggerExtensions | |
{ | |
private static readonly Func<object, Exception, string> NoCallbackProvidedDelegate = NoCallbackProvided; | |
private static string NoCallbackProvided(object state, Exception exception) | |
{ | |
if (state == null) | |
{ | |
return exception == null ? null : exception.Message; | |
} | |
return exception == null ? state.ToString() : state + ": " + exception.Message; | |
} | |
public static bool IsWarningEnabled(this ILogger logger) | |
{ | |
return logger.Log(TraceEventType.Warning, null, null, null); | |
} | |
public static bool Warning(this ILogger logger, string message) | |
{ | |
return logger.Log(TraceEventType.Warning, NoCallbackProvidedDelegate, message, null); | |
} | |
public static bool Warning(this ILogger logger, Exception exception) | |
{ | |
return logger.Log(TraceEventType.Warning, NoCallbackProvidedDelegate, null, exception); | |
} | |
public static bool Warning(this ILogger logger, string message, Exception exception) | |
{ | |
return logger.Log(TraceEventType.Warning, NoCallbackProvidedDelegate, message, exception); | |
} | |
public static bool Warning(this ILogger logger, Func<object, Exception, string> message, object state, Exception exception) | |
{ | |
return logger.Log(TraceEventType.Warning, message, state, exception); | |
} | |
public static bool Warning(this ILogger logger, Func<string> message) | |
{ | |
return logger.Log(TraceEventType.Warning, (_, __) => message(), null, null); | |
} | |
public static bool Warning(this ILogger logger, Func<string> message, Exception exception) | |
{ | |
return logger.Log(TraceEventType.Warning, (_, __) => message(), null, exception); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment