예제 #1
0
        public static JsonSerializableException Create(Exception ex)
        {
            if (ex == null)
            {
                return(null);
            }

            var serializable = new JsonSerializableException
            {
                Type     = ex.GetType().FullName,
                Message  = ex.Message,
                HelpLink = ex.HelpLink,
                Source   = ex.Source,
#if NET45
                HResult = ex.HResult,
#endif
                StackTrace = ex.StackTrace,
                Data       = ToStringDictionary(ex.Data)
            };

            if (ex.InnerException != null)
            {
                serializable.InnerException = JsonSerializableException.Create(ex.InnerException);
            }
            return(serializable);
        }
예제 #2
0
        static logEvent Create(LoggingEvent loggingEvent)
        {
            var logEvent = new logEvent
            {
                loggerName = loggingEvent.LoggerName,
                domain     = loggingEvent.Domain,
                identity   = loggingEvent.Identity,
                threadName = loggingEvent.ThreadName,
                userName   = loggingEvent.UserName,
                timeStamp  = loggingEvent.TimeStamp.ToUniversalTime().ToString("O"),
                exception  = loggingEvent.ExceptionObject == null ? new object() : JsonSerializableException.Create(loggingEvent.ExceptionObject),
                message    = loggingEvent.RenderedMessage,
                fix        = loggingEvent.Fix.ToString(),
                hostName   = Environment.MachineName,
                level      = loggingEvent.Level == null ? null : loggingEvent.Level.DisplayName
            };

            // Added special handling of the MessageObject since it may be an exception.
            // Exception Types require specialized serialization to prevent serialization exceptions.
            if (loggingEvent.MessageObject != null && loggingEvent.MessageObject.GetType() != typeof(string))
            {
                if (loggingEvent.MessageObject is Exception)
                {
                    logEvent.messageObject = JsonSerializableException.Create((Exception)loggingEvent.MessageObject);
                }
                else
                {
                    logEvent.messageObject = loggingEvent.MessageObject;
                }
            }
            else
            {
                logEvent.messageObject = new object();
            }

            if (loggingEvent.LocationInformation != null)
            {
                logEvent.className  = loggingEvent.LocationInformation.ClassName;
                logEvent.fileName   = loggingEvent.LocationInformation.FileName;
                logEvent.lineNumber = loggingEvent.LocationInformation.LineNumber;
                logEvent.fullInfo   = loggingEvent.LocationInformation.FullInfo;
                logEvent.methodName = loggingEvent.LocationInformation.MethodName;
            }

            AddProperties(loggingEvent, logEvent);

            return(logEvent);
        }
        public static JsonSerializableException Create(Exception ex)
        {
            if (ex == null)
                return null;

            var serializable = new JsonSerializableException
            {
                Type = ex.GetType().FullName,
                Message = ex.Message,
                HelpLink = ex.HelpLink,
                Source = ex.Source,
#if NET45
                HResult = ex.HResult,
#endif
                StackTrace = ex.StackTrace,
                Data = ex.Data
            };

            if (ex.InnerException != null)
            {
                serializable.InnerException = JsonSerializableException.Create(ex.InnerException);
            }
            return serializable;
        }
예제 #4
0
        static logEvent Create(LoggingEvent loggingEvent)
        {
            var logEvent = new logEvent
            {
                //loggerName = loggingEvent.LoggerName,
                domain = loggingEvent.Domain,
                //identity = loggingEvent.Identity,
                threadName = loggingEvent.ThreadName,
                //userName = loggingEvent.UserName,
                timeStamp = loggingEvent.TimeStamp.ToUniversalTime().ToString("O"),
                exception = loggingEvent.ExceptionObject == null ? new object() : JsonSerializableException.Create(loggingEvent.ExceptionObject),
                message   = loggingEvent.RenderedMessage,
                //fix = loggingEvent.Fix.ToString(),
                hostName = Environment.MachineName,
                level    = loggingEvent.Level == null ? null : loggingEvent.Level.DisplayName.ToLower()
            };

            #region 扩展 接口级日志
            MangoLogEvent mangoLogEvent = null;
            try
            {
                if (loggingEvent.MessageObject != null)
                {
                    if (loggingEvent.MessageObject.GetType() == typeof(string))
                    {
                        mangoLogEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <MangoLogEvent>(loggingEvent.MessageObject.ToString());
                    }
                    else
                    {
                        mangoLogEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <MangoLogEvent>(loggingEvent.MessageObject.ToJson());
                    }
                    if (mangoLogEvent != null)
                    {
                        logEvent.ltctraid = mangoLogEvent.uuid;
                        logEvent.module   = mangoLogEvent.module;
                        logEvent.uuidtag  = mangoLogEvent.uuidtag;
                        logEvent.message  = mangoLogEvent.message;
                        logEvent.request  = mangoLogEvent.request;
                        logEvent.response = mangoLogEvent.response;
                        if (mangoLogEvent.otherMsg.Count > 0)
                        {
                            logEvent.otherMsg = mangoLogEvent.otherMsg;
                        }
                        else
                        {
                            logEvent.otherMsg = new Dictionary <string, string>();
                        }
                        //类信息
                        //logEvent.className = mangoLogEvent.className;
                        //logEvent.fileName = mangoLogEvent.fileName;
                        //logEvent.lineNumber = mangoLogEvent.lineNumber.ToString();
                        //logEvent.methodName = mangoLogEvent.methodName;
                        logEvent.fullInfo = mangoLogEvent.fullInfo;


                        //上下文信息
                        if (!string.IsNullOrEmpty(mangoLogEvent.SERVER_PORT))
                        {
                            var ipParam = mangoLogEvent.LOCAL_ADDR.Split('.');
                            logEvent.hostId = mangoLogEvent.HTTP_HOST;
                            if (ipParam.Length == 4)
                            {
                                //logEvent.hostId = ipParam[3] + "-" + mangoLogEvent.SERVER_PORT;
                                logEvent.hostId = mangoLogEvent.LOCAL_ADDR + ":" + mangoLogEvent.SERVER_PORT;
                            }
                        }
                        if (!string.IsNullOrEmpty(mangoLogEvent.SERVER_NAME))
                        {
                            logEvent.domain = mangoLogEvent.SERVER_NAME;
                        }

                        //项目名
                        logEvent.project = logEvent.hostId;
                        if (!string.IsNullOrEmpty(mangoLogEvent.Appl_Physical_Path))
                        {
                            var appl_Path_Param = mangoLogEvent.Appl_Physical_Path.Split('\\');
                            if (appl_Path_Param.Length < 2)
                            {
                                appl_Path_Param = mangoLogEvent.Appl_Physical_Path.Split('/');
                                if (appl_Path_Param.Length > 3)
                                {
                                    logEvent.project = appl_Path_Param[3];
                                }
                            }
                            else if (appl_Path_Param.Length > 3)
                            {
                                logEvent.project = appl_Path_Param[2];
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            #endregion 扩展 接口级日志

            //if (loggingEvent.LocationInformation != null)
            //扩展 新增了判断条件
            if (string.IsNullOrEmpty(logEvent.fullInfo) && loggingEvent.LocationInformation != null)
            {
                //logEvent.className = loggingEvent.LocationInformation.ClassName;
                //logEvent.fileName = loggingEvent.LocationInformation.FileName;
                //logEvent.lineNumber = loggingEvent.LocationInformation.LineNumber;
                logEvent.fullInfo = loggingEvent.LocationInformation.FullInfo;
                //logEvent.methodName = loggingEvent.LocationInformation.MethodName;
            }

            AddProperties(loggingEvent, logEvent);
            //throw new Exception("终止提交ES");

            return(logEvent);
        }