From 24f91f191742d5e6ea2f36aa97d7ac08adc1402b Mon Sep 17 00:00:00 2001 From: Warren Date: Thu, 23 Aug 2018 14:43:38 +0100 Subject: [PATCH] Adds in Error without ex for just a message and a message template with params --- .../Logging/DebugDiagnosticsLogger.cs | 12 ++++++++++ src/Umbraco.Core/Logging/ILogger.cs | 17 +++++++++++++- src/Umbraco.Core/Logging/Logger.cs | 15 +++++++++++- src/Umbraco.Core/Logging/LoggerExtensions.cs | 23 +++++++++++++++++++ src/Umbraco.Core/Logging/OwinLogger.cs | 4 ++-- .../Implement/ContentTypeRepository.cs | 6 ++--- .../Implement/ContentTypeRepositoryBase.cs | 9 ++++---- .../TestHelpers/ConsoleLogger.cs | 12 +++++++++- 8 files changed, 85 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs b/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs index 18e26fe331..b50c23d228 100644 --- a/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs +++ b/src/Umbraco.Core/Logging/DebugDiagnosticsLogger.cs @@ -19,12 +19,24 @@ namespace Umbraco.Core.Logging System.Diagnostics.Debug.WriteLine(Environment.NewLine + exception, reporting.FullName); } + /// + public void Error(Type reporting, string message) + { + System.Diagnostics.Debug.WriteLine(message); + } + /// public void Error(Type reporting, Exception exception, string messageTemplate, params object[] args) { System.Diagnostics.Debug.WriteLine(string.Format(messageTemplate, args) + Environment.NewLine + exception, reporting.FullName); } + /// + public void Error(Type reporting, string messageTemplate, params object[] args) + { + System.Diagnostics.Debug.WriteLine(messageTemplate, args); + } + /// public void Warn(Type reporting, string format) { diff --git a/src/Umbraco.Core/Logging/ILogger.cs b/src/Umbraco.Core/Logging/ILogger.cs index 7e18930c22..643ad044a3 100644 --- a/src/Umbraco.Core/Logging/ILogger.cs +++ b/src/Umbraco.Core/Logging/ILogger.cs @@ -22,6 +22,13 @@ namespace Umbraco.Core.Logging /// An exception. void Error(Type reporting, Exception exception); + /// + /// Logs an error message WITHOUT EX + /// + /// The reporting type. + /// A message. + void Error(Type reporting, string message); + /// /// Logs an error message - using a structured log message /// @@ -30,7 +37,15 @@ namespace Umbraco.Core.Logging /// The message template that includes property values /// Property values to log & update in message template void Error(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues); - + + /// + /// Logs an error message WITHOUT EX - using a structured log message + /// + /// The reporting type. + /// The message template that includes property values + /// Property values to log & update in message template + void Error(Type reporting, string messageTemplate, params object[] propertyValues); + /// /// Logs a warning message. /// diff --git a/src/Umbraco.Core/Logging/Logger.cs b/src/Umbraco.Core/Logging/Logger.cs index 744c0ff97c..656de088a7 100644 --- a/src/Umbraco.Core/Logging/Logger.cs +++ b/src/Umbraco.Core/Logging/Logger.cs @@ -61,6 +61,20 @@ namespace Umbraco.Core.Logging Error(reporting, exception, string.Empty); } + /// + public void Error(Type reporting, string message) + { + //Sometimes we need to throw an error without an ex + Error(reporting, null, message); + } + + /// + public void Error(Type reporting, string messageTemplate, params object[] propertyValues) + { + //Log a structured message WITHOUT an ex + Error(reporting, null, messageTemplate, propertyValues); + } + /// public void Error(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues) { @@ -97,7 +111,6 @@ namespace Umbraco.Core.Logging logger?.ForContext(reporting).Error(exception, messageTemplate, propertyValues); } - private static bool IsMonitorEnterThreadAbortException(Exception exception) { var abort = exception as ThreadAbortException; diff --git a/src/Umbraco.Core/Logging/LoggerExtensions.cs b/src/Umbraco.Core/Logging/LoggerExtensions.cs index 80071a8c02..c29bcef4f7 100644 --- a/src/Umbraco.Core/Logging/LoggerExtensions.cs +++ b/src/Umbraco.Core/Logging/LoggerExtensions.cs @@ -43,6 +43,29 @@ namespace Umbraco.Core.Logging logger.Error(typeof(T), exception); } + /// + /// Logs an error message WITHOUT EX + /// + /// + /// + /// + public static void Error(this ILogger logger, string message) + { + logger.Error(typeof(T), message); + } + + /// + /// Logs an error message - using a structured log message + /// + /// The reporting type + /// The logger. + /// A structured message template + /// Message property values + public static void Error(this ILogger logger, string messageTemplate, params object[] propertyValues) + { + logger.Error(typeof(T), messageTemplate, propertyValues); + } + /// /// Logs a warning message. /// diff --git a/src/Umbraco.Core/Logging/OwinLogger.cs b/src/Umbraco.Core/Logging/OwinLogger.cs index fd4699b542..5601cb53f2 100644 --- a/src/Umbraco.Core/Logging/OwinLogger.cs +++ b/src/Umbraco.Core/Logging/OwinLogger.cs @@ -26,10 +26,10 @@ namespace Umbraco.Core.Logging switch (eventType) { case TraceEventType.Critical: - _logger.Fatal(_type.Value, exception ?? new Exception("Critical error"), "[{EventType}] Event Id: {EventId}, State: {State}", eventType, eventId, state); + _logger.Fatal(_type.Value, exception, "[{EventType}] Event Id: {EventId}, State: {State}", eventType, eventId, state); return true; case TraceEventType.Error: - _logger.Error(_type.Value, exception ?? new Exception("Error"), "[{EventType}] Event Id: {EventId}, State: {State}", eventType, eventId, state); + _logger.Error(_type.Value, exception, "[{EventType}] Event Id: {EventId}, State: {State}", eventType, eventId, state); return true; case TraceEventType.Warning: _logger.Warn(_type.Value, "[{EventType}] Event Id: {EventId}, State: {State}", eventType, eventId, state); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs index 8fc9cdebcb..aa61383f85 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepository.cs @@ -227,9 +227,9 @@ namespace Umbraco.Core.Persistence.Repositories.Implement { if (string.IsNullOrWhiteSpace(entity.Alias)) { - var e = new Exception($"ContentType '{entity.Name}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias."); - Logger.Error(e, "ContentType '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", entity.Name); - throw e; + var ex = new Exception($"ContentType '{entity.Name}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias."); + Logger.Error("ContentType '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", entity.Name); + throw ex; } ((ContentType)entity).AddingEntity(); diff --git a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs index 0dfc25a5ff..555fc56157 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Implement/ContentTypeRepositoryBase.cs @@ -521,13 +521,12 @@ AND umbracoNode.id <> @id", { if (string.IsNullOrWhiteSpace(pt.Alias)) { - var e = new InvalidOperationException($"Property Type '{pt.Name}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias."); + var ex = new InvalidOperationException($"Property Type '{pt.Name}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias."); - Logger.Error>(e, - "Property Type '{PropertyTypeName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", + Logger.Error>("Property Type '{PropertyTypeName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", pt.Name); - throw e; + throw ex; } } @@ -537,7 +536,7 @@ AND umbracoNode.id <> @id", { var ex = new InvalidOperationException($"{typeof(TEntity).Name} '{entity.Name}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias."); - Logger.Error>(ex, "{EntityTypeName} '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", + Logger.Error>("{EntityTypeName} '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.", typeof(TEntity).Name, entity.Name); diff --git a/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs b/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs index 5611626b7d..e5efd1560c 100644 --- a/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs +++ b/src/Umbraco.Tests/TestHelpers/ConsoleLogger.cs @@ -17,12 +17,22 @@ namespace Umbraco.Tests.TestHelpers Console.WriteLine(exception); } + public void Error(Type reporting, string message) + { + Console.WriteLine("ERROR {0} - {1}", reporting.Name, message); + } + public void Error(Type reporting, Exception exception, string format, params object[] args) { Console.WriteLine("ERROR {0} - {1}", reporting.Name, string.Format(format, args)); Console.WriteLine(exception); } - + + public void Error(Type reporting, string format, params object[] args) + { + Console.WriteLine("ERROR {0} - {1}", reporting.Name, string.Format(format, args)); + } + public void Warn(Type reporting, string message) { Console.WriteLine("WARN {0} - {1}", reporting.Name, message);