decouples more of LogHelper and DisposableTimer

This commit is contained in:
Shannon
2015-01-16 12:29:27 +11:00
parent ed95ccc18e
commit 8f100a7e9f
13 changed files with 60 additions and 44 deletions

View File

@@ -8,6 +8,7 @@ using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Logging;
using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Profiling;
using Umbraco.Core.Services;
@@ -28,7 +29,7 @@ namespace Umbraco.Core
/// <param name="serviceContext"></param>
/// <param name="cache"></param>
/// <param name="logger"></param>
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, ILogger logger)
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, ProfilingLogger logger)
{
if (dbContext == null) throw new ArgumentNullException("dbContext");
if (serviceContext == null) throw new ArgumentNullException("serviceContext");
@@ -36,7 +37,7 @@ namespace Umbraco.Core
_databaseContext = dbContext;
_services = serviceContext;
ApplicationCache = cache;
Logger = logger;
ProfilingLogger = logger;
}
/// <summary>
@@ -47,7 +48,8 @@ namespace Umbraco.Core
/// <param name="cache"></param>
[Obsolete("Use the other constructor specifying an ILogger instead")]
public ApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache)
: this(dbContext, serviceContext, cache, LoggerResolver.Current.Logger)
: this(dbContext, serviceContext, cache,
new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler))
{
}
@@ -96,7 +98,7 @@ namespace Umbraco.Core
/// <remarks>
/// This is NOT thread safe
/// </remarks>
[Obsolete("Use the other method specifying an ILogger instead")]
[Obsolete("Use the other method specifying an ProfilingLogger instead")]
public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, bool replaceContext)
{
if (ApplicationContext.Current != null)
@@ -124,7 +126,7 @@ namespace Umbraco.Core
/// <remarks>
/// This is NOT thread safe
/// </remarks>
public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, ILogger logger, bool replaceContext)
public static ApplicationContext EnsureContext(DatabaseContext dbContext, ServiceContext serviceContext, CacheHelper cache, ProfilingLogger logger, bool replaceContext)
{
if (ApplicationContext.Current != null)
{
@@ -150,10 +152,10 @@ namespace Umbraco.Core
public CacheHelper ApplicationCache { get; private set; }
/// <summary>
/// Exposes the global ILogger - this should generally not be accessed via the UmbracoContext and should normally just be exposed
/// Exposes the global ProfilingLogger - this should generally not be accessed via the UmbracoContext and should normally just be exposed
/// on most base classes or injected with IoC
/// </summary>
public ILogger Logger { get; private set; }
public ProfilingLogger ProfilingLogger { get; private set; }
// IsReady is set to true by the boot manager once it has successfully booted
// note - the original umbraco module checks on content.Instance in umbraco.dll
@@ -227,7 +229,7 @@ namespace Umbraco.Core
if (currentVersion != configStatus)
{
Logger.Info<ApplicationContext>("CurrentVersion different from configStatus: '" + currentVersion + "','" + configStatus + "'");
ProfilingLogger.Logger.Info<ApplicationContext>("CurrentVersion different from configStatus: '" + currentVersion + "','" + configStatus + "'");
}

View File

@@ -36,7 +36,7 @@ namespace Umbraco.Core
/// </remarks>
public class CoreBootManager : IBootManager
{
private ProfilingLogger _profilingLogger;
private DisposableTimer _timer;
private bool _isInitialized = false;
private bool _isStarted = false;
@@ -64,7 +64,9 @@ namespace Umbraco.Core
InitializeLoggerResolver();
InitializeProfilerResolver();
_timer = DisposableTimer.DebugDuration<CoreBootManager>("Umbraco application starting", "Umbraco application startup complete");
_profilingLogger = new ProfilingLogger(LoggerResolver.Current.Logger, ProfilerResolver.Current.Profiler);
_timer = _profilingLogger.DebugDuration<CoreBootManager>("Umbraco application starting", "Umbraco application startup complete");
CreateApplicationCache();
@@ -119,7 +121,7 @@ namespace Umbraco.Core
protected virtual void CreateApplicationContext(DatabaseContext dbContext, ServiceContext serviceContext)
{
//create the ApplicationContext
ApplicationContext = ApplicationContext.Current = new ApplicationContext(dbContext, serviceContext, ApplicationCache, LoggerResolver.Current.Logger);
ApplicationContext = ApplicationContext.Current = new ApplicationContext(dbContext, serviceContext, ApplicationCache, _profilingLogger);
}
/// <summary>

View File

@@ -62,7 +62,7 @@ namespace Umbraco.Core
}
}
protected DisposableTimer(Action<long> callback)
protected internal DisposableTimer(Action<long> callback)
{
if (callback == null) throw new ArgumentNullException("callback");
_callback = callback;

View File

@@ -196,7 +196,7 @@ namespace Umbraco.Core
//add the trees.config - use the contents to create the has since this gets resaved on every app startup!
new Tuple<FileSystemInfo, bool>(new FileInfo(IOHelper.MapPath(SystemDirectories.Config + "/trees.config")), true)
}
}, _appContext.ProfilingLogger
);
return _currentAssembliesHash;
}
@@ -219,9 +219,9 @@ namespace Umbraco.Core
/// (true will make a hash based on it's contents)
/// </param>
/// <returns></returns>
internal static long GetFileHash(IEnumerable<Tuple<FileSystemInfo, bool>> filesAndFolders)
internal static long GetFileHash(IEnumerable<Tuple<FileSystemInfo, bool>> filesAndFolders, ProfilingLogger logger)
{
using (DisposableTimer.TraceDuration<PluginManager>("Determining hash of code files on disk", "Hash determined"))
using (logger.TraceDuration<PluginManager>("Determining hash of code files on disk", "Hash determined"))
{
var hashCombiner = new HashCodeCombiner();
@@ -250,9 +250,9 @@ namespace Umbraco.Core
}
}
internal static long GetFileHash(IEnumerable<FileSystemInfo> filesAndFolders)
internal static long GetFileHash(IEnumerable<FileSystemInfo> filesAndFolders, ProfilingLogger logger)
{
using (DisposableTimer.TraceDuration<PluginManager>("Determining hash of code files on disk", "Hash determined"))
using (logger.TraceDuration<PluginManager>("Determining hash of code files on disk", "Hash determined"))
{
var hashCombiner = new HashCodeCombiner();
@@ -610,7 +610,7 @@ namespace Umbraco.Core
//we want to know if it fails ever, not how long it took if it is only 0.
var typesAsArray = types.ToArray();
//using (DisposableTimer.DebugDuration<PluginManager>(
//using (_appContext.ProfilingLogger.DebugDuration<PluginManager>(
// String.Format("Starting instantiation of {0} objects of type {1}", typesAsArray.Length, typeof(T).FullName),
// String.Format("Completed instantiation of {0} objects of type {1}", typesAsArray.Length, typeof(T).FullName)))
//{
@@ -625,7 +625,7 @@ namespace Umbraco.Core
catch (Exception ex)
{
LogHelper.Error<PluginManager>(String.Format("Error creating type {0}", t.FullName), ex);
_appContext.ProfilingLogger.Logger.Error<PluginManager>(String.Format("Error creating type {0}", t.FullName), ex);
if (throwException)
{
@@ -659,9 +659,9 @@ namespace Umbraco.Core
{
var typesFound = new List<Type>();
using (DisposableTimer.TraceDuration<PluginManager>(
() => String.Format("Starting resolution types of {0}", typeof(T).FullName),
() => String.Format("Completed resolution of types of {0}, found {1}", typeof(T).FullName, typesFound.Count)))
using (_appContext.ProfilingLogger.TraceDuration<PluginManager>(
String.Format("Starting resolution types of {0}", typeof(T).FullName),
String.Format("Completed resolution of types of {0}, found {1}", typeof(T).FullName, typesFound.Count)))
{
//check if the TypeList already exists, if so return it, if not we'll create it
var typeList = _types.SingleOrDefault(x => x.IsTypeList<T>(resolutionType));
@@ -669,7 +669,7 @@ namespace Umbraco.Core
//need to put some logging here to try to figure out why this is happening: http://issues.umbraco.org/issue/U4-3505
if (cacheResult && typeList != null)
{
LogHelper.Debug<PluginManager>("Existing typeList found for {0} with resolution type {1}", () => typeof(T), () => resolutionType);
_appContext.ProfilingLogger.Logger.Debug<PluginManager>("Existing typeList found for {0} with resolution type {1}", () => typeof(T), () => resolutionType);
}
//if we're not caching the result then proceed, or if the type list doesn't exist then proceed
@@ -691,7 +691,7 @@ namespace Umbraco.Core
//so in this instance there will never be a result.
if (fileCacheResult.Exception != null && fileCacheResult.Exception is CachedPluginNotFoundInFileException)
{
LogHelper.Debug<PluginManager>("Tried to find typelist for type {0} and resolution {1} in file cache but the type was not found so loading types by assembly scan ", () => typeof(T), () => resolutionType);
_appContext.ProfilingLogger.Logger.Debug<PluginManager>("Tried to find typelist for type {0} and resolution {1} in file cache but the type was not found so loading types by assembly scan ", () => typeof(T), () => resolutionType);
//we don't have a cache for this so proceed to look them up by scanning
LoadViaScanningAndUpdateCacheFile<T>(typeList, resolutionType, finder);
@@ -716,7 +716,7 @@ namespace Umbraco.Core
//if there are any exceptions loading types, we have to exist, this should never happen so
//we will need to revert to scanning for types.
successfullyLoadedFromCache = false;
LogHelper.Error<PluginManager>("Could not load a cached plugin type: " + t + " now reverting to re-scanning assemblies for the base type: " + typeof(T).FullName, ex);
_appContext.ProfilingLogger.Logger.Error<PluginManager>("Could not load a cached plugin type: " + t + " now reverting to re-scanning assemblies for the base type: " + typeof(T).FullName, ex);
break;
}
}
@@ -727,14 +727,14 @@ namespace Umbraco.Core
}
else
{
LogHelper.Debug<PluginManager>("Loaded plugin types {0} with resolution {1} from persisted cache", () => typeof(T), () => resolutionType);
_appContext.ProfilingLogger.Logger.Debug<PluginManager>("Loaded plugin types {0} with resolution {1} from persisted cache", () => typeof(T), () => resolutionType);
}
}
}
}
else
{
LogHelper.Debug<PluginManager>("Assembly changes detected, loading types {0} for resolution {1} by assembly scan", () => typeof(T), () => resolutionType);
_appContext.ProfilingLogger.Logger.Debug<PluginManager>("Assembly changes detected, loading types {0} for resolution {1} by assembly scan", () => typeof(T), () => resolutionType);
//we don't have a cache for this so proceed to look them up by scanning
LoadViaScanningAndUpdateCacheFile<T>(typeList, resolutionType, finder);
@@ -746,7 +746,7 @@ namespace Umbraco.Core
//add the type list to the collection
var added = _types.Add(typeList);
LogHelper.Debug<PluginManager>("Caching of typelist for type {0} and resolution {1} was successful = {2}", () => typeof(T), () => resolutionType, () => added);
_appContext.ProfilingLogger.Logger.Debug<PluginManager>("Caching of typelist for type {0} and resolution {1} was successful = {2}", () => typeof(T), () => resolutionType, () => added);
}
}

View File

@@ -7,7 +7,7 @@ using Umbraco.Core.Logging;
namespace Umbraco.Core.Profiling
{
/// <summary>
/// A profiler that outputs its results to the LogHelper
/// A profiler that outputs its results to an ILogger
/// </summary>
internal class LogProfiler : IProfiler
{
@@ -26,7 +26,7 @@ namespace Umbraco.Core.Profiling
public IDisposable Step(string name)
{
_logger.Debug(typeof(LogProfiler), "Starting - " + name);
return DisposableTimer.Start(l => _logger.Info(typeof(LogProfiler), () => name + " (took " + l + "ms)"));
return new DisposableTimer(l => _logger.Info(typeof(LogProfiler), () => name + " (took " + l + "ms)"));
}
public void Start()

View File

@@ -10,6 +10,7 @@ using Umbraco.Core.Logging;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Profiling;
using Umbraco.Core.Services;
using Moq;
using Umbraco.Web;
@@ -112,7 +113,7 @@ namespace Umbraco.Tests
new Mock<INotificationService>().Object,
Mock.Of<ILocalizedTextService>()),
CacheHelper.CreateDisabledCacheHelper(),
Mock.Of<ILogger>());
new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
Assert.Pass();
}

View File

@@ -12,6 +12,7 @@ using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Profiling;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using Umbraco.Tests.TestHelpers;
@@ -60,7 +61,7 @@ namespace Umbraco.Tests.Persistence
//assign the service context
new ServiceContext(repositoryFactory, new PetaPocoUnitOfWorkProvider(_logger), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, _logger),
cacheHelper,
new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))))
new ProfilingLogger(new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config"))), Mock.Of<IProfiler>()))
{
IsReady = true
};

View File

@@ -4,6 +4,7 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using Moq;
using NUnit.Framework;
using SqlCE4Umbraco;
using umbraco;
@@ -11,6 +12,8 @@ using umbraco.businesslogic;
using umbraco.cms.businesslogic;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Profiling;
using Umbraco.Core.PropertyEditors;
using umbraco.DataLayer;
using umbraco.editorControls;
@@ -236,18 +239,18 @@ namespace Umbraco.Tests.Plugins
var list1 = new[] { f1, f2, f3, f4, f5, f6 };
var list2 = new[] { f1, f3, f5 };
var list3 = new[] { f1, f3, f5, f7 };
//Act
var hash1 = PluginManager.GetFileHash(list1);
var hash2 = PluginManager.GetFileHash(list2);
var hash3 = PluginManager.GetFileHash(list3);
var hash1 = PluginManager.GetFileHash(list1, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
var hash2 = PluginManager.GetFileHash(list2, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
var hash3 = PluginManager.GetFileHash(list3, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));
//Assert
Assert.AreNotEqual(hash1, hash2);
Assert.AreNotEqual(hash1, hash3);
Assert.AreNotEqual(hash2, hash3);
Assert.AreEqual(hash1, PluginManager.GetFileHash(list1));
Assert.AreEqual(hash1, PluginManager.GetFileHash(list1, new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>())));
}
[Test]

View File

@@ -81,7 +81,7 @@ namespace Umbraco.Tests.TestHelpers
//assign the service context
new ServiceContext(repositoryFactory, new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, Logger),
cacheHelper,
Logger)
ProfilingLogger)
{
IsReady = true
};

View File

@@ -13,6 +13,7 @@ using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Profiling;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
using GlobalSettings = Umbraco.Core.Configuration.GlobalSettings;
@@ -73,7 +74,7 @@ namespace Umbraco.Tests.TestHelpers
//assign the service context
new ServiceContext(repositoryFactory, new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, logger),
cacheHelper,
logger)
new ProfilingLogger(logger, Mock.Of<IProfiler>()))
{
IsReady = true
};

View File

@@ -11,6 +11,7 @@ using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.SqlSyntax;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Profiling;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
@@ -32,7 +33,8 @@ namespace Umbraco.Tests.TestHelpers
[TestFixtureSetUp]
public void InitializeFixture()
{
Logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config")));
var logger = new Logger(new FileInfo(TestHelper.MapPathForTest("~/unit-test-log4net.config")));
ProfilingLogger = new ProfilingLogger(logger, new LogProfiler(logger));
}
[SetUp]
@@ -158,7 +160,7 @@ namespace Umbraco.Tests.TestHelpers
//assign the service context
new ServiceContext(repoFactory, new PetaPocoUnitOfWorkProvider(Logger), new FileUnitOfWorkProvider(), new PublishingStrategy(), cacheHelper, Logger),
cacheHelper,
Logger)
ProfilingLogger)
{
IsReady = true
};
@@ -201,6 +203,10 @@ namespace Umbraco.Tests.TestHelpers
get { return ApplicationContext.Current; }
}
protected ILogger Logger { get; private set; }
protected ILogger Logger
{
get { return ProfilingLogger.Logger; }
}
protected ProfilingLogger ProfilingLogger { get; private set; }
}
}

View File

@@ -87,7 +87,7 @@ namespace Umbraco.Web.Editors
ins.LoadConfig(IOHelper.MapPath(model.TemporaryDirectoryPath));
ins.InstallCleanUp(model.Id, IOHelper.MapPath(model.TemporaryDirectoryPath));
var clientDependencyConfig = new Umbraco.Core.Configuration.ClientDependencyConfiguration(ApplicationContext.Logger);
var clientDependencyConfig = new Umbraco.Core.Configuration.ClientDependencyConfiguration(ApplicationContext.ProfilingLogger.Logger);
var clientDependencyUpdated = clientDependencyConfig.IncreaseVersionNumber();
//clear the tree cache - we'll do this here even though the browser will reload, but just in case it doesn't can't hurt.

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Web.Install.InstallSteps
GlobalSettings.ConfigurationStatus = UmbracoVersion.Current.ToString(3);
// Update ClientDependency version
var clientDependencyConfig = new ClientDependencyConfiguration(_applicationContext.Logger);
var clientDependencyConfig = new ClientDependencyConfiguration(_applicationContext.ProfilingLogger.Logger);
var clientDependencyUpdated = clientDependencyConfig.IncreaseVersionNumber();
var security = new WebSecurity(_httpContext, _applicationContext);