コード例 #1
0
        public static ActiveLoggerError Parse(Exception exception)
        {
            if (exception == null)
            {
                return(null);
            }

            var error = new ActiveLoggerError()
            {
                Message    = exception.Message,
                StackTrace = exception.StackTrace,
                InnerError = Parse(exception.InnerException)
            };

            List <ActiveLoggerError> list = new List <ActiveLoggerError>();

            if (exception is AggregateException aggregateException)
            {
                foreach (var ex in aggregateException.InnerExceptions)
                {
                    list.Add(Parse(ex));
                }
            }
            error.InnerErrors = list.ToArray();

            return(error);
        }
コード例 #2
0
        /// <summary>
        /// 日志记录
        /// </summary>
        /// <typeparam name="TState"></typeparam>
        /// <param name="logLevel"></param>
        /// <param name="eventId"></param>
        /// <param name="state"></param>
        /// <param name="exception"></param>
        /// <param name="formatter"></param>
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (string.IsNullOrEmpty(ip))
            {
                lock (ip)
                {
                    if (string.IsNullOrEmpty(ip))
                    {
                        ip = GetLocalIp();
                    }
                }
            }

            if (IsEnabled(logLevel))
            {
                try
                {
                    var message = new ActiveLoggerMessage <TState>
                    {
                        Category        = category,
                        Error           = ActiveLoggerError.Parse(exception),
                        LogLevel        = logLevel,
                        State           = state,
                        Message         = formatter?.Invoke(state, exception),
                        ApplicationName = loggerOptions.ApplicationName,
                        Time            = DateTime.Now,
                        IpAddress       = ip,
                    };
                    var json = loggerOptions.Serialize(message);
                    //发送消息
                    if (loggerOptions.UseQueue)
                    {
                        producer.Send(new ActiveMessage()
                        {
                            Destination  = loggerOptions.Destination,
                            IsPersistent = loggerOptions.IsPersistent,
                            Message      = json,
                            Properties   = loggerOptions.Properties,
                            TimeToLive   = loggerOptions.TimeToLive
                        });
                    }
                    else
                    {
                        producer.Publish(new ActiveMessage()
                        {
                            Destination  = loggerOptions.Destination,
                            IsPersistent = loggerOptions.IsPersistent,
                            Message      = json,
                            Properties   = loggerOptions.Properties,
                            TimeToLive   = loggerOptions.TimeToLive
                        });
                    }
                }
                catch (Exception ex)
                {
                    do
                    {
                        Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
                    } while ((ex = ex.InnerException) != null);
                };
            }
        }