参考:NLog Getting started with ASP.NET Core 2

    在 Program 中 UseNLog:

    1. using System;
    2. using Microsoft.AspNetCore;
    3. using Microsoft.AspNetCore.Hosting;
    4. using Microsoft.Extensions.Logging;
    5. using NLog.Web;
    6. namespace StudentManagement
    7. {
    8. public class Program
    9. {
    10. public static void Main(string[] args)
    11. {
    12. // NLog: setup the logger first to catch all errors
    13. var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
    14. try
    15. {
    16. logger.Debug("init main");
    17. CreateWebHostBuilder(args).Build().Run();
    18. }
    19. catch (Exception ex)
    20. {
    21. //NLog: catch setup errors
    22. logger.Error(ex, "Stopped program because of exception");
    23. throw;
    24. }
    25. finally
    26. {
    27. // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
    28. NLog.LogManager.Shutdown();
    29. }
    30. }
    31. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    32. WebHost.CreateDefaultBuilder(args)
    33. .UseStartup<Startup>()
    34. .ConfigureLogging(logging =>
    35. {
    36. logging.ClearProviders();
    37. logging.AddConsole();
    38. logging.AddDebug();
    39. logging.AddEventSourceLogger();
    40. logging.SetMinimumLevel(LogLevel.Trace);
    41. })
    42. .UseNLog(); // NLog: setup NLog for Dependency injection
    43. }
    44. }