#author("2017-11-27T14:49:07+00:00","default:admin","admin") *NLog [#z3238bda] -[[Getting started with ASP.NET Core (csproj vs2017):https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)]] --[[Getting started with ASP.NET Core 2:https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2]] -[[ASP.NET Core Logging Tutorial – What Still Works and What Changed?:https://stackify.com/asp-net-core-logging-what-changed/]] **セットアップ [#a8c75994] -NuGet で [[NLog.Web.AspNetCore:https://www.nuget.org/packages/NLog.Web.AspNetCore]] をインストール -プロジェクト直下に「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"> <!-- Load the ASP.NET Core plugin --> <extensions> <add assembly="NLog.Web.AspNetCore"/> </extensions> <!-- the targets to write to --> <targets> <!-- write logs to file --> <target xsi:type="File" name="allfile" fileName="c:\var\log\nlog-all-${shortdate}.log" layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <!-- another file log, only own logs. Uses some ASP.NET core renderers --> <target xsi:type="File" name="ownFile-web" fileName="c:\var\log\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 to map from logger name to target --> <rules> <!--All logs, including from Microsoft--> <logger name="*" minlevel="Trace" writeTo="allfile" /> <!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> </rules> </nlog> -nlog.config のプロパティを開き、直接出力を「常に出力」にする -Startup.cs を変更 public Startup(IConfiguration configuration, IHostingEnvironment env) { Configuration = configuration; env.ConfigureNLog("nlog.config"); } public void ConfigureServices(IServiceCollection Services) { //call this in case you need aspnet-user-authtype/aspnet-user-identity services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); } // 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) { //add NLog to ASP.NET Core loggerFactory.AddNLog(); //add NLog.Web app.AddNLogWeb(); -ログ出力 public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { _logger.Fatal("致命的レベル"); _logger.Error("エラーレベル"); _logger.Warn("警告レベル"); _logger.Info("情報レベル"); _logger.Debug("デバッグレベル"); _logger.Trace("トレースレベル"); return View(); } -ログ確認 > Get-Content C:\var\log\nlog-own-2017-11-27.log -wait -tail 10 -Encoding UTF8 *[[Serilog:https://serilog.net]] [#q943c57e] -[[ASP.NET Core~SerilogからSeqでロギングしてslackに通知する:http://ryuichi111std.hatenablog.com/entry/2016/07/20/111015]]