1. 程式人生 > 其它 >NET LOG日誌的使用以及設定檔案大小和數量限制------關鍵子caced和date

NET LOG日誌的使用以及設定檔案大小和數量限制------關鍵子caced和date

NET LOG日誌的使用以及設定檔案大小和數量限制------關鍵子caced和date

1、新建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" throwExceptions="false"
      internalLogLevel="Warn" internalLogFile="
${basedir}/logs/NlogRecords.log"> <!--Nlog內部日誌記錄為Off關閉。除非糾錯,不可以設為Trace否則速度很慢,起碼Debug以上--> <targets> <!--輸出檔案 按天歸檔,最多存檔檔案7個,單個檔案不超過50M,--> <!--輸出檔案 按天歸檔,最多存檔檔案7個,單個檔案不超過50M, 日誌檔案的大小個數與檔名稱有非常密切的關係,archiveFileName日誌檔名稱是常量的時候,設定archiveAbovesize會根據archiveAbovesize設定的大小進行生成資訊的日誌檔案, archiveFileName日誌檔名稱是時間格式的時候,設定archiveAbovesize會根據archiveAbovesize設定的大小以及日誌檔案的時間格式來進行生成資訊的日誌檔案,誰先到以水為準, 例如同一時間內日誌檔案大小達到archiveAbovesize日式檔案設定的大小了,則以為archiveAbovesize為準生成新的檔案 ,如果日誌檔案大小沒有達到,時間變化了,則以時間為準生成新的日誌檔案,archiveAbovesize
="10240",表示日誌檔案大小設定10kb,即archiveAbovesize="10240" ${date:format=yyyy-MM-dd HH_mm_ss} archiveFileName="${basedir}/logs/${date:format=yyyy-MM-dd HH_mm_ss}.txt" ${cached:${date:format=yyyy-MM-dd HH_mm_ss}} archiveFileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.txt"--> <target name="
logFile" xsi:type="File" concurrentWrites="true" fileName="${basedir}/logs/logfile.txt" archiveFileName="${basedir}/logs/log.{#}.txt" archiveNumbering="Date" maxArchiveFiles="7" archiveEvery="Day" archiveDateFormat="yyyyMMdd" archiveAbovesize="10240" layout="${longdate} ${uppercase:${level}} ${message}" /> <!--日誌名稱時間變數 關鍵字cached ${cached:${date:format=yyyy-MM-dd HH_mm_ss}} archiveFileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.txt"--> <target name="logFileSize0" xsi:type="File" concurrentWrites="true" fileName="${basedir}/logs/${cached:${date:format=yyyy-MM-dd HH_mm_ss}}.txt" maxArchiveFiles="7" archiveAbovesize="1024" layout="${longdate} ${uppercase:${level}} ${message}" /> <!--日誌名稱時間變數 ${date:format=yyyy-MM-dd HH_mm_ss} archiveFileName="${basedir}/logs/${date:format=yyyy-MM-dd HH_mm_ss}.txt"--> <target name="logFileSize1" xsi:type="File" concurrentWrites="true" fileName="${basedir}/logs/${date:format=yyyy-MM-dd HH_mm_ss}.txt" maxArchiveFiles="7" archiveAbovesize="1024" layout="${longdate} ${uppercase:${level}} ${message}" /> <!--日誌名稱固定常量 archiveFileName="log.txt"--> <target name="logFileSize2" xsi:type="File" concurrentWrites="true" fileName="${basedir}/logs/log.txt" maxArchiveFiles="7" archiveAbovesize="1024" layout="${longdate} ${uppercase:${level}} ${message}" /> <!--日誌名稱時間變數 ${date:format=yyyy-MM-dd HH_mm} archiveFileName="${basedir}/logs/${date:format=yyyy-MM-dd HH_mm}.txt"--> <target name="logFileSize" xsi:type="File" concurrentWrites="true" fileName="${basedir}/logs/${date:format=yyyy-MM-dd HH_mm}.txt" maxArchiveFiles="7" archiveAbovesize="10240" layout="${longdate} ${uppercase:${level}} ${message}" /> <!--輸出彩色控制檯 xsi:type="Console"是指定輸出到普通控制檯--> <target name="logConsole" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="false" layout="${pad:padding=-5:fixedLength=True:inner=${level:uppercase=true}} : ${longdate} | ${logger} | ${message} ${onexception:inner=${newline}${exception:format=tostring}}"> <highlight-row condition="level == LogLevel.Trace" foregroundColor="DarkGray" /> <highlight-word text="TRACE" foregroundColor="White" backgroundColor="DarkGray" ignoreCase="false" wholeWords="true"/> <highlight-word text="DEBUG" foregroundColor="White" backgroundColor="DarkGreen" ignoreCase="false" wholeWords="true"/> <highlight-word text="INFO " foregroundColor="White" backgroundColor="DarkCyan" ignoreCase="false" wholeWords="false"/> <highlight-word text="WARN " foregroundColor="DarkGray" backgroundColor="Yellow" ignoreCase="false" wholeWords="false"/> <highlight-word text="ERROR" foregroundColor="White" backgroundColor="Red" ignoreCase="false" wholeWords="true"/> <highlight-word text="FATAL" foregroundColor="White" backgroundColor="Magenta" ignoreCase="false" wholeWords="true"/> </target> </targets> <rules> <!--注意這裡的name="*",如果這裡的name=xx了那麼在C#的後臺.cs檔案使用日誌的時候必須把xx當做引數來傳遞,否則沒有日誌輸出--> <!--注意這裡的name="*",如果這裡的name=xx了那麼在C#的後臺.cs檔案使用日誌的時候必須把xx當做引數來傳遞,否則沒有日誌輸出--> <!--minlevel all>fatal>error>warn>debug>info>trace>off />--> <!--minlevel="Info" 表示只有Info以上的fatal,error才會輸出日誌,warn debug info trace不會輸出日誌 all 所有等級都會輸出日誌 off 所有等級都不輸出日誌/>--> <logger name="*" minlevel="Info" writeTo="logConsole" /> <logger name="*" minlevel="Info" writeTo="logFile" /> <!--<logger name="*" minlevel="Info" writeTo="logFileSize" />--> </rules> </nlog>

2、設定nlog.config屬性,,複製到輸出目錄---始終複製,生成操作---內容

3、程式碼使用

using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Utils;

namespace ConsoleTest
{
    internal class Program
    {
        /// <summary>
        /// 日誌
        /// </summary>
        private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            try
            {
                TestEnum();

                #region MyRe測試gion
                //TestLinqOrderBy();
                //TestResolveArgs();
                //TestTestHID();
                //TestConvert();
                //TesttWebSocket();
                #endregion
                Console.ReadKey();
            }
            catch/*(Exception ex)*/
            {
                //_logger.Error($"IdWorkerManager,初始化報錯,錯誤資訊:{ex.InnerException}");
            }
        }

        public static void TestEnum()
        {
            _logger.Info($"測試列舉。。。。");
            string comment = "77777";
            var enumIsDefined = Enum.IsDefined(typeof(EvMSEvaluateBmyyy), "服務條件差");
            if (enumIsDefined)
            {
                var enumValue = (EvMSEvaluateBmyyy)Enum.Parse(typeof(EvMSEvaluateBmyyy), "服務條件差");
                comment = comment + "|" + enumValue.GetHashCode() + "|" + "服務條件差";
                //獲取列舉名稱字串
                var ss= EvMSEvaluateBmyyy.交易核稅慢.ToString();
            }
            while (true)
            {
                _logger.Info($"測試列舉。。。。");
            }

            //int a = 1;
            //var senumName = Enum.GetName(typeof(EvMSEvaluateBmyyy), a);
            //Console.WriteLine($"列舉--根據值獲取名稱:值:{a}------名稱:{senumName}");
            //var enumValue = Enum.GetValues(typeof(EvMSEvaluateBmyyy));
            //var enumValue1 = Enum.GetNames(typeof(EvMSEvaluateBmyyy));
            //var enumValue2 = Enum.IsDefined(typeof(EvMSEvaluateBmyyy),2);
            //var enumValue3 = Enum.IsDefined(typeof(EvMSEvaluateBmyyy), "服務條件差");
            //if (enumValue3)
            //{
            //   var dddd= Enum.Parse(typeof(EvMSEvaluateBmyyy), "服務條件差");
            //    var dddd2 =(EvMSEvaluateBmyyy)(dddd);
            //    var dddd3 = dddd2.GetHashCode();
            //    Console.WriteLine();
            //}
            var enumValue4 = Enum.IsDefined(typeof(EvMSEvaluateBmyyy), EvMSEvaluateBmyyy.其他);
        }
   }
}