寫程式紀錄Log好工具_NLog

開發時Log紀錄工具,還有另外一個log4net也式之前用過的~

1.NuGet下載KLog & KLog.config

2.修改KLog.config

原本的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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
  <targets>

    <!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->

    <!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
  </targets>

  <rules>
    <!-- add your logging rules here -->

    <!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
  </rules>
</nlog>

把它改成這樣

<?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">

    <!--[變數] 文字樣板 -->
    <variable name="Layout" value="${longdate} | ${level:uppercase=true} | ${logger} | ${message} ${newline}"/>
    <variable name="LayoutFatal" value="${longdate} | ${level:uppercase=true} | ${logger} | ${message} | ${exception:format=tostring} ${newline}"/>

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

    <!--[設定] 寫入目標-->
    <targets>
        <target name="File" xsi:type="File" fileName="${LogTxtLocation}" layout="${Layout}" 
                encoding="utf-8" maxArchiveFiles="30" archiveNumbering="Sequence" 
                archiveAboveSize="1048576" archiveFileName="${LogTxtDir}/${logger}.log{#######}" />
        <target name="FileFatal" xsi:type="File" fileName="${LogTxtLocationFatal}" layout="${LayoutFatal}"
                encoding="utf-8" maxArchiveFiles="30" archiveNumbering="Sequence"
                archiveAboveSize="1048576" archiveFileName="${LogTxtDir}/FatalFile.log{#######}" />
    </targets>

    <!--[設定] 紀錄規則-->
    <rules>
        <logger name="*" levels="Trace,Debug,Info,Warn" writeTo="File" />
        <logger name="*" levels="Error,Fatal" writeTo="FileFatal" />
    </rules>

</nlog>

3.測試

using NLog;

namespace nLogTest.Controllers
{    
    public class HomeController : Controller
    {
//宣告NLog物件
        private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public ActionResult Index()
        {
            logger.Info("My Test Log~~");
            return View();
        }