Adds in Error without ex for just a message and a message template with params

This commit is contained in:
Warren
2018-08-23 14:43:38 +01:00
parent fb4b0ceb69
commit 24f91f1917
8 changed files with 85 additions and 13 deletions

View File

@@ -19,12 +19,24 @@ namespace Umbraco.Core.Logging
System.Diagnostics.Debug.WriteLine(Environment.NewLine + exception, reporting.FullName);
}
/// <inheritdoc/>
public void Error(Type reporting, string message)
{
System.Diagnostics.Debug.WriteLine(message);
}
/// <inheritdoc/>
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);
}
/// <inheritdoc/>
public void Error(Type reporting, string messageTemplate, params object[] args)
{
System.Diagnostics.Debug.WriteLine(messageTemplate, args);
}
/// <inheritdoc/>
public void Warn(Type reporting, string format)
{

View File

@@ -22,6 +22,13 @@ namespace Umbraco.Core.Logging
/// <param name="exception">An exception.</param>
void Error(Type reporting, Exception exception);
/// <summary>
/// Logs an error message WITHOUT EX
/// </summary>
/// <param name="reporting">The reporting type.</param>
/// <param name="message">A message.</param>
void Error(Type reporting, string message);
/// <summary>
/// Logs an error message - using a structured log message
/// </summary>
@@ -30,7 +37,15 @@ namespace Umbraco.Core.Logging
/// <param name="messageTemplate">The message template that includes property values</param>
/// <param name="propertyValues">Property values to log & update in message template</param>
void Error(Type reporting, Exception exception, string messageTemplate, params object[] propertyValues);
/// <summary>
/// Logs an error message WITHOUT EX - using a structured log message
/// </summary>
/// <param name="reporting">The reporting type.</param>
/// <param name="messageTemplate">The message template that includes property values</param>
/// <param name="propertyValues">Property values to log & update in message template</param>
void Error(Type reporting, string messageTemplate, params object[] propertyValues);
/// <summary>
/// Logs a warning message.
/// </summary>

View File

@@ -61,6 +61,20 @@ namespace Umbraco.Core.Logging
Error(reporting, exception, string.Empty);
}
/// <inheritdoc/>
public void Error(Type reporting, string message)
{
//Sometimes we need to throw an error without an ex
Error(reporting, null, message);
}
/// <inheritdoc/>
public void Error(Type reporting, string messageTemplate, params object[] propertyValues)
{
//Log a structured message WITHOUT an ex
Error(reporting, null, messageTemplate, propertyValues);
}
/// <inheritdoc/>
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;

View File

@@ -43,6 +43,29 @@ namespace Umbraco.Core.Logging
logger.Error(typeof(T), exception);
}
/// <summary>
/// Logs an error message WITHOUT EX
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="logger"></param>
/// <param name="message"></param>
public static void Error<T>(this ILogger logger, string message)
{
logger.Error(typeof(T), message);
}
/// <summary>
/// Logs an error message - using a structured log message
/// </summary>
/// <typeparam name="T">The reporting type</typeparam>
/// <param name="logger">The logger.</param>
/// <param name="messageTemplate">A structured message template</param>
/// <param name="propertyValues">Message property values</param>
public static void Error<T>(this ILogger logger, string messageTemplate, params object[] propertyValues)
{
logger.Error(typeof(T), messageTemplate, propertyValues);
}
/// <summary>
/// Logs a warning message.
/// </summary>

View File

@@ -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);

View File

@@ -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<ContentTypeRepository>(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<ContentTypeRepository>("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();

View File

@@ -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<ContentTypeRepositoryBase<TEntity>>(e,
"Property Type '{PropertyTypeName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.",
Logger.Error<ContentTypeRepositoryBase<TEntity>>("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<ContentTypeRepositoryBase<TEntity>>(ex, "{EntityTypeName} '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.",
Logger.Error<ContentTypeRepositoryBase<TEntity>>("{EntityTypeName} '{EntityName}' cannot have an empty Alias. This is most likely due to invalid characters stripped from the Alias.",
typeof(TEntity).Name,
entity.Name);

View File

@@ -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);