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?.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 exception) { logEvent.MessageObject = JsonSerializableException.Create(exception); } 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; } foreach (var property in loggingEvent.Properties()) { logEvent.Properties.Add(property.Key, property.Value); } logEvent.Properties.Add("@timestamp", loggingEvent.TimeStamp.ToUniversalTime().ToString("O")); 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, StackTrace = ex.StackTrace, Data = ex.Data }; if (ex.InnerException != null) { serializable.InnerException = Create(ex.InnerException); } return(serializable); }