-
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
632daac
commit 4433b60
Showing
5 changed files
with
198 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using System; | ||
|
||
namespace Palmmedia.ReportGenerator.Core.Logging | ||
{ | ||
/// <summary> | ||
/// <see cref="ILogger"/> which executes a delegate. | ||
/// </summary> | ||
internal class DelegateLogger : ILogger | ||
{ | ||
/// <summary> | ||
/// The log delegate. | ||
/// </summary> | ||
private Action<VerbosityLevel, string> logDelegate; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DelegateLogger"/> class. | ||
/// </summary> | ||
/// <param name="logDelegate">The log delegate.</param> | ||
public DelegateLogger(Action<VerbosityLevel, string> logDelegate) | ||
{ | ||
this.logDelegate = logDelegate ?? throw new ArgumentNullException(nameof(logDelegate)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the verbosity level. | ||
/// </summary> | ||
public VerbosityLevel VerbosityLevel { get; set; } | ||
|
||
/// <summary> | ||
/// Log a message at DEBUG level. | ||
/// </summary> | ||
/// <param name="message">The message.</param> | ||
public void Debug(string message) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Info) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a formatted message at DEBUG level. | ||
/// </summary> | ||
/// <param name="format">The template string.</param> | ||
/// <param name="args">The arguments.</param> | ||
public void DebugFormat(string format, params object[] args) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Info) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, string.Format(format, args)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a message at INFO level. | ||
/// </summary> | ||
/// <param name="message">The message.</param> | ||
public void Info(string message) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Warning) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a formatted message at INFO level. | ||
/// </summary> | ||
/// <param name="format">The template string.</param> | ||
/// <param name="args">The arguments.</param> | ||
public void InfoFormat(string format, params object[] args) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Warning) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, string.Format(format, args)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a message at WARN level. | ||
/// </summary> | ||
/// <param name="message">The message.</param> | ||
public void Warn(string message) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Error) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a formatted message at WARN level. | ||
/// </summary> | ||
/// <param name="format">The template string.</param> | ||
/// <param name="args">The arguments.</param> | ||
public void WarnFormat(string format, params object[] args) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Error) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, string.Format(format, args)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a message at INFO level. | ||
/// </summary> | ||
/// <param name="message">The message.</param> | ||
public void Error(string message) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Off) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, message); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Log a formatted message at ERROR level. | ||
/// </summary> | ||
/// <param name="format">The template string.</param> | ||
/// <param name="args">The arguments.</param> | ||
public void ErrorFormat(string format, params object[] args) | ||
{ | ||
if (this.VerbosityLevel < VerbosityLevel.Off) | ||
{ | ||
this.logDelegate(this.VerbosityLevel, string.Format(format, args)); | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
|
||
namespace Palmmedia.ReportGenerator.Core.Logging | ||
{ | ||
/// <summary> | ||
/// A logger factory creating delegate loggers. | ||
/// </summary> | ||
internal class DelegateLoggerFactory : ILoggerFactory | ||
{ | ||
/// <summary> | ||
/// The cached logger. | ||
/// </summary> | ||
private readonly ILogger logger; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DelegateLoggerFactory"/> class. | ||
/// </summary> | ||
/// <param name="logDelegate">The log delegate.</param> | ||
public DelegateLoggerFactory(Action<VerbosityLevel, string> logDelegate) | ||
{ | ||
if (logDelegate == null) | ||
{ | ||
throw new ArgumentNullException(nameof(logDelegate)); | ||
} | ||
|
||
this.logger = new DelegateLogger(logDelegate); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the verbosity of delegate loggers. | ||
/// </summary> | ||
public VerbosityLevel VerbosityLevel | ||
{ | ||
get | ||
{ | ||
return this.logger.VerbosityLevel; | ||
} | ||
|
||
set | ||
{ | ||
this.logger.VerbosityLevel = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Initializes the logger for the given type. | ||
/// </summary> | ||
/// <param name="type">The type of the class that uses the logger.</param> | ||
/// <returns>The logger.</returns> | ||
public ILogger GetLogger(Type type) => this.logger; | ||
} | ||
} |
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