我创建了一个新的dotnet核心项目,该项目发布:dotnet new mvc -au none -o aspnet_app然后按照本指南向应用程序添加日志记录所以我将COnfigureLogging添加到Program.BuildWebHostpublic static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostContext, logging) => { logging.AddConfiguration(hostContext.Configuration.GetSection("Logging")); logging.AddConsole(); }) .UseStartup<Startup>() .Build();并且还修改了Startup.cspublic 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
1 回答
智慧大石
TA贡献1946条经验 获得超3个赞
您正在对它进行编码。 CreateDefaultBuilder生成自动获取
hostContext.Configuration.GetSection(“ Logging”)的记录器, 您实际上不需要在program.cs或启动中添加注释。
只需编辑基本项目并添加它,它将起作用。
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index action!");
return View();
}
- 1 回答
- 0 关注
- 123 浏览
添加回答
举报
0/150
提交
取消