NLog

セットアップ

  • NuGetNLog.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

ログレベル

  • Trace
    • プロトコルのペイロードなど大量で詳細なデータを出力するときに使用する。開発中のみ有効
  • Debug
    • Traceレベルよりも詳細ではないデバック中のログを出力するときに使用する。開発中のみ有効
  • Info
    • 情報メッセージ。稼働環境で有効
  • Warn
    • 警告メッセージ。回復可能であるか、または一時的な障害に関する警告メッセージを出力する。
  • Error
    • エラーメッセージ。Exseption情報を出力する。
  • Fatal
    • 非常に重大なエラーメッセージ。

ローテーション

メール送信

.Net Core で動かない

Serilog


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-01-09 (木) 19:51:35 (1567d)