Exemplo n.º 1
0
        public virtual string GetLogFormat(LoggingFormatterParams @params, LogAttribute attribute)
        {
            var builder = new StringBuilder();

            foreach (var formatter in formatters.Where(x => x.IsSatisfiedBy(attribute)))
            {
                builder.Append(formatter.Format(@params));
            }

            return(builder.ToString());
        }
Exemplo n.º 2
0
        public virtual void Intercept(IInvocation invocation)
        {
            if (!isConfigured)
            {
                ConfigureLog4Net();
            }

            Exception ex  = null;
            Stopwatch sw  = null;
            var       log = LogManager.GetLogger(invocation.TargetType);

            try
            {
                sw = Stopwatch.StartNew();
                invocation.Proceed();
            }
            catch (Exception error)
            {
                ex = error;
            }
            finally
            {
                sw.Stop();
            }

            Task.Factory.StartNew(() =>
            {
                var @params = new LoggingFormatterParams(invocation, sw.Elapsed, ex);
                ExecuteLoggingAction(invocation, @params, log);
            });

            if (ex != null)
            {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }

                throw ex;
            }
        }
Exemplo n.º 3
0
        protected virtual void ExecuteLoggingAction(IInvocation invocation, LoggingFormatterParams @params, ILog log)
        {
            try
            {
                var levelTargetConfiguration =
                    new Dictionary <LogLevel, Action <string, Exception> >
                {
                    { LogLevel.Debug, log.Debug },
                    { LogLevel.Error, log.Error },
                    { LogLevel.Fatal, log.Fatal },
                    { LogLevel.Info, log.Info },
                    { LogLevel.Warn, log.Warn },
                };

                var attributes = GetLogAttributes(invocation);
                if (attributes.Length > 0)
                {
                    var logAttribute = attributes.First();

                    Action <string, Exception> logAction;
                    if (@params.Exception == null)
                    {
                        logAction = levelTargetConfiguration[logAttribute.LogLevel];
                    }
                    else
                    {
                        logAction = levelTargetConfiguration[LogLevel.Error];
                    }

                    LogDetails(logAction, logAttribute, @params);
                }
            }
            catch (Exception err)
            {
                log.Error("Logger Broke!", err);
            }
        }
Exemplo n.º 4
0
 private static void LogDetails(Action <string, Exception> logTarget, LogAttribute attribute, LoggingFormatterParams @params)
 {
     if (attribute.Formatter != null)
     {
         var logFormat = attribute.Formatter.GetLogFormat(@params, attribute);
         logTarget(logFormat.Take(4096).Aggregate("", (input, next) => input += next), null);
     }
     else
     {
         logTarget(@params.Invocation.MethodInvocationTarget.Name, @params.Exception);
     }
 }