Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Exception logging #5

Closed
wants to merge 1 commit into from

3 participants

@Tasteful

Add possibility to log message + exception.

@Tasteful Tasteful Exception logging
Add possibility to log message + exception.
8b3fc48
@ferventcoder

Why would you pass an Exception to a debug, info or warn? Curious on that as it is a code smell for me.

@Tasteful

That's something i'm not have think about earlier, it should be enough to have the possibility for error and fatal level.

@ShaneCourtrille

Agreed that having exception logging for error or fatal would be nice.

@ferventcoder
Owner

I'm unlikely to pull this in as it sits now. Not a fan of passing the exception to anything more than error and fatal.

@ferventcoder
Owner

I implemented something similar to this, but only on Error and Fatal. 4a468d2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 6, 2013
  1. @Tasteful

    Exception logging

    Tasteful authored
    Add possibility to log message + exception.
This page is out of date. Refresh to see the latest.
View
35 LoggingExtensions.Core/Logging/ILog.cs
@@ -14,6 +14,13 @@ public interface ILog
void InitializeFor(string loggerName);
/// <summary>
+ /// Debug level of the specified message and exception.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ /// <param name="exception">The exception.</param>
+ void Debug(string message, Exception exception);
+
+ /// <summary>
/// Debug level of the specified message. The other method is preferred since the execution is deferred.
/// </summary>
/// <param name="message">The message.</param>
@@ -27,6 +34,13 @@ public interface ILog
void Debug(Func<string> message);
/// <summary>
+ /// Info level of the specified message and exception.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ /// <param name="exception">The exception.</param>
+ void Info(string message, Exception exception);
+
+ /// <summary>
/// Info level of the specified message. The other method is preferred since the execution is deferred.
/// </summary>
/// <param name="message">The message.</param>
@@ -40,6 +54,13 @@ public interface ILog
void Info(Func<string> message);
/// <summary>
+ /// Warn level of the specified message and exception.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ /// <param name="exception">The exception.</param>
+ void Warn(string message, Exception exception);
+
+ /// <summary>
/// Warn level of the specified message. The other method is preferred since the execution is deferred.
/// </summary>
/// <param name="message">The message.</param>
@@ -53,6 +74,13 @@ public interface ILog
void Warn(Func<string> message);
/// <summary>
+ /// Error level of the specified message and exception.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ /// <param name="exception">The exception.</param>
+ void Error(string message, Exception exception);
+
+ /// <summary>
/// Error level of the specified message. The other method is preferred since the execution is deferred.
/// </summary>
/// <param name="message">The message.</param>
@@ -66,6 +94,13 @@ public interface ILog
void Error(Func<string> message);
/// <summary>
+ /// Fatal level of the specified message and exception.
+ /// </summary>
+ /// <param name="message">The message.</param>
+ /// <param name="exception">The exception.</param>
+ void Fatal(string message, Exception exception);
+
+ /// <summary>
/// Fatal level of the specified message. The other method is preferred since the execution is deferred.
/// </summary>
/// <param name="message">The message.</param>
View
20 LoggingExtensions.Core/Logging/NullLog.cs
@@ -11,6 +11,10 @@ public void InitializeFor(string loggerName)
{
}
+ public void Debug(string message, Exception exception)
+ {
+ }
+
public void Debug(string message, params object[] formatting)
{
}
@@ -19,6 +23,10 @@ public void Debug(Func<string> message)
{
}
+ public void Info(string message, Exception exception)
+ {
+ }
+
public void Info(string message, params object[] formatting)
{
}
@@ -27,6 +35,10 @@ public void Info(Func<string> message)
{
}
+ public void Warn(string message, Exception exception)
+ {
+ }
+
public void Warn(string message, params object[] formatting)
{
}
@@ -35,6 +47,10 @@ public void Warn(Func<string> message)
{
}
+ public void Error(string message, Exception exception)
+ {
+ }
+
public void Error(string message, params object[] formatting)
{
}
@@ -43,6 +59,10 @@ public void Error(Func<string> message)
{
}
+ public void Fatal(string message, Exception exception)
+ {
+ }
+
public void Fatal(string message, params object[] formatting)
{
}
View
30 LoggingExtensions.Moq/MockLogger.cs
@@ -30,6 +30,11 @@ public IList<string> MessagesFor(LogLevel logLevel)
}
public void InitializeFor(string loggerName) {}
+ public void Debug(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Debug, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ Object.Debug(message, exception);
+ }
public void LogMessage(LogLevel logLevel, string message)
{
@@ -49,6 +54,13 @@ public void Debug(Func<string> message)
Object.Debug(message);
}
+ public void Info(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Info, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ Object.Info(message, exception);
+
+ }
+
public void Info(string message, params object[] formatting)
{
LogMessage(LogLevel.Info, string.Format(message, formatting));
@@ -61,6 +73,12 @@ public void Info(Func<string> message)
Object.Info(message);
}
+ public void Warn(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Warn, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ Object.Warn(message, exception);
+ }
+
public void Warn(string message, params object[] formatting)
{
LogMessage(LogLevel.Warn, string.Format(message, formatting));
@@ -73,6 +91,12 @@ public void Warn(Func<string> message)
Object.Warn(message);
}
+ public void Error(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Error, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ Object.Error(message, exception);
+ }
+
public void Error(string message, params object[] formatting)
{
LogMessage(LogLevel.Error, string.Format(message, formatting));
@@ -85,6 +109,12 @@ public void Error(Func<string> message)
Object.Error(message);
}
+ public void Fatal(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Fatal, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ Object.Fatal(message, exception);
+ }
+
public void Fatal(string message, params object[] formatting)
{
LogMessage(LogLevel.Fatal, string.Format(message, formatting));
View
27 LoggingExtensions.NLog/NLogLog.cs
@@ -17,6 +17,11 @@ public void InitializeFor(string loggerName)
_logger = LogManager.GetLogger(loggerName);
}
+ public void Debug(string message, Exception exception)
+ {
+ if (_logger.IsDebugEnabled) _logger.DebugException(message, exception);
+ }
+
public void Debug(string message, params object[] formatting)
{
if (_logger.IsDebugEnabled) _logger.Debug(message, formatting);
@@ -27,6 +32,11 @@ public void Debug(Func<string> message)
if (_logger.IsDebugEnabled) _logger.Debug(message());
}
+ public void Info(string message, Exception exception)
+ {
+ if (_logger.IsInfoEnabled) _logger.InfoException(message, exception);
+ }
+
public void Info(string message, params object[] formatting)
{
if (_logger.IsInfoEnabled) _logger.Info(message, formatting);
@@ -37,6 +47,11 @@ public void Info(Func<string> message)
if (_logger.IsInfoEnabled) _logger.Info(message());
}
+ public void Warn(string message, Exception exception)
+ {
+ if (_logger.IsWarnEnabled) _logger.WarnException(message, exception);
+ }
+
public void Warn(string message, params object[] formatting)
{
if (_logger.IsWarnEnabled) _logger.Warn(message, formatting);
@@ -47,6 +62,12 @@ public void Warn(Func<string> message)
if (_logger.IsWarnEnabled) _logger.Warn(message());
}
+ public void Error(string message, Exception exception)
+ {
+ // don't check for enabled at this level
+ _logger.ErrorException(message, exception);
+ }
+
public void Error(string message, params object[] formatting)
{
// don't check for enabled at this level
@@ -59,6 +80,12 @@ public void Error(Func<string> message)
_logger.Error(message());
}
+ public void Fatal(string message, Exception exception)
+ {
+ // don't check for enabled at this level
+ _logger.FatalException(message, exception);
+ }
+
public void Fatal(string message, params object[] formatting)
{
// don't check for enabled at this level
View
30 LoggingExtensions.RhinoMocks/MockLogger.cs
@@ -38,6 +38,12 @@ public void InitializeFor(string loggerName)
{
}
+ public void Debug(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Debug, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ _logger.Debug(message, exception);
+ }
+
public void LogMessage(LogLevel logLevel, string message)
{
var list = _messages.Value.GetOrAdd(logLevel.ToString(), new List<string>());
@@ -56,6 +62,12 @@ public void Debug(Func<string> message)
_logger.Debug(message);
}
+ public void Info(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Info, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ _logger.Info(message, exception);
+ }
+
public void Info(string message, params object[] formatting)
{
LogMessage(LogLevel.Info, string.Format(message, formatting));
@@ -68,6 +80,12 @@ public void Info(Func<string> message)
_logger.Info(message);
}
+ public void Warn(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Warn, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ _logger.Warn(message, exception);
+ }
+
public void Warn(string message, params object[] formatting)
{
LogMessage(LogLevel.Warn, string.Format(message, formatting));
@@ -80,6 +98,12 @@ public void Warn(Func<string> message)
_logger.Warn(message);
}
+ public void Error(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Error, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ _logger.Error(message, exception);
+ }
+
public void Error(string message, params object[] formatting)
{
LogMessage(LogLevel.Error, string.Format(message, formatting));
@@ -92,6 +116,12 @@ public void Error(Func<string> message)
_logger.Error(message);
}
+ public void Fatal(string message, Exception exception)
+ {
+ LogMessage(LogLevel.Fatal, string.Concat(message, Environment.NewLine, exception.Message, Environment.NewLine, exception.StackTrace));
+ _logger.Fatal(message, exception);
+ }
+
public void Fatal(string message, params object[] formatting)
{
LogMessage(LogLevel.Fatal, string.Format(message, formatting));
View
32 LoggingExtensions.log4net/Log4NetLog.cs
@@ -23,6 +23,12 @@ public void InitializeFor(string loggerName)
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
+ public void Debug(string message, Exception exception)
+ {
+ if (_logger.IsDebugEnabled) _logger.Debug(message, exception);
+ }
+
+ [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Debug(string message, params object[] formatting)
{
if (_logger.IsDebugEnabled) _logger.DebugFormat(message, formatting);
@@ -35,6 +41,12 @@ public void Debug(Func<string> message)
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
+ public void Info(string message, Exception exception)
+ {
+ if (_logger.IsInfoEnabled) _logger.Info(message, exception);
+ }
+
+ [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Info(string message, params object[] formatting)
{
if (_logger.IsInfoEnabled) _logger.InfoFormat(message, formatting);
@@ -47,6 +59,12 @@ public void Info(Func<string> message)
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
+ public void Warn(string message, Exception exception)
+ {
+ if (_logger.IsWarnEnabled) _logger.Warn(message, exception);
+ }
+
+ [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Warn(string message, params object[] formatting)
{
if (_logger.IsWarnEnabled) _logger.WarnFormat(message, formatting);
@@ -59,6 +77,13 @@ public void Warn(Func<string> message)
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
+ public void Error(string message, Exception exception)
+ {
+ // don't check for enabled at this level
+ _logger.Error(message, exception);
+ }
+
+ [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Error(string message, params object[] formatting)
{
// don't check for enabled at this level
@@ -73,6 +98,13 @@ public void Error(Func<string> message)
}
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
+ public void Fatal(string message, Exception exception)
+ {
+ // don't check for enabled at this level
+ _logger.Fatal(message, exception);
+ }
+
+ [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public void Fatal(string message, params object[] formatting)
{
// don't check for enabled at this level
Something went wrong with that request. Please try again.