Exemplo n.º 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       = ex.Data
            };

            if (ex.InnerException != null)
            {
                serializable.InnerException = JsonSerializableException.Create(ex.InnerException);
            }
            return(serializable);
        }
        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);
        }