@@ -28,6 +28,14 @@ internal sealed class TrebllePayloadFactory
2828 Encoder = System . Text . Encodings . Web . JavaScriptEncoder . UnsafeRelaxedJsonEscaping
2929 } ;
3030
31+ // Cache static system information to avoid repeated allocations
32+ private static readonly string CachedOSName = Environment . OSVersion . ToString ( ) ;
33+ private static readonly string CachedOSVersion = Environment . OSVersion . Version . ToString ( ) ;
34+ private static readonly string CachedArchitecture = RuntimeInformation . ProcessArchitecture . ToString ( ) ;
35+ private static readonly string CachedTimezone = ( ! string . IsNullOrEmpty ( TimeZoneInfo . Local . StandardName ) )
36+ ? TimeZoneInfo . Local . StandardName
37+ : "UTC" ;
38+
3139 private readonly TreblleOptions _treblleOptions ;
3240 private readonly ILogger < TrebllePayloadFactory > _logger ;
3341
@@ -90,16 +98,14 @@ private static void AddLanguage(TrebllePayload payload)
9098 private static void AddServer ( HttpContext httpContext , TrebllePayload payload )
9199 {
92100 payload . Data . Server . Ip = httpContext . Connection . LocalIpAddress ? . MapToIPv4 ( ) ? . ToString ( ) ?? "bogon" ;
93- payload . Data . Server . Timezone = ( ! string . IsNullOrEmpty ( TimeZoneInfo . Local . StandardName ) )
94- ? TimeZoneInfo . Local . StandardName
95- : "UTC" ;
101+ payload . Data . Server . Timezone = CachedTimezone ;
96102 payload . Data . Server . Software = httpContext . GetServerVariable ( "SERVER_SOFTWARE" ) ;
97103 payload . Data . Server . Signature = null ;
98104 payload . Data . Server . Protocol = httpContext . Request . Protocol ;
99105
100- payload . Data . Server . Os . Name = Environment . OSVersion . ToString ( ) ;
101- payload . Data . Server . Os . Release = Environment . OSVersion . Version . ToString ( ) ;
102- payload . Data . Server . Os . Architecture = RuntimeInformation . ProcessArchitecture . ToString ( ) ;
106+ payload . Data . Server . Os . Name = CachedOSName ;
107+ payload . Data . Server . Os . Release = CachedOSVersion ;
108+ payload . Data . Server . Os . Architecture = CachedArchitecture ;
103109 }
104110
105111 private async Task AddRequest ( HttpContext httpContext , TrebllePayload payload )
@@ -233,7 +239,7 @@ private async Task TryAddRequestBody(HttpContext httpContext, TrebllePayload pay
233239 }
234240 else if ( IsValidJson ( bodyData ) )
235241 {
236- payload . Data . Request . Body = JsonSerializer . Deserialize < JsonElement > ( bodyData , JsonOptions ) ;
242+ payload . Data . Request . Body = JsonSerializer . Deserialize < JsonElement > ( bodyData , TreblleJsonContext . Default . JsonElement ) ;
237243 }
238244 else
239245 {
@@ -250,8 +256,8 @@ private async Task TryAddRequestBody(HttpContext httpContext, TrebllePayload pay
250256 else if ( contentType . Contains ( "application/xml" , StringComparison . OrdinalIgnoreCase ) )
251257 {
252258 var doc = XDocument . Parse ( bodyData ) ;
253- var jsonText = JsonSerializer . Serialize ( ConvertXDocumentToObject ( doc ) , JsonOptions ) ;
254- payload . Data . Request . Body = JsonSerializer . Deserialize < JsonElement > ( jsonText , JsonOptions ) ;
259+ var jsonText = JsonSerializer . Serialize ( ConvertXDocumentToObject ( doc ) , TreblleJsonContext . Default . Object ) ;
260+ payload . Data . Request . Body = JsonSerializer . Deserialize < JsonElement > ( jsonText , TreblleJsonContext . Default . JsonElement ) ;
255261 }
256262 else
257263 {
@@ -318,7 +324,7 @@ private async Task TryAddResponse(HttpContext httpContext, MemoryStream? respons
318324 }
319325 else if ( IsValidJson ( responseContent ) )
320326 {
321- payload . Data . Response . Body = JsonSerializer . Deserialize < JsonElement > ( responseContent , JsonOptions ) ;
327+ payload . Data . Response . Body = JsonSerializer . Deserialize < JsonElement > ( responseContent , TreblleJsonContext . Default . JsonElement ) ;
322328 }
323329 else
324330 {
@@ -421,7 +427,7 @@ private bool IsValidJson(string str)
421427 {
422428 try
423429 {
424- JsonSerializer . Deserialize < JsonElement > ( str , JsonOptions ) ;
430+ JsonSerializer . Deserialize < JsonElement > ( str , TreblleJsonContext . Default . JsonElement ) ;
425431 return true ;
426432 }
427433 catch ( JsonException )
0 commit comments