Commit d24fe49b authored by 阿星Plus's avatar 阿星Plus

LogHelper

parent e6f8f8b9
using Castle.Core.Logging;
using Plus.Dependency;
using Plus.Runtime.Validation;
using System;
using System.Linq;
namespace Plus.Logging
{
public static class LogHelper
{
public static ILogger Logger { get; private set; }
static LogHelper()
{
Logger = IocManager.Instance.IsRegistered(typeof(ILoggerFactory))
? IocManager.Instance.Resolve<ILoggerFactory>().Create(typeof(LogHelper))
: NullLogger.Instance;
}
public static void LogException(Exception ex)
{
LogException(Logger, ex);
}
public static void LogException(ILogger logger, Exception ex)
{
LogSeverity severity = (ex as IHasLogSeverity)?.Severity ?? LogSeverity.Error;
logger.Log(severity, ex.Message, ex);
LogValidationErrors(logger, ex);
}
private static void LogValidationErrors(ILogger logger, Exception exception)
{
//Try to find inner validation exception
if (exception is AggregateException && exception.InnerException != null)
{
var aggException = exception as AggregateException;
if (aggException.InnerException is PlusValidationException)
{
exception = aggException.InnerException;
}
}
if (!(exception is PlusValidationException))
{
return;
}
var validationException = exception as PlusValidationException;
if (validationException.ValidationErrors.IsNullOrEmpty())
{
return;
}
logger.Log(validationException.Severity, "There are " + validationException.ValidationErrors.Count + " validation errors:");
foreach (var validationResult in validationException.ValidationErrors)
{
var memberNames = "";
if (validationResult.MemberNames != null && validationResult.MemberNames.Any())
{
memberNames = " (" + string.Join(", ", validationResult.MemberNames) + ")";
}
logger.Log(validationException.Severity, validationResult.ErrorMessage + memberNames);
}
}
}
}
\ No newline at end of file
using Castle.Core.Logging;
using System;
namespace Plus.Logging
{
public static class LoggerExtensions
{
public static void Log(this ILogger logger, LogSeverity severity, string message)
{
switch (severity)
{
case LogSeverity.Fatal:
logger.Fatal(message);
break;
case LogSeverity.Error:
logger.Error(message);
break;
case LogSeverity.Warn:
logger.Warn(message);
break;
case LogSeverity.Info:
logger.Info(message);
break;
case LogSeverity.Debug:
logger.Debug(message);
break;
default:
throw new PlusException("Unknown LogSeverity value: " + severity);
}
}
public static void Log(this ILogger logger, LogSeverity severity, string message, Exception exception)
{
switch (severity)
{
case LogSeverity.Fatal:
logger.Fatal(message, exception);
break;
case LogSeverity.Error:
logger.Error(message, exception);
break;
case LogSeverity.Warn:
logger.Warn(message, exception);
break;
case LogSeverity.Info:
logger.Info(message, exception);
break;
case LogSeverity.Debug:
logger.Debug(message, exception);
break;
default:
throw new PlusException("Unknown LogSeverity value: " + severity);
}
}
public static void Log(this ILogger logger, LogSeverity severity, Func<string> messageFactory)
{
switch (severity)
{
case LogSeverity.Fatal:
logger.Fatal(messageFactory);
break;
case LogSeverity.Error:
logger.Error(messageFactory);
break;
case LogSeverity.Warn:
logger.Warn(messageFactory);
break;
case LogSeverity.Info:
logger.Info(messageFactory);
break;
case LogSeverity.Debug:
logger.Debug(messageFactory);
break;
default:
throw new PlusException("Unknown LogSeverity value: " + severity);
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment