Skip to content

Instantly share code, notes, and snippets.

@cchamberlain
Last active March 26, 2021 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cchamberlain/1efe938cee1b70372bb8 to your computer and use it in GitHub Desktop.
Save cchamberlain/1efe938cee1b70372bb8 to your computer and use it in GitHub Desktop.
ILog.cs / Log.cs - NLog service wrapper
using System;
using System.Threading;
using NLog;
using App.Diagnostics;
namespace App {
public abstract class ExampleBase {
private static readonly Lazy<ILogger> GenericLogger = new Lazy<ILogger>(LogManager.GetCurrentClassLogger, LazyThreadSafetyMode.PublicationOnly);
protected ILog Log { get; private set; }
/// <summary>
/// Optionally allows subclasses to pass in an NLog ILogger instance or will otherwise use the Lazy initialized static logger for this class
/// </summary>
/// <param name="logger">An NLog ILogger class instance.</param>
protected ExampleBase(ILogger logger = null) { Log = new Log(logger ?? GenericLogger.Value); }
}
}
using NLog;
using App.Diagnostics;
namespace App {
public abstract class ExampleConcrete : ExampleBase {
public ExampleConcrete() : base(LogManager.GetCurrentClassLogger()) {
// This will only execute the log message when LogLevel debugging is enabled in NLog config.
Log.Debug(() => "Constructed!!!");
}
}
}
using System;
using System.Runtime.CompilerServices;
namespace App.Diagnostics {
public interface ILog {
/// <summary>
/// Records trace info if enabled. Accepts an optional operation that returns a string message to save processing if trace logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Trace(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records debug info if enabled. Accepts an optional operation that returns a string message to save processing if debug logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Debug(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records info if enabled. Accepts an optional operation that returns a string message to save processing if info logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Info(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records warn info if enabled. Accepts an operation that returns a string message to save processing if warn logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Warn(Func<string> message, Exception ex = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records warn info if enabled for an exception. Ignore the parameters, they are received by reflection.
/// </summary>
void Warn(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records error info if enabled. Accepts an operation that returns a string message to save processing if error logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Error(Func<string> message, Exception ex = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records error info if enabled for an exception. Ignore the parameters, they are received by reflection.
/// </summary>
void Error(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records fatal info if enabled. Accepts an operation that returns a string message to save processing if fatal logging is disabled. Ignore the other parameters, they are received by reflection.
/// </summary>
void Fatal(Func<string> message, Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
/// <summary>
/// Records fatal info if enabled for an exception. Ignore the parameters, they are received by reflection.
/// </summary>
void Fatal(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = 0);
}
}
using System;
using System.Runtime.CompilerServices;
using NLog;
namespace App.Diagnostics {
public class Log : ILog {
private readonly ILogger _logger;
public Log(ILogger logger) { _logger = logger; }
public void Trace(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsTraceEnabled) _logger.Trace(FormatLog(message?.Invoke(), memberName, filePath, lineNumber));
}
public void Debug(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsDebugEnabled) _logger.Debug(FormatLog(message?.Invoke(), memberName, filePath, lineNumber));
}
public void Info(Func<string> message = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsInfoEnabled) _logger.Info(FormatLog(message?.Invoke(), memberName, filePath, lineNumber));
}
public void Warn(Func<string> message, Exception ex = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsWarnEnabled) _logger.Warn(ex, FormatLog(message(), memberName, filePath, lineNumber));
}
public void Warn(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsWarnEnabled) _logger.Warn(ex, FormatLog(null, memberName, filePath, lineNumber));
}
public void Error(Func<string> message, Exception ex = null, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsErrorEnabled) _logger.Error(ex, FormatLog(message(), memberName, filePath, lineNumber));
}
public void Error(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsErrorEnabled) _logger.Error(ex, FormatLog(null, memberName, filePath, lineNumber));
}
public void Fatal(Func<string> message, Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsFatalEnabled) _logger.Fatal(ex, FormatLog(message(), memberName, filePath, lineNumber));
}
public void Fatal(Exception ex, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int lineNumber = default(int)) {
if (_logger.IsFatalEnabled) _logger.Fatal(ex, FormatLog(null, memberName, filePath, lineNumber));
}
/// <summary>
/// Can be overridden to allow specification of a custom log format.
/// </summary>
protected virtual string FormatLog(string message, string memberName = null, string filePath = null, int lineNumber = default(int))
=> $"{(message == null ? string.Empty : $"{message} | ")}Member => {memberName} | FilePath => {filePath} | LineNumber => {lineNumber}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment