.net core 配置 NLOG的两种方式

方式1. Startup.cs 中配置

public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
        {            
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            ////Config NLog
            loggerFactory.AddNLog();
            app.AddNLogWeb();
            loggerFactory.ConfigureNLog("nlog.config");

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

方式2. Program.cs中配置

 public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup()
                .UseNLog()  //Config NLog
                .Build();
    }

使用(注意要用泛型!):

  public class VisitorController : Controller
    {
        private readonly VisitorSysDbContext _apiDBContent;
        private IInitService _initService;
        private IVisitorService _visitorService;
        private readonly ILogger _logger;
        public VisitorController(VisitorSysDbContext apiDBContent, IInitService initService, IVisitorService visitorService,ILogger logger) {
            this._apiDBContent = apiDBContent;
            this._initService = initService;
            this._visitorService = visitorService;
            this._logger = logger;
        }

        
        [HttpGet("Users")]
        public JsonResult Users() {
            this._logger.Log(LogLevel.Error,"this is my first NLOG!");
            return Json(this._visitorService.GetUsers());
        }

 

你可能感兴趣的:(.net core 配置 NLOG的两种方式)