32 var buffer = MemoryPool<byte>.Shared.Rent(65535);
37 Write(
id, level, message, args, exception, location,
ref wrt, writer.Encoding);
38 writer.Write(writer.Encoding.GetString(buffer.Memory.Span[..wrt.WrittenCount]));
58 var buffer = MemoryPool<byte>.Shared.Rent(65535);
63 Write(
id, level, message, args, exception, location,
ref wrt, writer.Encoding);
64 await writer.WriteAsync(writer.Encoding.GetString(buffer.Memory.Span[..wrt.WrittenCount]));
86 where TWriter : IBufferWriter<
byte>
88 encoding ??= Encoding.UTF8;
90 var mem =
default(IMemoryOwner<byte>);
91 var buf = (IBufferWriter<byte>)writer;
96 if (encoding is not UTF8Encoding)
98 mem = MemoryPool<byte>.Shared.Rent(65535);
102 using var json =
new Utf8JsonWriter(buf,
new JsonWriterOptions() { Indented =
false });
106 json.WriteStartObject();
107 json.WriteNumber(
"id",
id);
108 json.WriteString(
"level", level
switch
110 DiagnosticLevel.Trace =>
"trace",
111 DiagnosticLevel.Info =>
"info",
112 DiagnosticLevel.Warning =>
"warning",
113 DiagnosticLevel.Error =>
"error",
114 DiagnosticLevel.Fatal =>
"fatal",
115 _ =>
throw new NotImplementedException(),
118 json.WriteString(
"message", message);
119 json.WriteStartArray(
"args");
122 foreach (var arg
in args)
123 JsonSerializer.Serialize(json, arg, arg?.GetType() ?? typeof(
object), JsonSerializerOptions.Default);
125 json.WriteEndArray();
127 if (location.Path !=
null ||
128 location.StartLine != 0 ||
129 location.StartColumn != 0 ||
130 location.EndLine != 0 ||
131 location.EndColumn != 0)
133 json.WriteStartObject(
"location");
134 json.WriteString(
"path", location.Path);
135 json.WriteStartArray(
"position");
136 json.WriteNumberValue(location.StartLine);
137 json.WriteNumberValue(location.StartColumn);
138 json.WriteNumberValue(location.EndLine);
139 json.WriteNumberValue(location.EndColumn);
140 json.WriteEndArray();
142 json.WriteEndObject();
145 json.WriteEndObject();
154 var len = Encoding.UTF8.GetCharCount(mem.Memory.Span[..(
int)json.BytesCommitted]);
155 using var tmp = MemoryPool<char>.Shared.Rent(len);
156 len = Encoding.UTF8.GetChars(mem.Memory.Span[..len], tmp.Memory.Span);
159 encoding.GetBytes(tmp.Memory.Span[..len], writer);
162 catch (ArgumentOutOfRangeException)