Last active
May 10, 2016 14:59
-
-
Save mbcrawfo/eff6e8fd1c93861dbc9373636fdd2276 to your computer and use it in GitHub Desktop.
NLog in Xamarin.Forms
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
public interface ILogger | |
{ | |
void Trace(string msg, params object[] args); | |
void Trace(Exception ex, string msg, params object[] args); | |
void Trace(IFormatProvider fmt, string msg, params object[] args); | |
void Trace(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
void Debug(string msg, params object[] args); | |
void Debug(Exception ex, string msg, params object[] args); | |
void Debug(IFormatProvider fmt, string msg, params object[] args); | |
void Debug(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
void Info(string msg, params object[] args); | |
void Info(Exception ex, string msg, params object[] args); | |
void Info(IFormatProvider fmt, string msg, params object[] args); | |
void Info(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
void Warn(string msg, params object[] args); | |
void Warn(Exception ex, string msg, params object[] args); | |
void Warn(IFormatProvider fmt, string msg, params object[] args); | |
void Warn(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
void Error(string msg, params object[] args); | |
void Error(Exception ex, string msg, params object[] args); | |
void Error(IFormatProvider fmt, string msg, params object[] args); | |
void Error(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
void Fatal(string msg, params object[] args); | |
void Fatal(Exception ex, string msg, params object[] args); | |
void Fatal(IFormatProvider fmt, string msg, params object[] args); | |
void Fatal(Exception ex, IFormatProvider fmt, string msg, params object[] args); | |
} | |
public enum LogLevel | |
{ | |
Trace, | |
Debug, | |
Warn, | |
Error, | |
Fatal | |
} |
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
public interface ILogManager | |
{ | |
ILogger GetLogger(Type type); | |
ILogger GetLogger<T>(); | |
} |
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
public sealed class LogManager | |
: ILogManager | |
{ | |
private static readonly Lazy<LogManager> instance = | |
new Lazy<LogManager>(() => new LogManager()); | |
public static LogManager Instance { get { return instance.Value; } } | |
private LogManager() | |
{ | |
} | |
#region ILogManager Implementation | |
public ILogger GetLogger(Type type) | |
{ | |
var log = NLog.LogManager.GetLogger(type.FullName); | |
return new NLogLoggerWrapper(log); | |
} | |
public ILogger GetLogger<T>() | |
{ | |
return GetLogger(typeof(T)); | |
} | |
#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
public sealed class NLogLoggerWrapper | |
: MyNameSpace.Interfaces.ILogger | |
{ | |
private NLog.ILogger m_log; | |
public NLogLoggerWrapper(NLog.ILogger log) | |
{ | |
if (log == null) | |
throw new ArgumentNullException("log"); | |
m_log = log; | |
} | |
#region ILogger implementation | |
public void Trace(string msg, params object[] args) | |
{ | |
if (m_log.IsTraceEnabled) | |
{ | |
WriteMessage(LogLevel.Trace, null, msg, args, null); | |
} | |
} | |
public void Trace(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsTraceEnabled) | |
{ | |
WriteMessage(LogLevel.Trace, null, msg, args, ex); | |
} | |
} | |
public void Trace(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsTraceEnabled) | |
{ | |
WriteMessage(LogLevel.Trace, fmt, msg, args, null); | |
} | |
} | |
public void Trace(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsTraceEnabled) | |
{ | |
WriteMessage(LogLevel.Trace, fmt, msg, args, ex); | |
} | |
} | |
public void Debug(string msg, params object[] args) | |
{ | |
if (m_log.IsDebugEnabled) | |
{ | |
WriteMessage(LogLevel.Debug, null, msg, args, null); | |
} | |
} | |
public void Debug(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsDebugEnabled) | |
{ | |
WriteMessage(LogLevel.Debug, null, msg, args, ex); | |
} | |
} | |
public void Debug(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsDebugEnabled) | |
{ | |
WriteMessage(LogLevel.Debug, fmt, msg, args, null); | |
} | |
} | |
public void Debug(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsDebugEnabled) | |
{ | |
WriteMessage(LogLevel.Debug, fmt, msg, args, ex); | |
} | |
} | |
public void Info(string msg, params object[] args) | |
{ | |
if (m_log.IsInfoEnabled) | |
{ | |
WriteMessage(LogLevel.Info, null, msg, args, null); | |
} | |
} | |
public void Info(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsInfoEnabled) | |
{ | |
WriteMessage(LogLevel.Info, null, msg, args, ex); | |
} | |
} | |
public void Info(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsInfoEnabled) | |
{ | |
WriteMessage(LogLevel.Info, fmt, msg, args, null); | |
} | |
} | |
public void Info(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsInfoEnabled) | |
{ | |
WriteMessage(LogLevel.Info, fmt, msg, args, ex); | |
} | |
} | |
public void Warn(string msg, params object[] args) | |
{ | |
if (m_log.IsWarnEnabled) | |
{ | |
WriteMessage(LogLevel.Warn, null, msg, args, null); | |
} | |
} | |
public void Warn(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsWarnEnabled) | |
{ | |
WriteMessage(LogLevel.Warn, null, msg, args, ex); | |
} | |
} | |
public void Warn(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsWarnEnabled) | |
{ | |
WriteMessage(LogLevel.Warn, fmt, msg, args, null); | |
} | |
} | |
public void Warn(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsWarnEnabled) | |
{ | |
WriteMessage(LogLevel.Warn, fmt, msg, args, ex); | |
} | |
} | |
public void Error(string msg, params object[] args) | |
{ | |
if (m_log.IsErrorEnabled) | |
{ | |
WriteMessage(LogLevel.Error, null, msg, args, null); | |
} | |
} | |
public void Error(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsErrorEnabled) | |
{ | |
WriteMessage(LogLevel.Error, null, msg, args, ex); | |
} | |
} | |
public void Error(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsErrorEnabled) | |
{ | |
WriteMessage(LogLevel.Error, fmt, msg, args, null); | |
} | |
} | |
public void Error(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsErrorEnabled) | |
{ | |
WriteMessage(LogLevel.Error, fmt, msg, args, ex); | |
} | |
} | |
public void Fatal(string msg, params object[] args) | |
{ | |
if (m_log.IsFatalEnabled) | |
{ | |
WriteMessage(LogLevel.Fatal, null, msg, args, null); | |
} | |
} | |
public void Fatal(Exception ex, string msg, params object[] args) | |
{ | |
if (m_log.IsFatalEnabled) | |
{ | |
WriteMessage(LogLevel.Fatal, null, msg, args, ex); | |
} | |
} | |
public void Fatal(IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsFatalEnabled) | |
{ | |
WriteMessage(LogLevel.Fatal, fmt, msg, args, null); | |
} | |
} | |
public void Fatal(Exception ex, IFormatProvider fmt, string msg, params object[] args) | |
{ | |
if (m_log.IsFatalEnabled) | |
{ | |
WriteMessage(LogLevel.Fatal, fmt, msg, args, ex); | |
} | |
} | |
#endregion | |
private void WriteMessage(LogLevel level, IFormatProvider formatProvider, | |
string message, object[] parameters, Exception exception) | |
{ | |
var evt = new LogEventInfo(level, m_log.Name, formatProvider, | |
message, parameters, exception); | |
m_log.Log(typeof(NLogLoggerWrapper), evt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment