private void SerializeMessageToJson(LoggerJsonMessage message) { using (Utf8JsonWriter Writer = new Utf8JsonWriter(_Buffer)) { try { JsonSerializer.Serialize(Writer, message, _JsonOptions); } catch (JsonException JsonException) { JsonSerializer.Serialize( Writer, new LoggerJsonMessage { LogLevel = message.LogLevel, TimestampUtc = message.TimestampUtc, ThreadId = message.ThreadId, EventId = message.EventId, GroupName = message.GroupName, CategoryName = message.CategoryName, Content = $"Message with Content [{message.Content}] contained data that could not be serialized into Json.", Exception = LoggerJsonMessageException.FromException(JsonException) }, _JsonOptions); } } _Buffer.WriteToStream(_OutputStream); _Buffer.Clear(); _OutputStream.Write(s_NewLine, 0, s_NewLine.Length); _OutputStream.Flush(); }
private string SerializeMessageToJson(LoggerJsonMessage message) { try { return(JsonSerializer.Serialize(message, _JsonOptions)); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception JsonException) #pragma warning restore CA1031 // Do not catch general exception types { return(JsonSerializer.Serialize( new LoggerJsonMessage { LogLevel = message.LogLevel, TimestampUtc = message.TimestampUtc, ThreadId = message.ThreadId, EventId = message.EventId, GroupName = message.GroupName, CategoryName = message.CategoryName, Content = $"Message with Content [{message.Content}] contained data that could not be serialized into Json.", Exception = LoggerJsonMessageException.FromException(JsonException) }, _JsonOptions)); } }
public void SerializeMessageToJson(LoggerJsonMessage message, Stream stream) { try { _Writer.Reset(); try { JsonSerializer.Serialize(_Writer, message, _Options); } catch (JsonException JsonException) { JsonSerializer.Serialize( _Writer, new LoggerJsonMessage { LogLevel = message.LogLevel, TimestampUtc = message.TimestampUtc, ThreadId = message.ThreadId, EventId = message.EventId, GroupName = message.GroupName, CategoryName = message.CategoryName, Content = $"Message with Content [{message.Content}] contained data that could not be serialized into Json.", Exception = LoggerJsonMessageException.FromException(JsonException) }, _Options); } finally { LoggerJsonMessage.Return(message); } _Buffer.WriteToStream(stream); stream.Write(s_NewLine, 0, s_NewLine.Length); stream.Flush(); } finally { _Buffer.Clear(); } }