Skip to content

Commit

Permalink
Added LogMessageGenerator overloads for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
c0shea committed Apr 4, 2017
1 parent bc25298 commit 45d9413
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 72 deletions.
78 changes: 60 additions & 18 deletions src/NLog/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,54 +50,54 @@ public partial interface ILogger : ILoggerBase, ISuppress
/// Gets a value indicating whether logging is enabled for the <c>Trace</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Trace</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsTraceEnabled
{
get;
bool IsTraceEnabled
{
get;
}

/// <summary>
/// Gets a value indicating whether logging is enabled for the <c>Debug</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Debug</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsDebugEnabled
{
get;
bool IsDebugEnabled
{
get;
}

/// <summary>
/// Gets a value indicating whether logging is enabled for the <c>Info</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Info</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsInfoEnabled
{
get;
bool IsInfoEnabled
{
get;
}

/// <summary>
/// Gets a value indicating whether logging is enabled for the <c>Warn</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Warn</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsWarnEnabled
{
get;
bool IsWarnEnabled
{
get;
}

/// <summary>
/// Gets a value indicating whether logging is enabled for the <c>Error</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Error</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsErrorEnabled
{
get;
bool IsErrorEnabled
{
get;
}

/// <summary>
/// Gets a value indicating whether logging is enabled for the <c>Fatal</c> level.
/// </summary>
/// <returns>A value of <see langword="true" /> if logging is enabled for the <c>Fatal</c> level, otherwise it returns <see langword="false" />.</returns>
bool IsFatalEnabled
{
get;
bool IsFatalEnabled
{
get;
}

#region Trace() overloads
Expand Down Expand Up @@ -135,6 +135,13 @@ bool IsFatalEnabled
[Obsolete("Use Trace(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void TraceException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Trace</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Trace(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Trace</c> level.
/// </summary>
Expand Down Expand Up @@ -299,6 +306,13 @@ bool IsFatalEnabled
[Obsolete("Use Debug(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void DebugException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Debug</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Debug(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Debug</c> level.
/// </summary>
Expand Down Expand Up @@ -463,6 +477,13 @@ bool IsFatalEnabled
[Obsolete("Use Info(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void InfoException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Info</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Info(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Info</c> level.
/// </summary>
Expand Down Expand Up @@ -627,6 +648,13 @@ bool IsFatalEnabled
[Obsolete("Use Warn(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void WarnException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Warn</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Warn(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Warn</c> level.
/// </summary>
Expand Down Expand Up @@ -791,6 +819,13 @@ bool IsFatalEnabled
[Obsolete("Use Error(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void ErrorException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Error(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
Expand Down Expand Up @@ -956,6 +991,13 @@ bool IsFatalEnabled
[Obsolete("Use Fatal(Exception exception, string message, params object[] args) method instead. Marked obsolete before v4.3.11")]
void FatalException([Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Fatal</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Fatal(Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the <c>Fatal</c> level.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/NLog/ILoggerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ LogFactory Factory
[Obsolete("Use Log(LogLevel level, Exception exception, [Localizable(false)] string message, params object[] args) instead. Marked obsolete before v4.3.11")]
void LogException(LogLevel level, [Localizable(false)] string message, Exception exception);

/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
/// <param name="level">The log level.</param>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
void Log(LogLevel level, Exception exception, LogMessageGenerator messageFunc);

/// <summary>
/// Writes the diagnostic message and exception at the specified level.
/// </summary>
Expand Down
108 changes: 108 additions & 0 deletions src/NLog/Logger-generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ public void Trace([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Trace</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Trace(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsTraceEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Trace, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Trace</c> level.
/// </summary>
Expand Down Expand Up @@ -500,6 +518,24 @@ public void Debug([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Debug</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Debug(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsDebugEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Debug, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Debug</c> level.
/// </summary>
Expand Down Expand Up @@ -786,6 +822,24 @@ public void Info([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Info</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Info(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsInfoEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Info, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Info</c> level.
/// </summary>
Expand Down Expand Up @@ -1072,6 +1126,24 @@ public void Warn([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Warn</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Warn(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsWarnEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Warn, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Warn</c> level.
/// </summary>
Expand Down Expand Up @@ -1358,6 +1430,24 @@ public void Error([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Error(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsErrorEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Error, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Error</c> level.
/// </summary>
Expand Down Expand Up @@ -1644,6 +1734,24 @@ public void Fatal([Localizable(false)] string message, Exception exception)
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Fatal</c> level.
/// </summary>
/// <param name="exception">An exception to be logged.</param>
/// <param name="messageFunc">A function returning message to be written. Function is not evaluated if logging is not enabled.</param>
public void Fatal(Exception exception, LogMessageGenerator messageFunc)
{
if (this.IsFatalEnabled)
{
if (messageFunc == null)
{
throw new ArgumentNullException("messageFunc");
}

this.WriteToTargets(LogLevel.Fatal, exception, messageFunc(), null);
}
}

/// <summary>
/// Writes the diagnostic message and exception at the <c>Fatal</c> level.
/// </summary>
Expand Down
Loading

0 comments on commit 45d9413

Please sign in to comment.