Move logging stuff

This commit is contained in:
Bjarke Berg
2019-11-06 10:43:33 +01:00
parent 747fe85ede
commit 640da8bc91
22 changed files with 63 additions and 46 deletions

View File

@@ -0,0 +1,140 @@
using System;
namespace Umbraco.Core.Logging
{
/// <summary>
/// Implements <see cref="ILogger"/> on top of <see cref="System.Diagnostics"/>.
/// </summary>
public class DebugDiagnosticsLogger : ILogger
{
private readonly IMessageTemplates _messageTemplates;
public DebugDiagnosticsLogger(IMessageTemplates messageTemplates)
{
_messageTemplates = messageTemplates;
}
public bool IsEnabled(Type reporting, LogLevel level)
=> true;
/// <inheritdoc/>
public void Fatal(Type reporting, Exception exception, string message)
{
System.Diagnostics.Debug.WriteLine(message + Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Fatal(Type reporting, Exception exception)
{
System.Diagnostics.Debug.WriteLine(Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Fatal(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message);
}
/// <inheritdoc/>
public void Fatal(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(messageTemplate, propertyValues) + Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Fatal(Type reporting, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(messageTemplate, propertyValues);
}
/// <inheritdoc/>
public void Error(Type reporting, Exception exception, string message)
{
System.Diagnostics.Debug.WriteLine(message + Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Error(Type reporting, Exception exception)
{
System.Diagnostics.Debug.WriteLine(Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Error(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message);
}
/// <inheritdoc/>
public void Error(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(messageTemplate, propertyValues) + Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Error(Type reporting, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(messageTemplate, propertyValues);
}
/// <inheritdoc/>
public void Warn(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message, reporting.FullName);
}
/// <inheritdoc/>
public void Warn(Type reporting, string message, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(message, propertyValues), reporting.FullName);
}
/// <inheritdoc/>
public void Warn(Type reporting, Exception exception, string message)
{
System.Diagnostics.Debug.WriteLine(message + Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Warn(Type reporting, Exception exception, string message, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(message + Environment.NewLine + exception, propertyValues), reporting.FullName);
}
/// <inheritdoc/>
public void Info(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message, reporting.FullName);
}
/// <inheritdoc/>
public void Info(Type reporting, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(messageTemplate, propertyValues), reporting.FullName);
}
/// <inheritdoc/>
public void Debug(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message, reporting.FullName);
}
/// <inheritdoc/>
public void Debug(Type reporting, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(messageTemplate, propertyValues), reporting.FullName);
}
/// <inheritdoc/>
public void Verbose(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message, reporting.FullName);
}
/// <inheritdoc/>
public void Verbose(Type reporting, string messageTemplate, params object[] propertyValues)
{
System.Diagnostics.Debug.WriteLine(_messageTemplates.Render(messageTemplate, propertyValues), reporting.FullName);
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Umbraco.Core.Logging
{
public interface IMessageTemplates
{
string Render(string messageTemplate, params object[] args);
}
}

View File

@@ -0,0 +1,62 @@
using System;
using System.Diagnostics;
namespace Umbraco.Core.Logging
{
/// <summary>
/// Implements <see cref="IProfiler"/> by writing profiling results to an <see cref="ILogger"/>.
/// </summary>
public class LogProfiler : IProfiler
{
private readonly ILogger _logger;
public LogProfiler(ILogger logger)
{
_logger = logger;
}
/// <inheritdoc/>
public string Render()
{
return string.Empty;
}
/// <inheritdoc/>
public IDisposable Step(string name)
{
_logger.Debug<LogProfiler>("Begin: {ProfileName}", name);
return new LightDisposableTimer(duration => _logger.Info<LogProfiler>("End {ProfileName} ({ProfileDuration}ms)", name, duration));
}
/// <inheritdoc/>
public void Start()
{
// the log will always be started
}
/// <inheritdoc/>
public void Stop(bool discardResults = false)
{
// the log never stops
}
// a lightweight disposable timer
private class LightDisposableTimer : DisposableObjectSlim
{
private readonly Action<long> _callback;
private readonly Stopwatch _stopwatch = Stopwatch.StartNew();
protected internal LightDisposableTimer(Action<long> callback)
{
if (callback == null) throw new ArgumentNullException(nameof(callback));
_callback = callback;
}
protected override void DisposeResources()
{
_stopwatch.Stop();
_callback(_stopwatch.ElapsedMilliseconds);
}
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
namespace Umbraco.Core.Logging
{
internal static class LoggingTaskExtension
{
/// <summary>
/// This task shouldn't be waited on (as it's not guaranteed to run), and you shouldn't wait on the parent task either (because it might throw an
/// exception that doesn't get handled). If you want to be waiting on something, use LogErrorsWaitable instead.
///
/// None of these methods are suitable for tasks that return a value. If you're wanting a result, you should probably be handling
/// errors yourself.
/// </summary>
public static Task LogErrors(this Task task, Action<string, Exception> logMethod)
{
return task.ContinueWith(t => LogErrorsInner(t, logMethod), TaskContinuationOptions.OnlyOnFaulted);
}
/// <summary>
/// This task can be waited on (as it's guaranteed to run), and you should wait on this rather than the parent task. Because it's
/// guaranteed to run, it may be slower than using LogErrors, and you should consider using that method if you don't want to wait.
///
/// None of these methods are suitable for tasks that return a value. If you're wanting a result, you should probably be handling
/// errors yourself.
/// </summary>
public static Task LogErrorsWaitable(this Task task, Action<string, Exception> logMethod)
{
return task.ContinueWith(t => LogErrorsInner(t, logMethod));
}
private static void LogErrorsInner(Task task, Action<string, Exception> logAction)
{
if (task.Exception != null)
{
logAction("Aggregate Exception with " + task.Exception.InnerExceptions.Count + " inner exceptions: ", task.Exception);
foreach (var innerException in task.Exception.InnerExceptions)
{
logAction("Inner exception from aggregate exception: ", innerException);
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
namespace Umbraco.Core.Logging
{
public class VoidProfiler : IProfiler
{
private readonly VoidDisposable _disposable = new VoidDisposable();
public string Render()
{
return string.Empty;
}
public IDisposable Step(string name)
{
return _disposable;
}
public void Start()
{ }
public void Stop(bool discardResults = false)
{ }
private class VoidDisposable : DisposableObjectSlim
{
protected override void DisposeResources()
{ }
}
}
}