我创建了一个新的dotnet核心项目,该项目发布:
dotnet new mvc -au none -o aspnet_app
然后按照本指南向应用程序添加日志记录
所以我将COnfigureLogging添加到Program.BuildWebHost
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostContext, logging) =>
{
logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
.UseStartup<Startup>()
.Build();
并且还修改了Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddMvc();
}
但是当我在HomeController中尝试使用它时:
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger) {
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index action!");
return View();
}
我收到以下错误:
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
An unhandled exception has occurred: Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'aspnet_app.Controllers.HomeController'.
--
使用dotnet core 2.1.105
智慧大石
相关分类