From f6d01c8d2323480aef82db7129479695bc83aa7a Mon Sep 17 00:00:00 2001 From: Daniel Stolt Date: Wed, 28 Aug 2013 02:28:36 +0200 Subject: [PATCH] Improved logging in Orchard.Azure module. --- .../Caching/Database/AzureCacheClient.cs | 4 +- .../Database/AzureCacheConfiguration.cs | 4 +- .../Caching/Database/AzureCacheRecord.cs | 18 ++++++++ .../Output/AzureOutputCacheStorageProvider.cs | 45 ++++++++++++------- .../AzureBlobShellSettingsManager.cs | 3 +- 5 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheRecord.cs diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheClient.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheClient.cs index da491c64a..eb9b5c85d 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheClient.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheClient.cs @@ -30,7 +30,7 @@ namespace Orchard.Azure.Services.Caching.Database { _regionAlphaNumeric = new String(Array.FindAll(_region.ToCharArray(), c => Char.IsLetterOrDigit(c))) + _region.GetHashCode().ToString(); if (_logger.IsDebugEnabled) - _logger.DebugFormat("Creating instance with CacheName='{0}' and Region='{1}' (original Region='{2}').", cacheName, _regionAlphaNumeric, _region); + _logger.DebugFormat("Creating cache with CacheName='{0}' and Region='{1}' (original Region='{2}').", cacheName, _regionAlphaNumeric, _region); if (!String.IsNullOrEmpty(cacheName)) _cache = dataCacheFactory.GetCache(cacheName); @@ -101,7 +101,7 @@ namespace Orchard.Azure.Services.Caching.Database { // The NHibernate locking mechanism and the Azure Cache pessimistic concurrency // model are not a perfect fit. For example, Azure Cache has atomic "get-and-lock" - // and "put-and-unlock" semantics but there is no corresponding atomic operations + // and "put-and-unlock" semantics but there are no corresponding atomic operations // defined on the ICache interface of NHibernate. Also, Azure Cache does not // strictly enforce the pessimistic concurrency model - clients are responsible // for following the locking protocol and therefore the implementation assumes that diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheConfiguration.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheConfiguration.cs index 9155b8247..7215e19b0 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheConfiguration.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheConfiguration.cs @@ -33,8 +33,10 @@ namespace Orchard.Azure.Services.Caching.Database { doSave = true; } - if (doSave) + if (doSave) { + Logger.Information("Added missing shell settings; calling IShellSettingsManager.SaveSettings()."); shellSettingsManager.SaveSettings(shellSettings); + } CacheHostIdentifier = shellSettings[Constants.DatabaseCacheHostIdentifierSettingName]; CacheName = shellSettings[Constants.DatabaseCacheCacheNameSettingName]; diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheRecord.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheRecord.cs new file mode 100644 index 000000000..f77eca746 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Database/AzureCacheRecord.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Orchard.Azure.Services.Caching.Database.Models { + + /// + /// Fake record in order to force the mappings to be updated + /// once the modules is enabled/disabled. + /// + public class AzureCacheRecord { + public virtual int Id { + get; + set; + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs index eba990340..630877d3f 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Caching/Output/AzureOutputCacheStorageProvider.cs @@ -1,12 +1,12 @@ -using Microsoft.ApplicationServer.Caching; -using Orchard.Environment.Configuration; -using Orchard.Environment.Extensions; -using Orchard.OutputCache.Models; -using Orchard.OutputCache.Services; -using System; +using System; using System.Collections.Generic; using System.Linq; -using System.Web; +using Microsoft.ApplicationServer.Caching; +using Orchard.Environment.Configuration; +using Orchard.Environment.Extensions; +using Orchard.Logging; +using Orchard.OutputCache.Models; +using Orchard.OutputCache.Services; namespace Orchard.Azure.Services.Caching.Output { @@ -31,13 +31,17 @@ namespace Orchard.Azure.Services.Caching.Output { shellSettings[Constants.OutputCacheIsSharedCachingSettingName] = "false"; doSave = true; } - - if (doSave) + + if (doSave) { + Logger.Information("Added missing shell settings; calling IShellSettingsManager.SaveSettings()."); shellSettingsManager.SaveSettings(shellSettings); + } var cacheHostIdentifier = shellSettings[Constants.OutputCacheHostIdentifierSettingName]; var cacheName = shellSettings[Constants.OutputCacheCacheNameSettingName]; + Logger.Debug("Creating cache with HostIdentifier='{0}', CacheName='{1}'.", cacheHostIdentifier, cacheName); + var dataCacheFactoryConfiguration = new DataCacheFactoryConfiguration() { AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, cacheHostIdentifier), MaxConnectionsToServer = 32, @@ -52,15 +56,16 @@ namespace Orchard.Azure.Services.Caching.Output { _cache = dataCacheFactory.GetDefaultCache(); _usingSharedCaching = Boolean.Parse(shellSettings[Constants.OutputCacheIsSharedCachingSettingName]); - - if (!_usingSharedCaching) - { + + if (!_usingSharedCaching) { // If not using Windows Azure Shared Caching we can enable additional features by // storing all cache items in a region. This enables enumerating and counting all // items currently in the cache. _region = shellSettings.Name; - _cache.CreateRegion(_region); + _cache.CreateRegion(_region); } + else + Logger.Debug("Configured to use Shared Caching."); } private readonly DataCache _cache; @@ -68,6 +73,7 @@ namespace Orchard.Azure.Services.Caching.Output { private readonly string _region; public void Set(string key, CacheItem cacheItem) { + Logger.Debug("Set() invoked with key='{0}' in region '{1}'.", key, _region); if (_usingSharedCaching) _cache.Put(key, cacheItem); else @@ -75,6 +81,7 @@ namespace Orchard.Azure.Services.Caching.Output { } public void Remove(string key) { + Logger.Debug("Remove() invoked with key='{0}' in region '{1}'.", key, _region); if (_usingSharedCaching) _cache.Remove(key); else @@ -82,6 +89,7 @@ namespace Orchard.Azure.Services.Caching.Output { } public void RemoveAll() { + Logger.Debug("RemoveAll() invoked in region '{0}'.", _region); if (_usingSharedCaching) _cache.Clear(); else @@ -89,6 +97,7 @@ namespace Orchard.Azure.Services.Caching.Output { } public CacheItem GetCacheItem(string key) { + Logger.Debug("GetCacheItem() invoked with key='{0}' in region '{1}'.", key, _region); if (_usingSharedCaching) return _cache.Get(key) as CacheItem; else @@ -96,8 +105,11 @@ namespace Orchard.Azure.Services.Caching.Output { } public IEnumerable GetCacheItems(int skip, int count) { - if (_usingSharedCaching) + Logger.Debug("GetCacheItems() invoked in region '{0}'.", _region); + if (_usingSharedCaching) { + Logger.Debug("Enumeration not supported with Shared Caching; returning empty enumerable."); return Enumerable.Empty(); // Enumeration not supported with Shared Caching. + } return _cache.GetObjectsInRegion(_region).AsParallel() .Select(x => x.Value) @@ -108,8 +120,11 @@ namespace Orchard.Azure.Services.Caching.Output { } public int GetCacheItemsCount() { - if (_usingSharedCaching) + Logger.Debug("GetCacheItemsCount() invoked in region '{0}'.", _region); + if (_usingSharedCaching) { + Logger.Debug("Enumeration not supported with Shared Caching; returning zero."); return 0; // Enumeration not supported with Shared Caching. + } return _cache.GetObjectsInRegion(_region).AsParallel() .Select(x => x.Value) diff --git a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/AzureBlobShellSettingsManager.cs b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/AzureBlobShellSettingsManager.cs index 9b4f6ca2b..7a1f6f763 100644 --- a/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/AzureBlobShellSettingsManager.cs +++ b/src/Orchard.Web/Modules/Orchard.Azure/Services/Environment/Configuration/AzureBlobShellSettingsManager.cs @@ -24,7 +24,8 @@ namespace Orchard.Azure.Services.Environment.Configuration { private readonly AzureFileSystem _fileSystem; private readonly IShellSettingsManagerEventHandler _events; - public AzureBlobShellSettingsManager(IMimeTypeProvider mimeTypeProvider, IShellSettingsManagerEventHandler events) { + public AzureBlobShellSettingsManager(IMimeTypeProvider mimeTypeProvider, IShellSettingsManagerEventHandler events) + : base() { _fileSystem = new AzureFileSystem(CloudConfigurationManager.GetSetting(Constants.ShellSettingsStorageConnectionStringSettingName), Constants.ShellSettingsContainerName, String.Empty, true, mimeTypeProvider); _events = events; }