Merge branch 'umbUKFest-U4-5231' of https://github.com/AnthonyCogworks/Umbraco-CMS into 7.3.0
Conflicts: src/Umbraco.Core/Services/ServiceContext.cs src/Umbraco.Core/Umbraco.Core.csproj src/Umbraco.Tests/MockTests.cs
This commit is contained in:
@@ -9,6 +9,7 @@ namespace Umbraco.Core.Logging
|
||||
///<summary>
|
||||
/// Used for logging
|
||||
///</summary>
|
||||
[Obsolete("Use UmbracoContext.Current.Application.Services.LoggingService instead")]
|
||||
public static class LogHelper
|
||||
{
|
||||
///<summary>
|
||||
|
||||
23
src/Umbraco.Core/Services/ILoggingService.cs
Normal file
23
src/Umbraco.Core/Services/ILoggingService.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for logging service.
|
||||
/// </summary>
|
||||
public interface ILoggingService
|
||||
{
|
||||
void Error(Type callingType, string message, Exception exception);
|
||||
void Warn(Type callingType, string message, params Func<object>[] formatItems);
|
||||
|
||||
void WarnWithException(Type callingType, string message, Exception e, params Func<object>[] formatItems);
|
||||
|
||||
void Info(Type callingType, Func<string> generateMessage);
|
||||
|
||||
void Info(Type type, string generateMessageFormat, params Func<object>[] formatItems);
|
||||
|
||||
void Debug(Type callingType, Func<string> generateMessage);
|
||||
|
||||
void Debug(Type type, string generateMessageFormat, params Func<object>[] formatItems);
|
||||
}
|
||||
}
|
||||
264
src/Umbraco.Core/Services/LoggingService.cs
Normal file
264
src/Umbraco.Core/Services/LoggingService.cs
Normal file
@@ -0,0 +1,264 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Web;
|
||||
using log4net;
|
||||
|
||||
namespace Umbraco.Core.Services
|
||||
{
|
||||
///<summary>
|
||||
/// Used for logging
|
||||
///</summary>
|
||||
public class LoggingService : ILoggingService
|
||||
{
|
||||
///<summary>
|
||||
/// Returns a logger for the type specified
|
||||
///</summary>
|
||||
///<typeparam name="T"></typeparam>
|
||||
///<returns></returns>
|
||||
internal ILog LoggerFor<T>()
|
||||
{
|
||||
return LogManager.GetLogger(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a logger for the object's type
|
||||
/// </summary>
|
||||
/// <param name="getTypeFromInstance"></param>
|
||||
/// <returns></returns>
|
||||
internal ILog LoggerFor(object getTypeFromInstance)
|
||||
{
|
||||
if (getTypeFromInstance == null) throw new ArgumentNullException("getTypeFromInstance");
|
||||
|
||||
return LogManager.GetLogger(getTypeFromInstance.GetType());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Useful if the logger itself is running on another thread
|
||||
/// </summary>
|
||||
/// <param name="generateMessageFormat"></param>
|
||||
/// <returns></returns>
|
||||
private string PrefixThreadId(string generateMessageFormat)
|
||||
{
|
||||
return "[Thread " + Thread.CurrentThread.ManagedThreadId + "] " + generateMessageFormat;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Adds an error log
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="exception"></param>
|
||||
public void Error<T>(string message, Exception exception)
|
||||
{
|
||||
Error(typeof (T), message, exception);
|
||||
}
|
||||
|
||||
public void Error(Type callingType, string message, Exception exception)
|
||||
{
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger != null)
|
||||
logger.Error(PrefixThreadId(message), exception);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Warn(Type callingType, string message, params Func<object>[] formatItems)
|
||||
{
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger == null || !logger.IsWarnEnabled) return;
|
||||
logger.WarnFormat(PrefixThreadId(message), formatItems.Select(x => x.Invoke()).ToArray());
|
||||
}
|
||||
|
||||
public void Warn(Type callingType, string message, bool showHttpTrace, params Func<object>[] formatItems)
|
||||
{
|
||||
Mandate.ParameterNotNull(callingType, "callingType");
|
||||
Mandate.ParameterNotNullOrEmpty(message, "message");
|
||||
|
||||
if (showHttpTrace && HttpContext.Current != null)
|
||||
{
|
||||
HttpContext.Current.Trace.Warn(callingType.Name, string.Format(message, formatItems.Select(x => x.Invoke()).ToArray()));
|
||||
}
|
||||
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger == null || !logger.IsWarnEnabled) return;
|
||||
logger.WarnFormat(PrefixThreadId(message), formatItems.Select(x => x.Invoke()).ToArray());
|
||||
|
||||
}
|
||||
|
||||
public void WarnWithException(Type callingType, string message, Exception e, params Func<object>[] formatItems)
|
||||
{
|
||||
WarnWithException(callingType, message, false, e, formatItems);
|
||||
}
|
||||
|
||||
public void WarnWithException(Type callingType, string message, bool showHttpTrace, Exception e, params Func<object>[] formatItems)
|
||||
{
|
||||
Mandate.ParameterNotNull(e, "e");
|
||||
Mandate.ParameterNotNull(callingType, "callingType");
|
||||
Mandate.ParameterNotNullOrEmpty(message, "message");
|
||||
|
||||
if (showHttpTrace && HttpContext.Current != null)
|
||||
{
|
||||
HttpContext.Current.Trace.Warn(
|
||||
callingType.Name,
|
||||
string.Format(message, formatItems.Select(x => x.Invoke()).ToArray()),
|
||||
e);
|
||||
}
|
||||
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger == null || !logger.IsWarnEnabled) return;
|
||||
var executedParams = formatItems.Select(x => x.Invoke()).ToArray();
|
||||
logger.WarnFormat(PrefixThreadId(message) + ". Exception: " + e, executedParams);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a warn log
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="message"></param>
|
||||
/// <param name="formatItems"></param>
|
||||
public void Warn<T>(string message, params Func<object>[] formatItems)
|
||||
{
|
||||
Warn(typeof(T), message, formatItems);
|
||||
}
|
||||
|
||||
public void Warn<T>(string message, bool showHttpTrace, params Func<object>[] formatItems)
|
||||
{
|
||||
Warn(typeof(T), message, showHttpTrace, formatItems);
|
||||
}
|
||||
|
||||
public void WarnWithException<T>(string message, Exception e, params Func<object>[] formatItems)
|
||||
{
|
||||
WarnWithException(typeof(T), message, e, formatItems);
|
||||
}
|
||||
public void WarnWithException<T>(string message, bool showHttpTrace, Exception e, params Func<object>[] formatItems)
|
||||
{
|
||||
WarnWithException(typeof(T), message, showHttpTrace, e, formatItems);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="generateMessage">The delegate to generate a message.</param>
|
||||
/// <remarks></remarks>
|
||||
public void Info<T>(Func<string> generateMessage)
|
||||
{
|
||||
Info(typeof(T), generateMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traces if tracing is enabled.
|
||||
/// </summary>
|
||||
/// <param name="callingType"></param>
|
||||
/// <param name="generateMessage"></param>
|
||||
public void Info(Type callingType, Func<string> generateMessage)
|
||||
{
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger == null || !logger.IsInfoEnabled) return;
|
||||
logger.Info(PrefixThreadId(generateMessage.Invoke()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Traces if tracing is enabled.
|
||||
/// </summary>
|
||||
/// <param name="type">The type for the logging namespace.</param>
|
||||
/// <param name="generateMessageFormat">The message format.</param>
|
||||
/// <param name="formatItems">The format items.</param>
|
||||
public void Info(Type type, string generateMessageFormat, params Func<object>[] 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="generateMessageFormat">The generate message format.</param>
|
||||
/// <param name="formatItems">The format items.</param>
|
||||
/// <remarks></remarks>
|
||||
public void Info<T>(string generateMessageFormat, params Func<object>[] formatItems)
|
||||
{
|
||||
Info(typeof(T), generateMessageFormat, formatItems);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="generateMessage">The delegate to generate a message.</param>
|
||||
/// <remarks></remarks>
|
||||
public void Debug<T>(Func<string> generateMessage)
|
||||
{
|
||||
Debug(typeof(T), generateMessage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Debugs if tracing is enabled.
|
||||
/// </summary>
|
||||
/// <param name="callingType"></param>
|
||||
/// <param name="generateMessage"></param>
|
||||
public void Debug(Type callingType, Func<string> generateMessage)
|
||||
{
|
||||
var logger = LogManager.GetLogger(callingType);
|
||||
if (logger == null || !logger.IsDebugEnabled) return;
|
||||
logger.Debug(PrefixThreadId(generateMessage.Invoke()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Debugs if tracing is enabled.
|
||||
/// </summary>
|
||||
/// <param name="type">The type for the logging namespace.</param>
|
||||
/// <param name="generateMessageFormat">The message format.</param>
|
||||
/// <param name="formatItems">The format items.</param>
|
||||
public void Debug(Type type, string generateMessageFormat, params Func<object>[] 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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="generateMessageFormat">The generate message format.</param>
|
||||
/// <param name="formatItems">The format items.</param>
|
||||
/// <remarks></remarks>
|
||||
public void Debug<T>(string generateMessageFormat, params Func<object>[] formatItems)
|
||||
{
|
||||
Debug(typeof(T), generateMessageFormat, formatItems);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="generateMessageFormat"></param>
|
||||
/// <param name="showHttpTrace"></param>
|
||||
/// <param name="formatItems"></param>
|
||||
public void Debug<T>(string generateMessageFormat, bool showHttpTrace, params Func<object>[] formatItems)
|
||||
{
|
||||
if (showHttpTrace && HttpContext.Current != null)
|
||||
{
|
||||
HttpContext.Current.Trace.Write(
|
||||
typeof(T).Name,
|
||||
string.Format(generateMessageFormat, formatItems.Select(x => x()).ToArray()));
|
||||
}
|
||||
Debug(typeof(T), generateMessageFormat, formatItems);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Umbraco.Core.Logging;
|
||||
using System.IO;
|
||||
using Umbraco.Core.IO;
|
||||
using Umbraco.Core.Persistence;
|
||||
@@ -15,6 +16,7 @@ namespace Umbraco.Core.Services
|
||||
public class ServiceContext
|
||||
{
|
||||
private Lazy<ILocalizedTextService> _localizedTextService;
|
||||
private Lazy<ILoggingService> _loggingService;
|
||||
private Lazy<ITagService> _tagService;
|
||||
private Lazy<IContentService> _contentService;
|
||||
private Lazy<IUserService> _userService;
|
||||
@@ -56,6 +58,7 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="tagService"></param>
|
||||
/// <param name="notificationService"></param>
|
||||
/// <param name="localizedTextService"></param>
|
||||
/// <param name="loggingService"></param>
|
||||
public ServiceContext(
|
||||
IContentService contentService,
|
||||
IMediaService mediaService,
|
||||
@@ -74,6 +77,7 @@ namespace Umbraco.Core.Services
|
||||
IApplicationTreeService treeService,
|
||||
ITagService tagService,
|
||||
INotificationService notificationService,
|
||||
INotificationService notificationService, ILoggingService loggingService)
|
||||
ILocalizedTextService localizedTextService)
|
||||
{
|
||||
_localizedTextService = new Lazy<ILocalizedTextService>(() => localizedTextService);
|
||||
@@ -94,7 +98,7 @@ namespace Umbraco.Core.Services
|
||||
_memberService = new Lazy<IMemberService>(() => memberService);
|
||||
_userService = new Lazy<IUserService>(() => userService);
|
||||
_notificationService = new Lazy<INotificationService>(() => notificationService);
|
||||
|
||||
_loggingService = new Lazy<ILoggingService>(() => loggingService);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -189,6 +193,10 @@ namespace Umbraco.Core.Services
|
||||
|
||||
if (_memberGroupService == null)
|
||||
_memberGroupService = new Lazy<IMemberGroupService>(() => new MemberGroupService(provider, repositoryFactory.Value));
|
||||
|
||||
|
||||
if (_loggingService== null)
|
||||
_loggingService = new Lazy<ILoggingService>(() => new LoggingService());
|
||||
|
||||
}
|
||||
|
||||
@@ -351,6 +359,13 @@ namespace Umbraco.Core.Services
|
||||
{
|
||||
get { return _memberGroupService.Value; }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets the LoggingService
|
||||
/// </summary>
|
||||
public ILoggingService LoggingService
|
||||
{
|
||||
get { return _loggingService.Value; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -318,6 +318,8 @@
|
||||
<Compile Include="IO\UmbracoMediaFile.cs" />
|
||||
<Compile Include="Services\LocalizedTextService.cs" />
|
||||
<Compile Include="Services\ILocalizedTextService.cs" />
|
||||
<Compile Include="Services\ILoggingService.cs" />
|
||||
<Compile Include="Services\LoggingService.cs" />
|
||||
<Compile Include="Macros\XsltExtension.cs" />
|
||||
<Compile Include="Macros\XsltExtensionAttribute.cs" />
|
||||
<Compile Include="Macros\XsltExtensionsResolver.cs" />
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Text;
|
||||
using System.Web;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
using Umbraco.Core.Services;
|
||||
@@ -57,7 +58,7 @@ namespace Umbraco.Tests
|
||||
new Mock<ISectionService>().Object,
|
||||
new Mock<IApplicationTreeService>().Object,
|
||||
new Mock<ITagService>().Object,
|
||||
new Mock<INotificationService>().Object,
|
||||
new Mock<INotificationService>().Object, new Mock<ILoggingService>().Object);
|
||||
Mock.Of<ILocalizedTextService>());
|
||||
Assert.Pass();
|
||||
}
|
||||
@@ -104,7 +105,7 @@ namespace Umbraco.Tests
|
||||
new Mock<ISectionService>().Object,
|
||||
new Mock<IApplicationTreeService>().Object,
|
||||
new Mock<ITagService>().Object,
|
||||
new Mock<INotificationService>().Object,
|
||||
new Mock<INotificationService>().Object, new Mock<ILoggingService>().Object),
|
||||
Mock.Of<ILocalizedTextService>()),
|
||||
CacheHelper.CreateDisabledCacheHelper());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user