- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2017-11-27T14:49:07+00:00","default:admin","admin")
#author("2020-01-09T10:51:35+00:00","default:admin","admin")
*NLog [#z3238bda]
-[[Configuration options:https://nlog-project.org/config/]]
-[[.NET CoreアプリケーションでNLogを使う:http://ohke.hateblo.jp/entry/2017/02/24/221702]]
-[[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/]]
-[[NLog 設定項目 詳細:http://dotnetcsharptips.seesaa.net/article/419883987.html]]
-[[【備忘録】NLog よく使う出力レイアウト:https://qiita.com/shusakuorz/items/47be13a66a7c93cef460]]
**セットアップ [#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
**ログレベル [#i1f8c6c4]
-Trace
--プロトコルのペイロードなど大量で詳細なデータを出力するときに使用する。開発中のみ有効
-Debug
--Traceレベルよりも詳細ではないデバック中のログを出力するときに使用する。開発中のみ有効
-Info
--情報メッセージ。稼働環境で有効
-Warn
--警告メッセージ。回復可能であるか、または一時的な障害に関する警告メッセージを出力する。
-Error
--エラーメッセージ。Exseption情報を出力する。
-Fatal
--非常に重大なエラーメッセージ。
**ローテーション [#hde80f18]
-[[NLog の archiveNumbering="Rolling" を使う:http://mk3008net.hatenablog.com/entry/2014/12/14/102254]]
-[[エラー処理 – NLogによるログファイルのローテーションとクリーンアップ:https://codeday.me/jp/qa/20190123/174619.html]]
**メール送信 [#b5daeb63]
-[[Mail target:https://github.com/nlog/NLog/wiki/Mail-target]]
-[[【C#】NLogでMail通知:https://kageura.hatenadiary.jp/entry/2015/03/31/001631]]
-[[Using NLog with GMail:https://github.com/NLog/NLog/wiki/Examples]]
***.Net Core で動かない [#kffd2785]
-[[Target cannot be found: 'Mail':https://github.com/NLog/NLog/issues/1718]]
-[[NLog.MailKit:https://github.com/NLog/NLog.MailKit]] のパッケージが必要
*[[Serilog:https://serilog.net]] [#q943c57e]
-[[ASP.NET Core~SerilogからSeqでロギングしてslackに通知する:http://ryuichi111std.hatenablog.com/entry/2016/07/20/111015]]