Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
Add timed log methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Mar 19, 2013
1 parent 20eee06 commit a6b70f8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/Logos.Utility/Logging/LogUtility.cs
@@ -0,0 +1,94 @@
using System;
using System.Diagnostics;

namespace Logos.Utility.Logging
{
/// <summary>
/// Helper methods for using a Logger.
/// </summary>
public static class LogUtility
{
/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedWarn(this Logger logger, string message)
{
return logger.IsWarnEnabled ? TimedLog(logger.Warn, message, null) : Scope.Empty;
}

/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <param name="args">The message arguments.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedWarn(this Logger logger, string message, params object[] args)
{
return logger.IsWarnEnabled ? TimedLog(logger.Warn, message, args) : Scope.Empty;
}

/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedInfo(this Logger logger, string message)
{
return logger.IsInfoEnabled ? TimedLog(logger.Info, message, null) : Scope.Empty;
}

/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <param name="args">The message arguments.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedInfo(this Logger logger, string message, params object[] args)
{
return logger.IsInfoEnabled ? TimedLog(logger.Info, message, args) : Scope.Empty;
}

/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedDebug(this Logger logger, string message)
{
return logger.IsDebugEnabled ? TimedLog(logger.Debug, message, null) : Scope.Empty;
}

/// <summary>
/// Logs the specified message before and after an operation, displaying the elapsed time.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="message">The message.</param>
/// <param name="args">The message arguments.</param>
/// <returns>A Scope that, when disposed, logs the elapsed time.</returns>
public static Scope TimedDebug(this Logger logger, string message, params object[] args)
{
return logger.IsDebugEnabled ? TimedLog(logger.Debug, message, args) : Scope.Empty;
}

private static Scope TimedLog(Action<string> doLog, string message, object[] args)
{
string formattedMessage = args == null || args.Length == 0 ? message : message.FormatInvariant(args);
doLog("(Timed) " + formattedMessage);
Stopwatch stopwatch = Stopwatch.StartNew();
return Scope.Create(
delegate
{
stopwatch.Stop();
TimeSpan elapsed = stopwatch.Elapsed;
doLog("({0}) {1}".FormatInvariant(TimeSpanUtility.FormatForLogging(elapsed.Ticks < 0 ? TimeSpan.Zero : elapsed), formattedMessage));
});
}
}
}
3 changes: 3 additions & 0 deletions src/Logos.Utility/Logos.Utility.csproj
Expand Up @@ -78,6 +78,9 @@
<Compile Include="Logging\LoggerCore.cs" />
<Compile Include="Logging\LoggingService.cs" />
<Compile Include="Logging\LogManager.cs" />
<Compile Include="Logging\LogUtility.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="NativeMethods.cs" />
<Compile Include="Net\HttpWebRequestUtility.cs" />
<Compile Include="Net\WebExceptionUtility.cs" />
Expand Down

0 comments on commit a6b70f8

Please sign in to comment.