NLog是一個基於.NET平台編寫的類庫,我們可以使用NLog在應用程序中添加極為完善的跟蹤調試代碼。
NLog是一個簡單靈活的.NET日志記錄類庫。通過使用NLog,我們可以在任何一種.NET語言中輸出帶有上下文的(contextual information)調試診斷信息,根據喜好配置其表現樣式之后發送到一個或多個輸出目標(target)中。
NLog的API非常類似於
log4net,且配置方式非常簡單。NLog使用路由表(routing table)進行配置,但log4net卻使用層次性的appender配置,這樣就讓NLog的配置文件非常容易閱讀,並便於今后維護。
如何使用?
1.創建一個新的ASP.NET Core項目
2.添加項目依賴
3.在項目目錄下添加nlog.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- 加載ASP.NET Core插件 -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- 輸出目的地 -->
<targets>
<!-- 輸出到文件,這個文件記錄所有日志 -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<!-- 另外一個日志記錄文件,戶口也跳過Microsoft開頭相關日志信息 -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<!-- write to the void aka just remove -->
<target xsi:type="Null" name="blackhole" />
</targets>
<!-- 寫入目的地的規則 -->
<rules>
<!--全部記錄,包括Microsoft開頭的相關日志信息-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--跳過Microsoft開頭的相關日志信息-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
3.將nlog.config復制到bin文件夾

4.在startup.cs文件中添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using NLog.Web;
namespace NlogDemo
{
public class Startup
{
public Startup(IConfiguration configuration,IHostingEnvironment env)
{
Configuration = configuration;
//配置nlog
env.ConfigureNLog("nlog.config");
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//把nlog添加到.net core
loggerFactory.AddNLog();
//add NLog.Web
app.AddNLogWeb();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
5.記錄日志
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index page says hello");
return View();
}
總結:
在我自己學習Nlog的時候,少了關鍵的一部那就是第三步,講config輸出到bin!!!
