Created
April 29, 2015 03:00
-
-
Save dbuksbaum/5b45b0c1cadb646beba3 to your computer and use it in GitHub Desktop.
How To Do Logging with Caliburn.Micro
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
class DebugLogger : ILog | |
{ | |
#region Fields | |
private readonly Type _type; | |
#endregion | |
#region Constructors | |
public DebugLogger(Type type) | |
{ | |
_type = type; | |
} | |
#endregion | |
#region Helper Methods | |
private string CreateLogMessage(string format, params object[] args) | |
{ | |
return string.Format("[{0}] {1}", | |
DateTime.Now.ToString("o"), | |
string.Format(format, args)); | |
} | |
#endregion | |
#region ILog Members | |
public void Error(Exception exception) | |
{ | |
Debug.WriteLine(CreateLogMessage(exception.ToString()), "ERROR"); | |
} | |
public void Info(string format, params object[] args) | |
{ | |
Debug.WriteLine(CreateLogMessage(format, args), "INFO"); | |
} | |
public void Warn(string format, params object[] args) | |
{ | |
Debug.WriteLine(CreateLogMessage(format, args), "WARN"); | |
} | |
#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
/// <summary> | |
// A logger. | |
// </summary> | |
public interface ILog | |
{ | |
/// <summary> | |
/// Logs the message as info. | |
/// </summary> | |
/// <param name="format">A formatted message.</param> | |
/// <param name="args">Parameters to be injected into the formatted message.</param> | |
void Info(string format, params object[] args); | |
/// <summary> | |
/// Logs the message as a warning. | |
/// </summary> | |
/// <param name="format">A formatted message.</param> | |
/// <param name="args">Parameters to be injected into the formatted message.</param> | |
void Warn(string format, params object[] args); | |
/// <summary> | |
/// Logs the exception. | |
/// </summary> | |
/// <param name="exception">The exception.</param> | |
void Error(Exception exception); | |
} |
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
<configSections> | |
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> | |
</configSections> | |
<log4net> | |
<appender name="TraceAppender" type="log4net.Appender.TraceAppender"> | |
<layout type="log4net.Layout.PatternLayout"> | |
<conversionPattern value="%date [%thread] %-5level - %message%newline" /> | |
</layout> | |
</appender> | |
<appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" > | |
<layout type="log4net.Layout.PatternLayout"> | |
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> | |
</layout> | |
</appender> | |
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > | |
<layout type="log4net.Layout.PatternLayout"> | |
<conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" /> | |
</layout> | |
</appender> | |
<root> | |
<level value="DEBUG" /> | |
<appender-ref ref="TraceAppender" /> | |
</root> | |
</log4net> |
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
class Log4netLogger : ILog | |
{ | |
#region Fields | |
private readonly log4net.ILog _innerLogger; | |
#endregion | |
#region Constructors | |
public Log4netLogger(Type type) | |
{ | |
_innerLogger = log4net.LogManager.GetLogger(type); | |
} | |
#endregion | |
#region ILog Members | |
public void Error(Exception exception) | |
{ | |
_innerLogger.Error(exception.Message, exception); | |
} | |
public void Info(string format, params object[] args) | |
{ | |
_innerLogger.InfoFormat(format, args); | |
} | |
public void Warn(string format, params object[] args) | |
{ | |
_innerLogger.WarnFormat(format, args); | |
} | |
#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
/// <summary> | |
/// Used to manage logging. | |
/// </summary> | |
public static class LogManager | |
{ | |
static readonly ILog NullLogInstance = new NullLog(); | |
/// <summary> | |
/// Creates an <see cref="ILog"/> for the provided type. | |
/// </summary> | |
public static Func<Type, ILog> GetLog = type => NullLogInstance; | |
private class NullLog : ILog | |
{ | |
public void Info(string format, params object[] args) { } | |
public void Warn(string format, params object[] args) { } | |
public void Error(Exception exception) { } | |
} | |
} |
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 class MyBootStrapper : Bootstrapper<ShellViewModel> | |
{ | |
static MyBootStrapper() | |
{ | |
LogManager.GetLog = type => new DebugLogger(type); | |
} | |
} |
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
<configSections> | |
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/> | |
</configSections> | |
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<targets> | |
<target name="tr" xsi:type="Trace"/> | |
</targets> | |
<rules> | |
<logger name="*" writeTo="tr" /> | |
</rules> | |
</nlog> |
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
class NLogLogger : ILog | |
{ | |
#region Fields | |
private readonly NLog.Logger _innerLogger; | |
#endregion | |
#region Constructors | |
public NLogLogger(Type type) | |
{ | |
_innerLogger = NLog.LogManager.GetLogger(type.Name); | |
} | |
#endregion | |
#region ILog Members | |
public void Error(Exception exception) | |
{ | |
_innerLogger.ErrorException(exception.Message, exception); | |
} | |
public void Info(string format, params object[] args) | |
{ | |
_innerLogger.Info(format, args); | |
} | |
public void Warn(string format, params object[] args) | |
{ | |
_innerLogger.Warn(format, args); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment