From 8e4644568bdbe2bb9502283867f8ad975b18cd8f Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Tue, 4 Dec 2012 05:58:24 +0500 Subject: [PATCH] Updated loghelper to be able to Warn and also trace Warn with an exception. Updated the macro control to check for the Partial View Macro, added Partial View macro to the enum. --- src/Umbraco.Core/Logging/LogHelper.cs | 45 ++++++++++++++---- src/Umbraco.Web/umbraco.presentation/macro.cs | 46 +++++++++++-------- src/umbraco.cms/businesslogic/macro/Macro.cs | 32 +++++++------ .../businesslogic/macro/MacroTypes.cs | 3 +- 4 files changed, 82 insertions(+), 44 deletions(-) diff --git a/src/Umbraco.Core/Logging/LogHelper.cs b/src/Umbraco.Core/Logging/LogHelper.cs index cdcfc3787c..8e35f3c217 100644 --- a/src/Umbraco.Core/Logging/LogHelper.cs +++ b/src/Umbraco.Core/Logging/LogHelper.cs @@ -80,6 +80,35 @@ namespace Umbraco.Core.Logging } + public static void Warn(Type callingType, string message, TraceContext trace, Exception e, params object[] items) + { + if (trace != null) + { + if (e != null) + { + trace.Warn(callingType.FullName, string.Format(message, items), e); + } + else + { + trace.Warn(callingType.FullName, string.Format(message, items)); + } + } + + var logger = LogManager.GetLogger(callingType); + if (logger != null) + { + if (e != null) + { + logger.WarnFormat(PrefixThreadId(message) + ". Exception: " + e, items); + } + else + { + logger.WarnFormat(PrefixThreadId(message), items); + } + } + + } + /// /// Adds a warn log /// @@ -88,21 +117,17 @@ namespace Umbraco.Core.Logging /// public static void Warn(string message, params object[] items) { - var logger = LoggerFor(); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), items); + Warn(typeof (T), message, items); } public static void Warn(string message, TraceContext trace, params object[] items) { - if (trace != null) - { - trace.Warn(string.Format(message, items)); - } + Warn(typeof(T), message, trace, items); + } - var logger = LoggerFor(); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), items); + public static void Warn(string message, TraceContext trace, Exception e, params object[] items) + { + Warn(typeof(T), message, trace, e, items); } #endregion diff --git a/src/Umbraco.Web/umbraco.presentation/macro.cs b/src/Umbraco.Web/umbraco.presentation/macro.cs index ffdd59879a..6fed6fbd90 100644 --- a/src/Umbraco.Web/umbraco.presentation/macro.cs +++ b/src/Umbraco.Web/umbraco.presentation/macro.cs @@ -17,6 +17,7 @@ using System.Web.UI.WebControls; using System.Xml; using System.Xml.Xsl; using Umbraco.Core; +using Umbraco.Core.Logging; using Umbraco.Web.Templates; using umbraco.BusinessLogic; using umbraco.BusinessLogic.Utils; @@ -369,6 +370,12 @@ namespace umbraco int macroType = Model.MacroType != MacroTypes.Unknown ? (int)Model.MacroType : MacroType; switch (macroType) { + case (int)MacroTypes.PartialView: + UmbracoContext.Current.Trace.Write("umbracoMacro", "Partial View added (" + Model.TypeName + ")"); + + //TODO: IMPLEMENT THIS!!!! + + break; case (int)MacroTypes.UserControl: try { @@ -400,10 +407,11 @@ namespace umbraco { renderFailed = true; Exceptions.Add(e); - UmbracoContext.Current.Trace.Warn("umbracoMacro", - "Error loading customControl (Assembly: " + - Model.TypeAssembly + - ", Type: '" + Model.TypeName + "'", e); + + LogHelper.Warn("Error loading customControl (Assembly: " + + Model.TypeAssembly + + ", Type: '" + Model.TypeName + "'", UmbracoContext.Current.Trace, e); + macroControl = new LiteralControl("Error loading customControl (Assembly: " + Model.TypeAssembly + ", Type: '" + @@ -433,9 +441,10 @@ namespace umbraco { renderFailed = true; Exceptions.Add(e); - UmbracoContext.Current.Trace.Warn("umbracoMacro", - "Error loading MacroEngine script (file: " + ScriptFile + - ", Type: '" + Model.TypeName + "'", e); + + LogHelper.Warn("umbracoMacro", + "Error loading MacroEngine script (file: " + ScriptFile + + ", Type: '" + Model.TypeName + "'", UmbracoContext.Current.Trace, e); var result = new LiteralControl("Error loading MacroEngine script (file: " + ScriptFile + ")"); @@ -1405,18 +1414,19 @@ namespace umbraco } } - /// - /// Loads an usercontrol using reflection into the macro object - /// - /// Filename of the usercontrol - ie. ~wulff.ascx - /// The attributes. - /// The page elements. - /// - public Control loadUserControl(string fileName, MacroModel model, Hashtable pageElements) + /// + /// Loads an usercontrol using reflection into the macro object + /// + /// Filename of the usercontrol - ie. ~wulff.ascx + /// + /// The page elements. + /// + public Control loadUserControl(string fileName, MacroModel model, Hashtable pageElements) { - Debug.Assert(!string.IsNullOrEmpty(fileName), "fileName cannot be empty"); - Debug.Assert(model.Properties != null, "attributes cannot be null"); - Debug.Assert(pageElements != null, "pageElements cannot be null"); + Mandate.ParameterNotNullOrEmpty(fileName, "fileName"); + Mandate.ParameterNotNull(model, "model"); + Mandate.ParameterNotNull(pageElements, "pageElements"); + try { string userControlPath = fileName; diff --git a/src/umbraco.cms/businesslogic/macro/Macro.cs b/src/umbraco.cms/businesslogic/macro/Macro.cs index 029c77e728..5c61562108 100644 --- a/src/umbraco.cms/businesslogic/macro/Macro.cs +++ b/src/umbraco.cms/businesslogic/macro/Macro.cs @@ -4,6 +4,7 @@ using System.Security.Cryptography; using System.Text; using System.Xml; using System.Runtime.CompilerServices; +using Umbraco.Core.IO; using umbraco.cms.businesslogic.cache; using umbraco.DataLayer; using umbraco.BusinessLogic; @@ -530,22 +531,23 @@ namespace umbraco.cms.businesslogic.macro { if (!string.IsNullOrEmpty(xslt)) return MacroTypes.XSLT; - else - { - if (!string.IsNullOrEmpty(scriptFile)) - return MacroTypes.Script; - else - { - if (!string.IsNullOrEmpty(scriptType) && scriptType.ToLower().IndexOf(".ascx") > -1) - { - return MacroTypes.UserControl; - } - else if (!string.IsNullOrEmpty(scriptType) && !string.IsNullOrEmpty(scriptAssembly)) - return MacroTypes.CustomControl; - } - } + + if (!string.IsNullOrEmpty(scriptFile)) + { + //we need to check if the file path saved is a virtual path starting with ~/Views/MacroPartials, if so then this is + //a partial view macro, not a script macro + return scriptFile.StartsWith(SystemDirectories.MvcViews + "/MacroPartials/") + ? MacroTypes.PartialView + : MacroTypes.Script; + } - return MacroTypes.Unknown; + if (!string.IsNullOrEmpty(scriptType) && scriptType.ToLower().IndexOf(".ascx") > -1) + return MacroTypes.UserControl; + + if (!string.IsNullOrEmpty(scriptType) && !string.IsNullOrEmpty(scriptAssembly)) + return MacroTypes.CustomControl; + + return MacroTypes.Unknown; } public static string GenerateCacheKeyFromCode(string input) diff --git a/src/umbraco.cms/businesslogic/macro/MacroTypes.cs b/src/umbraco.cms/businesslogic/macro/MacroTypes.cs index 3104d5062a..d75afbe739 100644 --- a/src/umbraco.cms/businesslogic/macro/MacroTypes.cs +++ b/src/umbraco.cms/businesslogic/macro/MacroTypes.cs @@ -7,6 +7,7 @@ namespace umbraco.cms.businesslogic.macro UserControl = 3, Unknown = 4, Python = 5, - Script = 6 + Script = 6, + PartialView = 7 } } \ No newline at end of file