Nlog配置會話和文件同時寫入

class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();  //Logger對象代表與當前類相關聯的日誌消息的來源    
        static void Main(string[] args)
        {
            logger.Trace("輸出一條記錄信息成功!");//最常見的記錄信息,一般用於普通輸出   
            logger.Debug("輸出一條Debug信息成功!"); //同樣是記錄信息,不過出現的頻率要比Trace少一些,一般用來調試程序   
            logger.Info("輸出一條消息類型信息成功!");//信息類型的消息   
            logger.Warn("輸出一條警告信息成功");//警告信息,一般用於比較重要的場合   
            logger.Error("輸出一條錯誤信息成功!");//錯誤信息  
            logger.Fatal("輸出一條致命信息成功!");//致命異常信息。一般來講,發生致命異常之後程序將無法繼續執行。  
            Console.WriteLine("11111111");
        }  
    }





配置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"
      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}" ></target>
    <target xsi:type="Console" name="c"  layout="${longdate} ${uppercase:${level}} ${message}" ></target>
  </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="*"  writeTo="f" />
    <logger name="*"  writeTo="c" />
  </rules>
</nlog>


按住CTRL+F5


如果不成功,控制檯無法顯示,請嘗試把Nlog.CONFIG,拷貝到運行,執行文件下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章