Try   HackMD

NLog 產生的 Log 檔路徑

tags: ASP.NET C# NLog

前言

NLog 是一個基於 .NET 平台編寫的 Log 工具,公司 C# 專案常常會用到它。但每次設定好 NLog 後,總是找不到 NLog 產生的 Log 檔路徑在哪裡,所以寫一篇筆記記錄一下。(這篇要感謝 Mitti 大大,因為每次找不到 Log 檔,我都去問他)

設定

  1. 從 NuGet 下載 NLog 之後,必須先在 NLog.config 裡面設定 Log 檔路徑,如下面 XML 程式碼。
  2. 請注意:下面程式碼中、「[變數] 檔案位置」的 ${basedir}-不同類型的專案,${basedir} 所代表的路徑不同。
<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!--[變數] 檔案位置 -->
  <variable name="LogTxtLocation" value="${basedir}/Logs/${shortdate}.log"/>

  <!--[設定] 寫入目標-->
  <targets>
    <target xsi:type="File" name="logFile" fileName="${LogTxtLocation}"
                layout="${longdate} ${uppercase:${level}} ${message}" />
  </targets>

  <!--[設定] 紀錄規則-->
  <rules>
    <logger name="*" minlevel="Debug" writeTo="logFile" />
  </rules>
</nlog>

ASP.NET Web 應用程式(ASP.NET Web Application)

如果是 ASP.NET Web 應用程式(ASP.NET Web Application),${basedir} 是代表「ASP.NET Web 應用程式的根目錄」。

以下面 WebApplication1 專案為例:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • ${basedir}即 WebApplication1 的根目錄-E:\WebApplication1\WebApplication1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 所以 ${basedir}/Logs 路徑就是-E:\WebApplication1\WebApplication1\Logs

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

主控台應用程式(Console App):

如果是主控台應用程式(Console App):

  • 【Debug 模式】:${basedir} 是「主控台應用程式的根目錄/bin/debug」。
  • 【Release 模式】:${basedir} 是「主控台應用程式的根目錄/bin/release」。

以下面 ConsoleApp1 專案為例:

  • 在下圖紅框處調整「Debug 模式」或「Release 模式」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 主控台應用程式的根目錄-E:\ConsoleApp1\ConsoleApp1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

【Debug 模式】:${basedir} =「主控台應用程式的根目錄/bin/debug」:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

【Release 模式】:${basedir} =「主控台應用程式的根目錄/bin/release」:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →