Nlog

 

 

using NLog;
using NLog.Config;
using NLog.Targets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingTools
{
    public class NlogConfig
    {
        public static void Config()
        {
            // Step 1. Create configuration object 
            var config = new LoggingConfiguration();


            var fileTarget = new FileTarget();
            config.AddTarget("file", fileTarget);
            fileTarget.FileName = "${basedir}/file.txt";
            fileTarget.Layout = "${message}";

            var fileTarget1 = new FileTarget();
            config.AddTarget("file", fileTarget1);
            fileTarget1.FileName = "${basedir}/Logs/${shortdate}/${shortdate}.txt";
            fileTarget1.Layout = "${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${callsite}:${callsite-linenumber}|${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}";
            fileTarget1.ArchiveFileName = "${basedir}/archives/${shortdate}-{#####}.txt";
            fileTarget1.ArchiveAboveSize = 102400;
            fileTarget1.ArchiveNumbering = ArchiveNumberingMode.Sequence;


            var rule1 = new LoggingRule(NlogLogger.Common_File.ToString(), LogLevel.Debug, fileTarget);
            config.LoggingRules.Add(rule1);

            var rule2 = new LoggingRule(NlogLogger.Log.ToString(), LogLevel.Debug, fileTarget1);
            config.LoggingRules.Add(rule2);

            LogManager.Configuration = config;


            NLog.LogManager.GetLogger(NlogLogger.Log.ToString()).Debug("111");
        }
        enum NlogLogger
        {
            Common_File,
            Log

        }
    }

}

 

上一篇:NLog在.Net Core控制台应用程序上的使用


下一篇:NLog整合Exceptionless