C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....'
UseHttpLogging을 활성화하는 경우,
namespace WebApplication1;
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseAuthorization();
app.UseHttpLogging();
app.MapControllers();
app.Run();
}
}
이런 오류가 발생합니다.
System.InvalidOperationException
HResult=0x80131509
Message=Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`1[Microsoft.AspNetCore.HttpLogging.HttpLoggingInterceptorContext]' while attempting to activate 'Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware'.
Source=Microsoft.AspNetCore.Http.Abstractions
StackTrace:
at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) in /_/src/Shared/ActivatorUtilities/ActivatorUtilities.cs:line 158
at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) in /_/src/Shared/ActivatorUtilities/ActivatorUtilities.cs:line 54
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.ReflectionMiddlewareBinder.CreateMiddleware(RequestDelegate next) in /_/src/Http/Http.Abstractions/src/Extensions/UseMiddlewareExtensions.cs:line 130
at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build() in /_/src/Http/Http/src/Builder/ApplicationBuilder.cs:line 197
at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build() in /_/src/Http/Http/src/Builder/ApplicationBuilder.cs:line 197
at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__40.MoveNext() in /_/src/Hosting/Hosting/src/GenericHost/GenericWebHostService.cs:line 143
at Microsoft.Extensions.Hosting.Internal.Host.<<StartAsync>b__15_1>d.MoveNext() in /_/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs:line 136
at Microsoft.Extensions.Hosting.Internal.Host.<ForeachService>d__18`1.MoveNext() in /_/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs:line 390
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__15.MoveNext() in /_/src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs:line 145
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext() in /_/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs:line 67
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext() in /_/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs:line 79
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host) in /_/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs:line 53
at WebApplication1.Program.Main(String[] args) in C:\temp\WebApplication1\WebApplication1\Program.cs:line 23
builder.Services.AddHttpLogging 호출을 잊으면 안 됩니다. ^^
// HTTP logging in ASP.NET Core
// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpLogging(o => {
o.LoggingFields = HttpLoggingFields.All & ~HttpLoggingFields.Duration;
});
builder.Services.AddControllers();
하지만, 위와 같이 해도 일반적인 Request/Response 내용은 콘솔 화면에 출력되지 않습니다. 왜냐하면 appSettings.json에 ASP.NET Core 관련 로그 레벨을 설정하지 않았기 때문입니다.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
},
"AllowedHosts": "*"
}
참고로, Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware (뿐만 아니라 대개의 마이크로소프트 구성요소의) 로깅 레벨은 "Warning"이 기본값입니다.
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]