From 487fdea34e9a96f04fe33444db7c5b1abbc9d571 Mon Sep 17 00:00:00 2001 From: Sebastiaan Janssen Date: Mon, 15 Oct 2012 07:02:29 -0200 Subject: [PATCH] Logging for the datalayer now properly uses the LogHelper in Umbraco.Core --- src/Umbraco.Core/Logging/LogHelper.cs | 2 - src/Umbraco.Core/Properties/AssemblyInfo.cs | 1 + src/Umbraco.Web.UI/Web.config | 8 +- src/Umbraco.Web/Umbraco.Web.csproj | 4 +- src/umbraco.datalayer/LogHelper.cs | 236 ------------------ src/umbraco.datalayer/SqlHelper.cs | 25 +- .../umbraco.datalayer.csproj | 10 +- 7 files changed, 25 insertions(+), 261 deletions(-) delete mode 100644 src/umbraco.datalayer/LogHelper.cs diff --git a/src/Umbraco.Core/Logging/LogHelper.cs b/src/Umbraco.Core/Logging/LogHelper.cs index 5f5c70eda9..4de0c29171 100644 --- a/src/Umbraco.Core/Logging/LogHelper.cs +++ b/src/Umbraco.Core/Logging/LogHelper.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Web; using log4net; diff --git a/src/Umbraco.Core/Properties/AssemblyInfo.cs b/src/Umbraco.Core/Properties/AssemblyInfo.cs index 0074388eca..e94b64590b 100644 --- a/src/Umbraco.Core/Properties/AssemblyInfo.cs +++ b/src/Umbraco.Core/Properties/AssemblyInfo.cs @@ -25,6 +25,7 @@ using System.Runtime.InteropServices; [assembly: InternalsVisibleTo("businesslogic")] [assembly: InternalsVisibleTo("cms")] [assembly: InternalsVisibleTo("umbraco.editorControls")] +[assembly: InternalsVisibleTo("umbraco.datalayer")] [assembly: InternalsVisibleTo("umbraco.MacroEngines")] [assembly: InternalsVisibleTo("umbraco.editorControls")] diff --git a/src/Umbraco.Web.UI/Web.config b/src/Umbraco.Web.UI/Web.config index 3422403616..bd20bbb178 100644 --- a/src/Umbraco.Web.UI/Web.config +++ b/src/Umbraco.Web.UI/Web.config @@ -35,12 +35,12 @@ - - + + - - + + diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 5f53a21809..c5d2cfbbdf 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -2061,7 +2061,9 @@ ASPXCodeBehind - + + ASPXCodeBehind + ASPXCodeBehind diff --git a/src/umbraco.datalayer/LogHelper.cs b/src/umbraco.datalayer/LogHelper.cs deleted file mode 100644 index 18de8e6a54..0000000000 --- a/src/umbraco.datalayer/LogHelper.cs +++ /dev/null @@ -1,236 +0,0 @@ -using System; -using System.Linq; -using System.Threading; -using System.Web; -using log4net; - -namespace umbraco.DataLayer -{ - // TODO: Make this not be a copy of the loghelper in Umbraco.Core - // Currently this is JUST here so we can do some logging of the - // installer and to find calls to legacy tables/columns so they - // can be removed over time. Do not rely on this thing, it will move - - /// - /// Used for logging - /// - internal static class TemporaryLogHelper - { - /// - /// Returns a logger for the type specified - /// - /// - /// - public static ILog LoggerFor() - { - return LogManager.GetLogger(typeof(T)); - } - - /// - /// Returns a logger for the object's type - /// - /// - /// - public static ILog LoggerFor(object getTypeFromInstance) - { - if (getTypeFromInstance == null) throw new ArgumentNullException("getTypeFromInstance"); - - return LogManager.GetLogger(getTypeFromInstance.GetType()); - } - - /// - /// Useful if the logger itself is running on another thread - /// - /// - /// - private static string PrefixThreadId(string generateMessageFormat) - { - return "[Thread " + Thread.CurrentThread.ManagedThreadId + "] " + generateMessageFormat; - } - - #region Error - /// - /// Adds an error log - /// - /// - /// - /// - public static void Error(string message, Exception exception) - { - var logger = LoggerFor(); - if (logger != null) - logger.Error(PrefixThreadId(message), exception); - } - #endregion - - #region Warn - - public static void Warn(Type callingType, string message, params object[] format) - { - var logger = LogManager.GetLogger(callingType); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), format); - } - - public static void Warn(Type callingType, TraceContext trace, string message, params object[] format) - { - if (trace != null) - { - trace.Warn(string.Format(message, format)); - } - - var logger = LogManager.GetLogger(callingType); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), format); - - } - - /// - /// Adds a warn log - /// - /// - /// - /// - public static void Warn(string message, params object[] items) - { - var logger = LoggerFor(); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), items); - } - - public static void Warn(string message, TraceContext trace, params object[] items) - { - if (trace != null) - { - trace.Warn(string.Format(message, items)); - } - - var logger = LoggerFor(); - if (logger != null) - logger.WarnFormat(PrefixThreadId(message), items); - } - - #endregion - - #region Info - /// - /// Traces a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled. - /// - /// - /// The delegate to generate a message. - /// - public static void Info(Func generateMessage) - { - Info(typeof(T), generateMessage); - } - - /// - /// Traces if tracing is enabled. - /// - /// - /// - public static void Info(Type callingType, Func generateMessage) - { - var logger = LogManager.GetLogger(callingType); - if (logger == null || !logger.IsInfoEnabled) return; - logger.Info(PrefixThreadId(generateMessage.Invoke())); - } - - /// - /// Traces if tracing is enabled. - /// - /// The type for the logging namespace. - /// The message format. - /// The format items. - public static void Info(Type type, string generateMessageFormat, params Func[] formatItems) - { - var logger = LogManager.GetLogger(type); - if (logger == null || !logger.IsInfoEnabled) return; - var executedParams = formatItems.Select(x => x.Invoke()).ToArray(); - logger.InfoFormat(PrefixThreadId(generateMessageFormat), executedParams); - } - - /// - /// Traces a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled. - /// - /// - /// The generate message format. - /// The format items. - /// - public static void Info(string generateMessageFormat, params Func[] formatItems) - { - Info(typeof(T), generateMessageFormat, formatItems); - } - #endregion - - #region Debug - /// - /// Debugs a message, only generating the message if tracing is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled. - /// - /// - /// The delegate to generate a message. - /// - public static void Debug(Func generateMessage) - { - Debug(typeof(T), generateMessage); - } - - /// - /// Debugs if tracing is enabled. - /// - /// - /// - public static void Debug(Type callingType, Func generateMessage) - { - var logger = LogManager.GetLogger(callingType); - if (logger == null || !logger.IsDebugEnabled) return; - logger.Debug(PrefixThreadId(generateMessage.Invoke())); - } - - /// - /// Debugs if tracing is enabled. - /// - /// The type for the logging namespace. - /// The message format. - /// The format items. - public static void Debug(Type type, string generateMessageFormat, params Func[] formatItems) - { - var logger = LogManager.GetLogger(type); - if (logger == null || !logger.IsDebugEnabled) return; - var executedParams = formatItems.Select(x => x.Invoke()).ToArray(); - logger.DebugFormat(PrefixThreadId(generateMessageFormat), executedParams); - } - - /// - /// Debugs a message, only generating the message if debug is actually enabled. Use this method to avoid calling any long-running methods such as "ToDebugString" if logging is disabled. - /// - /// - /// The generate message format. - /// The format items. - /// - public static void Debug(string generateMessageFormat, params Func[] formatItems) - { - Debug(typeof(T), generateMessageFormat, formatItems); - } - - /// - /// Debugs a message and also writes to the TraceContext specified, useful for when you would like the debug - /// output also displayed in the Http trace output. - /// - /// - /// - /// - /// - public static void Debug(string generateMessageFormat, TraceContext trace, params Func[] formatItems) - { - if (trace != null) - { - trace.Write(string.Format(generateMessageFormat, formatItems.Select(x => x()))); - } - Debug(typeof(T), generateMessageFormat, formatItems); - } - - #endregion - - } -} diff --git a/src/umbraco.datalayer/SqlHelper.cs b/src/umbraco.datalayer/SqlHelper.cs index f8b50bf374..9eea44c290 100644 --- a/src/umbraco.datalayer/SqlHelper.cs +++ b/src/umbraco.datalayer/SqlHelper.cs @@ -12,6 +12,7 @@ using System.Diagnostics; using System.IO; using System.Xml; using umbraco.DataLayer.Utility; +using Umbraco.Core.Logging; namespace umbraco.DataLayer { @@ -123,18 +124,13 @@ namespace umbraco.DataLayer throw new ArgumentNullException("parameters"); // Add raw types of parameters to an array - P[] newParams = new P[parameters.Length]; - for (int index = 0; index < parameters.Length; index++) + var newParams = new P[parameters.Length]; + for (var index = 0; index < parameters.Length; index++) { // Get raw type out of container - IParameterContainer

parameter = parameters[index] as IParameterContainer

; + var parameter = parameters[index] as IParameterContainer

; if (parameter == null) - throw new ArgumentException( - String.Format("Element {0} of parameters has the wrong type. Expected: IParameterContainer<{1}>. Received: {2}.", - index, - typeof(P).Name, - (parameter==null ? "null" : parameter.GetType().Name)), - "parameters"); + throw new ArgumentException(String.Format("Element {0} of parameters has the wrong type. Expected: IParameterContainer<{1}>. Received: null.", index, typeof(P).Name), "parameters"); newParams[index] = parameter.RawParameter; } return newParams; @@ -198,7 +194,7 @@ namespace umbraco.DataLayer } catch (Exception e) { - TemporaryLogHelper.Error>(string.Format("Error executing query {0}", commandText), e); + LogHelper.Error>(string.Format("Error executing query {0}", commandText), e); throw new SqlHelperException("ExecuteScalar", commandText, parameters, e); } } @@ -222,7 +218,7 @@ namespace umbraco.DataLayer } catch (Exception e) { - TemporaryLogHelper.Error>(string.Format("Error executing query {0}", commandText), e); + LogHelper.Error>(string.Format("Error executing query {0}", commandText), e); throw new SqlHelperException("ExecuteNonQuery", commandText, parameters, e); } } @@ -242,11 +238,12 @@ namespace umbraco.DataLayer P[] parametersConverted = ConvertParameters(parameters); try { + LogHelper.Info>(string.Format("Executing query {0}", commandText)); return ExecuteReader(commandConverted.TrimToOneLine(), parametersConverted); } catch (Exception e) { - TemporaryLogHelper.Error>(string.Format("Error executing query {0}", commandText), e); + LogHelper.Error>(string.Format("Error executing query {0}", commandText), e); throw new SqlHelperException("ExecuteReader", commandText, parameters, e); } } @@ -270,7 +267,7 @@ namespace umbraco.DataLayer } catch (Exception e) { - TemporaryLogHelper.Error>(string.Format("Error executing query {0}", commandText), e); + LogHelper.Error>(string.Format("Error executing query {0}", commandText), e); throw new SqlHelperException("ExecuteXmlReader", commandText, parameters, e); } } @@ -317,7 +314,7 @@ namespace umbraco.DataLayer /// protected virtual XmlReader ExecuteXmlReader(string commandText, params P[] parameters) { - string xmlString = (string)ExecuteScalar(commandText, parameters); + var xmlString = (string)ExecuteScalar(commandText, parameters); if (xmlString == null) throw new ArgumentException("The query didn't return a value."); diff --git a/src/umbraco.datalayer/umbraco.datalayer.csproj b/src/umbraco.datalayer/umbraco.datalayer.csproj index ac15a201a2..ef2c6a27d3 100644 --- a/src/umbraco.datalayer/umbraco.datalayer.csproj +++ b/src/umbraco.datalayer/umbraco.datalayer.csproj @@ -64,9 +64,6 @@ AllRules.ruleset - - ..\packages\log4net.2.0.0\lib\net40-full\log4net.dll - False ..\..\lib\Microsoft.ApplicationBlocks.Data.dll @@ -97,7 +94,6 @@ - @@ -198,6 +194,12 @@ + + + {31785bc3-256c-4613-b2f5-a1b0bdded8c1} + Umbraco.Core + +