1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- namespace Microsoft . AspNetCore . Http ;
5
-
6
4
using System . Text ;
7
5
using System . Text . Json ;
8
6
using Microsoft . AspNetCore . Http . Extensions ;
9
7
using Microsoft . AspNetCore . Internal ;
10
8
using Microsoft . AspNetCore . Mvc ;
11
- using Microsoft . Extensions . DependencyInjection ;
12
9
using Microsoft . Extensions . Logging ;
13
10
using Microsoft . Net . Http . Headers ;
14
11
12
+ namespace Microsoft . AspNetCore . Http ;
15
13
internal static partial class HttpResultsHelper
16
14
{
17
15
private const string DefaultContentType = "text/plain; charset=utf-8" ;
18
16
private static readonly Encoding DefaultEncoding = Encoding . UTF8 ;
19
17
20
18
public static Task WriteResultAsJsonAsync (
21
19
HttpContext httpContext ,
20
+ ILogger logger ,
22
21
object ? value ,
23
22
int ? statusCode ,
24
23
string ? contentType = null ,
25
24
JsonSerializerOptions ? jsonSerializerOptions = null )
26
25
{
27
- Log . WritingResultAsJson ( GetLogger ( httpContext ) , value , statusCode ) ;
26
+ Log . WritingResultAsJson ( logger , value , statusCode ) ;
28
27
29
28
if ( statusCode is { } code )
30
29
{
@@ -45,6 +44,7 @@ public static Task WriteResultAsJsonAsync(
45
44
46
45
public static async Task WriteResultAsContentAsync (
47
46
HttpContext httpContext ,
47
+ ILogger logger ,
48
48
string ? content ,
49
49
int ? statusCode ,
50
50
string ? contentType = null )
@@ -65,7 +65,7 @@ public static async Task WriteResultAsContentAsync(
65
65
response . StatusCode = code ;
66
66
}
67
67
68
- Log . WritingResultAsContent ( GetLogger ( httpContext ) , resolvedContentType ) ;
68
+ Log . WritingResultAsContent ( logger , resolvedContentType ) ;
69
69
70
70
if ( content != null )
71
71
{
@@ -76,6 +76,7 @@ public static async Task WriteResultAsContentAsync(
76
76
77
77
public static Task WriteResultAsFileAsync (
78
78
HttpContext httpContext ,
79
+ ILogger logger ,
79
80
string ? fileDownloadName ,
80
81
long ? fileLength ,
81
82
string contentType ,
@@ -84,7 +85,6 @@ public static Task WriteResultAsFileAsync(
84
85
EntityTagHeaderValue ? entityTag ,
85
86
Func < HttpContext , RangeItemHeaderValue ? , long , Task > writeOperation )
86
87
{
87
- var logger = GetLogger ( httpContext ) ;
88
88
fileDownloadName ??= string . Empty ;
89
89
90
90
Log . WritingResultAsFile ( logger , fileDownloadName ) ;
@@ -159,13 +159,6 @@ public static void ApplyProblemDetailsDefaults(ProblemDetails problemDetails, in
159
159
}
160
160
}
161
161
162
- private static ILogger GetLogger ( HttpContext httpContext )
163
- {
164
- var factory = httpContext . RequestServices . GetRequiredService < ILoggerFactory > ( ) ;
165
- var logger = factory . CreateLogger ( typeof ( HttpResultsHelper ) ) ;
166
- return logger ;
167
- }
168
-
169
162
internal static partial class Log
170
163
{
171
164
public static void WritingResultAsJson ( ILogger logger , object ? value , int ? statusCode )
0 commit comments