【发布时间】:2019-10-13 13:11:15
【问题描述】:
我创建了自己的自定义日志提供程序 FileLogerProviderr 来将日志保存到文件中。
我在 program.cs 中添加了自定义 FileLoggerProvider:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddProvider(new FileLoggerProvider(Path.Combine(Directory.GetCurrentDirectory(), "logger.txt")));
logging.SetMinimumLevel(LogLevel.None);
})
.UseStartup<Startup>();
为了测试我添加了登录控制器:
public class HomeController : Controller
{
private ILogger<HomeController> logger;
public HomeController(ILogger<HomeController> log)
{
logger = log;
}
public IActionResult Index()
{
logger.LogDebug($"test index path!!!");
return View();
}
}
工作,但在 test index path 之外的文件中,我看到了许多其他信息。我不需要这些信息。我只需要将控制器的日志保存在文件中。
Request starting HTTP/1.1 GET http://localhost:44339/
Route matched with {action = "Index", controller = "Home", page = "", area = ""}. Executing action LoggingTest.Controllers.HomeController.Index (LoggingTest)
Executing action method LoggingTest.Controllers.HomeController.Index (LoggingTest) - Validation state: Valid
test index path!!!
Executed action method LoggingTest.Controllers.HomeController.Index (LoggingTest), returned result Microsoft.AspNetCore.Mvc.ViewResult in 11.8159ms.
Executing ViewResult, running view Index.
Executed ViewResult - view Index executed in 43.4928ms.
Executed action LoggingTest.Controllers.HomeController.Index (LoggingTest) in 224.1184ms
Request finished in 400.2906ms 200 text/html; charset=utf-8
Request starting HTTP/1.1 GET http://localhost:44339/favicon.ico
Sending file. Request path: '/favicon.ico'. Physical path: 'C:\Users\dborovsky\projects\LoggingTest\LoggingTest\wwwroot\favicon.ico'
Request finished in 39.8394ms 200 image/x-icon
我该如何解决这个问题?谢谢你
更新 appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "CONSTRING"
},
"Logging": {
"LogLevel": {
"Default": "None",
"System": "None",
"Microsoft": "None"
}
},
"AllowedHosts": "*"
}
【问题讨论】:
-
可能重复:stackoverflow.com/questions/35251078/… 如果您认为不是,请发布您的 appsettings.json 文件
-
我已经更新了 appsetings.josn。请检查。我只是设置了无。
-
1.您没有通过
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));添加日志记录配置 2。确保您已更改appsettings.Development.json(或appsettings.Production.json,如果在生产环境中)。