-
-
Save MiloszKrajewski/1ce82c560a750c79463b1a574a28a5e6 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.Runtime.CompilerServices; | |
namespace Pocket.Logging | |
{ | |
public static class LoggerFactoryExtensions | |
{ | |
[MethodImpl(MethodImplOptions.NoInlining)] | |
public static ILogger Logger(this ILoggerFactory factory) => | |
factory.Logger(new StackTrace().GetFrame(1).GetMethod().DeclaringType); | |
public static ILogger Logger(this ILoggerFactory factory, Type type) => | |
factory.Logger(type.FullName); | |
public static ILogger Logger<T>(this ILoggerFactory factory) => | |
factory.Logger(typeof(T)); | |
} | |
public static class LoggerExtensions | |
{ | |
public static void Log( | |
this ILogger logger, Severity severity, string message) => | |
logger.Log(severity, () => message); | |
public static void Log( | |
this ILogger logger, Severity severity, string pattern, params object[] args) => | |
logger.Log(severity, () => string.Format(pattern, args)); | |
public static void Log( | |
this ILogger logger, Severity severity, Exception error) => | |
logger.Log(severity, error.ToString); | |
} | |
} |
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; | |
namespace Pocket.Logging | |
{ | |
public enum Severity | |
{ | |
Trace, Debug, Info, Warn, Error, Fatal | |
} | |
public interface ILogger | |
{ | |
void Log(Severity severity, Func<string> builder); | |
} | |
public interface ILoggerFactory | |
{ | |
ILogger Logger(string name); | |
} | |
} |
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; | |
namespace Pocket.Logging | |
{ | |
public static class MoreLoggerExtensions | |
{ | |
#region Trace | |
public static void Trace(this ILogger logger, string message) => | |
logger.Log(Severity.Trace, message); | |
public static void Trace(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Trace, builder); | |
public static void Trace( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Trace, pattern, args); | |
public static void Trace(this ILogger logger, Exception error) => | |
logger.Log(Severity.Trace, error); | |
#endregion | |
#region Debug | |
public static void Debug(this ILogger logger, string message) => | |
logger.Log(Severity.Debug, message); | |
public static void Debug(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Debug, builder); | |
public static void Debug( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Debug, pattern, args); | |
public static void Debug(this ILogger logger, Exception error) => | |
logger.Log(Severity.Debug, error); | |
#endregion | |
#region Info | |
public static void Info(this ILogger logger, string message) => | |
logger.Log(Severity.Info, message); | |
public static void Info(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Info, builder); | |
public static void Info( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Info, pattern, args); | |
public static void Info(this ILogger logger, Exception error) => | |
logger.Log(Severity.Info, error); | |
#endregion | |
#region Warn | |
public static void Warn(this ILogger logger, string message) => | |
logger.Log(Severity.Warn, message); | |
public static void Warn(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Warn, builder); | |
public static void Warn( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Warn, pattern, args); | |
public static void Warn(this ILogger logger, Exception error) => | |
logger.Log(Severity.Warn, error); | |
#endregion | |
#region Error | |
public static void Error(this ILogger logger, string message) => | |
logger.Log(Severity.Error, message); | |
public static void Error(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Error, builder); | |
public static void Error( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Error, pattern, args); | |
public static void Error(this ILogger logger, Exception error) => | |
logger.Log(Severity.Error, error); | |
#endregion | |
#region Fatal | |
public static void Fatal(this ILogger logger, string message) => | |
logger.Log(Severity.Fatal, message); | |
public static void Fatal(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.Fatal, builder); | |
public static void Fatal( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.Fatal, pattern, args); | |
public static void Fatal(this ILogger logger, Exception error) => | |
logger.Log(Severity.Fatal, error); | |
#endregion | |
} | |
} |
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
<#@ template debug="false" hostspecific="false" language="C#" #> | |
<#@ assembly name="System.Core" #> | |
<#@ output extension=".cs" #> | |
using System; | |
namespace Pocket.Logging | |
{ | |
public static class MoreLoggerExtensions | |
{ | |
<# foreach (var level in new[] { "Trace", "Debug", "Info", "Warn", "Error", "Fatal" }) { #> | |
#region <#= level #> | |
public static void <#= level #>(this ILogger logger, string message) => | |
logger.Log(Severity.<#= level #>, message); | |
public static void <#= level #>(this ILogger logger, Func<string> builder) => | |
logger.Log(Severity.<#= level #>, builder); | |
public static void <#= level #>( | |
this ILogger logger, string pattern, params object[] args) => | |
logger.Log(Severity.<#= level #>, pattern, args); | |
public static void <#= level #>(this ILogger logger, Exception error) => | |
logger.Log(Severity.<#= level #>, error); | |
#endregion | |
<# LF(level != "Fatal"); } #> | |
} | |
} | |
<#+ void LF(bool condition = true) { if (condition) WriteLine(""); } #> |
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 NLog; | |
namespace Pocket.Logging.OO.NLog | |
{ | |
public class NLogLoggerFactory: ILoggerFactory | |
{ | |
public ILogger Logger(string name) => | |
new NLogLogger(LogManager.GetLogger(name)); | |
} | |
public class NLogLogger: ILogger | |
{ | |
private readonly Logger _logger; | |
public NLogLogger(Logger logger) { _logger = logger; } | |
public void Log(Severity severity, Func<string> builder) | |
{ | |
var level = ToLogLevel(severity); | |
if (_logger.IsEnabled(level)) | |
_logger.Log(level, builder()); | |
} | |
private static LogLevel ToLogLevel(Severity severity) | |
{ | |
switch (severity) | |
{ | |
case Severity.Trace: return LogLevel.Trace; | |
case Severity.Debug: return LogLevel.Debug; | |
case Severity.Info: return LogLevel.Info; | |
case Severity.Warn: return LogLevel.Warn; | |
case Severity.Error: return LogLevel.Error; | |
case Severity.Fatal: return LogLevel.Fatal; | |
default: return LogLevel.Off; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment