使用 NLog 涵蓋所有 IIS Logs 可以記錄的事項


  1. 說明
  2. 參考資料

說明如何使用 NLog 涵蓋所有 IIS Logs 可以記錄的資訊欄位,包含 cs(Referer)、sc-status、cs-uri-stem,並處理 IIS Logs 無法蒐錄的資訊欄位,例如 Post Data 及 Form Collections 等。

logo

說明

IIS Logs Meaning NLog Renderers
date 日期 UTC ${date}
time 時間 UTC ${time}
s-ip 伺服器 IP ${local-ip}
cs-method HTTP Method (GET, POST) {aspnet-request-method}
cs-uri-stem URI 相對路徑 {aspnet-request-url}
cs-uri-query URI 參數 {aspnet-request-url}
s-port 伺服器 Port 😧
cs-username 使用者名稱 ${aspnet-user-identity} or ${identity}
c-ip 使用者 IP ${aspnet-request-ip}
cs(User-Agent) 瀏覽器、作業系統資訊 ${aspnet-request-useragent}
cs(Referer) 來源位址 ${aspnet-request-referrer}
sc-status Http Status ${aspnet-response-statuscode}
sc-substatus 😧
sc-win32-status 顯示各種錯誤狀態的代碼,0 表示為正常 😧
time-taken Byte 傳送時間,單位為 ms,1000 為 1 秒 😧

sc-win32-status 以及 time-taken 是 IIS Logs 的強項,NLog 的沒有相對應的 Renderers,而雖然沒有 Renderers 但 NLog 仍是可以藉由 .NET Framework 並使用 Properties 的方式來創造無法與 IIS Logs 對應的資料欄位,例如 s-port、sc-substatus。

相對地 NLog 也有許多勝過 IIS Logs 可以蒐集的資料欄位:

Renderers Usage
${exception} .NET 所觸發的 Exception 資訊
${environment} 取得系統的環境變數
${windows-identity} 取得 w3wp Thread 負責執行的身分
${aspnet-request-form} Request form 當中的內容,即 Post payload 中的 Data
${aspnet-request-headers} ASP.NET Header 的 key / value 資料

參考資料

NLog Configuration options