控制台输出日志。
安装Microsoft.Extensions.LoggingMicrosoft.Extensions.Logging.Console

简单使用

  1. static void Main(string[] args)
  2. {
  3. ServiceCollection sc=new ServiceCollection();
  4. sc.AddLogging(logBuilder=>logBuilder.AddConsole());
  5. sc.AddScoped<Test>();
  6. var sp = sc.BuildServiceProvider();
  7. Test test = sp.GetRequiredService<Test>();
  8. test.logTest();
  9. }
  10. public class Test
  11. {
  12. private ILogger<Test> log;
  13. public Test(ILogger<Test> log)
  14. {
  15. this.log = log;
  16. }
  17. public void logTest()
  18. {
  19. log.LogDebug("Debug:开始调试");
  20. log.LogInformation("info:默认日志");
  21. log.LogWarning("warn:警告日志");
  22. log.LogError("Error:错误日志");
  23. }
  24. }