Skip to content

Commit 00877b5

Browse files
committed
Create logger using string category
1 parent ff3cb76 commit 00877b5

28 files changed

+215
-40
lines changed

src/Http/Http.Results/src/AcceptedAtRouteHttpResult.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace Microsoft.AspNetCore.Http;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Routing;
88
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
910

1011
/// <summary>
1112
/// An <see cref="IResult"/> that on execution will write an object to the response
@@ -78,7 +79,11 @@ public Task ExecuteAsync(HttpContext httpContext)
7879
throw new InvalidOperationException("No route matches the supplied values.");
7980
}
8081

82+
// Creating the logger with a string to preserve the category after the refactoring.
83+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
84+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.AcceptedAtRouteResult");
85+
8186
httpContext.Response.Headers.Location = url;
82-
return HttpResultsHelper.WriteResultAsJsonAsync(httpContext, Value, StatusCode);
87+
return HttpResultsHelper.WriteResultAsJsonAsync(httpContext, logger, Value, StatusCode);
8388
}
8489
}

src/Http/Http.Results/src/AcceptedHttpResult.cs

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Microsoft.AspNetCore.Http;
55

66
using System.Threading.Tasks;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Logging;
79

810
/// <summary>
911
/// An <see cref="IResult"/> that on execution will write an object to the response
@@ -74,8 +76,12 @@ public Task ExecuteAsync(HttpContext httpContext)
7476
httpContext.Response.Headers.Location = Location;
7577
}
7678

79+
// Creating the logger with a string to preserve the category after the refactoring.
80+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
81+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.AcceptedResult");
7782
return HttpResultsHelper.WriteResultAsJsonAsync(
7883
httpContext,
84+
logger,
7985
Value,
8086
StatusCode);
8187
}

src/Http/Http.Results/src/BadRequestObjectHttpResult.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Http;
55

6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
69
/// <summary>
710
/// An <see cref="IResult"/> that on execution will write an object to the response
811
/// with Bad Request (400) status code.
@@ -32,5 +35,15 @@ internal BadRequestObjectHttpResult(object? error)
3235

3336
/// <inheritdoc/>
3437
public Task ExecuteAsync(HttpContext httpContext)
35-
=> HttpResultsHelper.WriteResultAsJsonAsync(httpContext, Value, StatusCode);
38+
{
39+
// Creating the logger with a string to preserve the category after the refactoring.
40+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
41+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.BadRequestObjectResult");
42+
43+
return HttpResultsHelper.WriteResultAsJsonAsync(
44+
httpContext,
45+
logger: logger,
46+
Value,
47+
StatusCode);
48+
}
3649
}

src/Http/Http.Results/src/ChallengeHttpResult.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ internal ChallengeHttpResult(IList<string> authenticationSchemes, Authentication
9090
/// <inheritdoc/>
9191
public async Task ExecuteAsync(HttpContext httpContext)
9292
{
93-
var logger = httpContext.RequestServices.GetRequiredService<ILogger<ChallengeHttpResult>>();
93+
// Creating the logger with a string to preserve the category after the refactoring.
94+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
95+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.ChallengeResult");
9496

9597
Log.ChallengeResultExecuting(logger, AuthenticationSchemes);
9698

src/Http/Http.Results/src/ConflictObjectHttpResult.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Http;
55

6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
69
/// <summary>
710
/// An <see cref="IResult"/> that on execution will write an object to the response
811
/// with Conflict (409) status code.
@@ -32,5 +35,15 @@ internal ConflictObjectHttpResult(object? error)
3235

3336
/// <inheritdoc/>
3437
public Task ExecuteAsync(HttpContext httpContext)
35-
=> HttpResultsHelper.WriteResultAsJsonAsync(httpContext, Value, StatusCode);
38+
{
39+
// Creating the logger with a string to preserve the category after the refactoring.
40+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
41+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.ConflictObjectResult");
42+
43+
return HttpResultsHelper.WriteResultAsJsonAsync(
44+
httpContext,
45+
logger: logger,
46+
Value,
47+
StatusCode);
48+
}
3649
}

src/Http/Http.Results/src/ContentHttpResult.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Http;
55

6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
69
/// <summary>
710
/// An <see cref="StatusCodeHttpResult"/> that when executed
811
/// will produce a response with content.
@@ -53,5 +56,11 @@ internal ContentHttpResult(string? content, string? contentType, int? statusCode
5356
/// <param name="httpContext">The <see cref="HttpContext"/> for the current request.</param>
5457
/// <returns>A task that represents the asynchronous execute operation.</returns>
5558
public Task ExecuteAsync(HttpContext httpContext)
56-
=> HttpResultsHelper.WriteResultAsContentAsync(httpContext, Content, StatusCode, ContentType);
59+
{
60+
// Creating the logger with a string to preserve the category after the refactoring.
61+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
62+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.ContentResult");
63+
64+
return HttpResultsHelper.WriteResultAsContentAsync(httpContext, logger, Content, StatusCode, ContentType);
65+
}
5766
}

src/Http/Http.Results/src/CreatedAtRouteHttpResult.cs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.AspNetCore.Http;
55

66
using Microsoft.AspNetCore.Routing;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Logging;
89

910
/// <summary>
1011
/// An <see cref="IResult"/> that on execution will write an object to the response
@@ -77,9 +78,14 @@ public Task ExecuteAsync(HttpContext httpContext)
7778
throw new InvalidOperationException("No route matches the supplied values.");
7879
}
7980

81+
// Creating the logger with a string to preserve the category after the refactoring.
82+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
83+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.CreatedAtRouteResult");
84+
8085
httpContext.Response.Headers.Location = url;
8186
return HttpResultsHelper.WriteResultAsJsonAsync(
8287
httpContext,
88+
logger,
8389
Value,
8490
StatusCode);
8591
}

src/Http/Http.Results/src/CreatedHttpResult.cs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Http;
55

6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
69
/// <summary>
710
/// An <see cref="IResult"/> that on execution will write an object to the response
811
/// with status code Created (201) and Location header.
@@ -69,8 +72,12 @@ public Task ExecuteAsync(HttpContext httpContext)
6972
httpContext.Response.Headers.Location = Location;
7073
}
7174

75+
// Creating the logger with a string to preserve the category after the refactoring.
76+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
77+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.CreatedResult");
7278
return HttpResultsHelper.WriteResultAsJsonAsync(
7379
httpContext,
80+
logger,
7481
Value,
7582
StatusCode);
7683
}

src/Http/Http.Results/src/FileContentHttpResult.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Internal;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
57
using Microsoft.Net.Http.Headers;
68

79
namespace Microsoft.AspNetCore.Http;
@@ -102,14 +104,21 @@ internal FileContentHttpResult(
102104
public ReadOnlyMemory<byte> FileContents { get; internal init; }
103105

104106
/// <inheritdoc/>
105-
public Task ExecuteAsync(HttpContext httpContext) =>
106-
HttpResultsHelper.WriteResultAsFileAsync(
107+
public Task ExecuteAsync(HttpContext httpContext)
108+
{
109+
// Creating the logger with a string to preserve the category after the refactoring.
110+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
111+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.FileContentResult");
112+
113+
return HttpResultsHelper.WriteResultAsFileAsync(
107114
httpContext,
115+
logger,
108116
FileDownloadName,
109117
FileLength,
110118
ContentType,
111119
EnableRangeProcessing,
112120
LastModified,
113121
EntityTag,
114122
(context, range, rangeLength) => FileResultHelper.WriteFileAsync(httpContext, FileContents, range, rangeLength));
123+
}
115124
}

src/Http/Http.Results/src/FileStreamHttpResult.cs

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.AspNetCore.Internal;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
57
using Microsoft.Net.Http.Headers;
68

79
namespace Microsoft.AspNetCore.Http;
@@ -113,10 +115,15 @@ internal FileStreamHttpResult(
113115
/// <inheritdoc/>
114116
public async Task ExecuteAsync(HttpContext httpContext)
115117
{
118+
// Creating the logger with a string to preserve the category after the refactoring.
119+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
120+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.FileStreamResult");
121+
116122
await using (FileStream)
117123
{
118124
await HttpResultsHelper.WriteResultAsFileAsync(
119125
httpContext,
126+
logger,
120127
FileDownloadName,
121128
FileLength,
122129
ContentType,

src/Http/Http.Results/src/ForbidHttpResult.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ internal ForbidHttpResult(IList<string> authenticationSchemes, AuthenticationPro
9090
/// <inheritdoc />
9191
public async Task ExecuteAsync(HttpContext httpContext)
9292
{
93-
var logger = httpContext.RequestServices.GetRequiredService<ILogger<ForbidHttpResult>>();
93+
// Creating the logger with a string to preserve the category after the refactoring.
94+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
95+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.ForbidResult");
9496

9597
Log.ForbidResultExecuting(logger, AuthenticationSchemes);
9698

src/Http/Http.Results/src/HttpResultsHelper.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.AspNetCore.Http;
5-
64
using System.Text;
75
using System.Text.Json;
86
using Microsoft.AspNetCore.Http.Extensions;
97
using Microsoft.AspNetCore.Internal;
108
using Microsoft.AspNetCore.Mvc;
11-
using Microsoft.Extensions.DependencyInjection;
129
using Microsoft.Extensions.Logging;
1310
using Microsoft.Net.Http.Headers;
1411

12+
namespace Microsoft.AspNetCore.Http;
1513
internal static partial class HttpResultsHelper
1614
{
1715
private const string DefaultContentType = "text/plain; charset=utf-8";
1816
private static readonly Encoding DefaultEncoding = Encoding.UTF8;
1917

2018
public static Task WriteResultAsJsonAsync(
2119
HttpContext httpContext,
20+
ILogger logger,
2221
object? value,
2322
int? statusCode,
2423
string? contentType = null,
2524
JsonSerializerOptions? jsonSerializerOptions = null)
2625
{
27-
Log.WritingResultAsJson(GetLogger(httpContext), value, statusCode);
26+
Log.WritingResultAsJson(logger, value, statusCode);
2827

2928
if (statusCode is { } code)
3029
{
@@ -45,6 +44,7 @@ public static Task WriteResultAsJsonAsync(
4544

4645
public static async Task WriteResultAsContentAsync(
4746
HttpContext httpContext,
47+
ILogger logger,
4848
string? content,
4949
int? statusCode,
5050
string? contentType = null)
@@ -65,7 +65,7 @@ public static async Task WriteResultAsContentAsync(
6565
response.StatusCode = code;
6666
}
6767

68-
Log.WritingResultAsContent(GetLogger(httpContext), resolvedContentType);
68+
Log.WritingResultAsContent(logger, resolvedContentType);
6969

7070
if (content != null)
7171
{
@@ -76,6 +76,7 @@ public static async Task WriteResultAsContentAsync(
7676

7777
public static Task WriteResultAsFileAsync(
7878
HttpContext httpContext,
79+
ILogger logger,
7980
string? fileDownloadName,
8081
long? fileLength,
8182
string contentType,
@@ -84,7 +85,6 @@ public static Task WriteResultAsFileAsync(
8485
EntityTagHeaderValue? entityTag,
8586
Func<HttpContext, RangeItemHeaderValue?, long, Task> writeOperation)
8687
{
87-
var logger = GetLogger(httpContext);
8888
fileDownloadName ??= string.Empty;
8989

9090
Log.WritingResultAsFile(logger, fileDownloadName);
@@ -159,13 +159,6 @@ public static void ApplyProblemDetailsDefaults(ProblemDetails problemDetails, in
159159
}
160160
}
161161

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-
169162
internal static partial class Log
170163
{
171164
public static void WritingResultAsJson(ILogger logger, object? value, int? statusCode)

src/Http/Http.Results/src/JsonHttpResult.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Text.Json;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
57

68
namespace Microsoft.AspNetCore.Http;
79

@@ -81,10 +83,17 @@ internal JsonHttpResult(object? value, int? statusCode, string? contentType, Jso
8183

8284
/// <inheritdoc/>
8385
public Task ExecuteAsync(HttpContext httpContext)
84-
=> HttpResultsHelper.WriteResultAsJsonAsync(
86+
{
87+
// Creating the logger with a string to preserve the category after the refactoring.
88+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
89+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.JsonResult");
90+
91+
return HttpResultsHelper.WriteResultAsJsonAsync(
8592
httpContext,
93+
logger,
8694
Value,
8795
StatusCode,
8896
ContentType,
8997
JsonSerializerOptions);
98+
}
9099
}

src/Http/Http.Results/src/NoContentHttpResult.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ internal NoContentHttpResult()
2727
/// <inheritdoc/>
2828
public Task ExecuteAsync(HttpContext httpContext)
2929
{
30-
var logger = httpContext.RequestServices.GetRequiredService<ILogger<NoContentHttpResult>>();
30+
// Creating the logger with a string to preserve the category after the refactoring.
31+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
32+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.NoContentResult");
33+
3134
HttpResultsHelper.Log.WritingResultAsStatusCode(logger, StatusCode);
3235

3336
httpContext.Response.StatusCode = StatusCode;

src/Http/Http.Results/src/NotFoundObjectHttpResult.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
namespace Microsoft.AspNetCore.Http;
55

6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
69
/// <summary>
710
/// An <see cref="IResult"/> that on execution will write an object to the response
811
/// with Not Found (404) status code.
@@ -31,5 +34,15 @@ internal NotFoundObjectHttpResult(object? value)
3134

3235
/// <inheritdoc/>
3336
public Task ExecuteAsync(HttpContext httpContext)
34-
=> HttpResultsHelper.WriteResultAsJsonAsync(httpContext, Value, StatusCode);
37+
{
38+
// Creating the logger with a string to preserve the category after the refactoring.
39+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
40+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.NotFoundObjectResult");
41+
42+
return HttpResultsHelper.WriteResultAsJsonAsync(
43+
httpContext,
44+
logger: logger,
45+
Value,
46+
StatusCode);
47+
}
3548
}

0 commit comments

Comments
 (0)