-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from OmniSharp/fix/init
fixed initialize not working correctly, added additional logging
- Loading branch information
Showing
3 changed files
with
179 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Microsoft.Extensions.Logging; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Logging | ||
{ | ||
internal static class TimeLoggerExtensions | ||
{ | ||
class Disposable : IDisposable | ||
{ | ||
private readonly IDisposable _disposable; | ||
private readonly Action<long> _action; | ||
private readonly Stopwatch _sw; | ||
|
||
public Disposable(IDisposable disposable, Action<long> action) | ||
{ | ||
_disposable = disposable; | ||
_action = action; | ||
_sw = new Stopwatch(); | ||
_sw.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_sw.Stop(); | ||
_action(_sw.ElapsedMilliseconds); | ||
_disposable.Dispose(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Times the trace. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="message">The message.</param> | ||
/// <param name="args">The arguments.</param> | ||
/// <returns>IDisposable.</returns> | ||
public static IDisposable TimeTrace(this ILogger logger, string message, params object[] args) | ||
{ | ||
var scope = logger.BeginScope(new { }); | ||
logger.LogTrace($"Starting: {message}", args); | ||
return new Disposable(scope, elapsed => | ||
{ | ||
var a = args.Concat(new object[] { elapsed }).ToArray(); | ||
logger.LogTrace($"Finished: {message} in {{ElapsedMilliseconds}}ms", a); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Times the debug. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="message">The message.</param> | ||
/// <param name="args">The arguments.</param> | ||
/// <returns>IDisposable.</returns> | ||
public static IDisposable TimeDebug(this ILogger logger, string message, params object[] args) | ||
{ | ||
var scope = logger.BeginScope(new { }); | ||
logger.LogDebug($"Starting: {message}", args); | ||
return new Disposable(scope, elapsed => | ||
{ | ||
var a = args.Concat(new object[] { elapsed }).ToArray(); | ||
logger.LogDebug($"Finished: {message} in {{ElapsedMilliseconds}}ms", a); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Times the information. | ||
/// </summary> | ||
/// <param name="logger">The logger.</param> | ||
/// <param name="message">The message.</param> | ||
/// <param name="args">The arguments.</param> | ||
/// <returns>IDisposable.</returns> | ||
public static IDisposable TimeInformation(this ILogger logger, string message, params object[] args) | ||
{ | ||
var scope = logger.BeginScope(new { }); | ||
logger.LogInformation($"Starting: {message}", args); | ||
return new Disposable(scope, elapsed => | ||
{ | ||
var a = args.Concat(new object[] { elapsed }).ToArray(); | ||
logger.LogInformation($"Finished: {message} in {{ElapsedMilliseconds}}ms", a); | ||
}); | ||
} | ||
} | ||
} |
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