簡體   English   中英

程序化NLog配置不起作用

[英]Programmatic NLog configuration isn't working

我正在使用Nlog設置測試應用,並且我希望配置可以通過代碼進行編輯。 我已經正確設置了配置,但是實際上看不到任何日志記錄。 我不確定這是因為我編碼不正確還是實現不正確。 我沒有看到正在創建的文件,也沒有看到彩色的控制台。 感謝您提供任何幫助,因為關於程序配置的文檔不足。

using System;
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
using NLog.Config;

namespace NLogArchitecture
{
    class ApplicationFramework
    {
        public static Logger log = LogManager.GetCurrentClassLogger();
        public static int sum = 0;

        public static void Main()
        {

            SetupLoggingConfiguration();

            F1();
            F2();
            F3();
            F4();
        }

        public static void F1()
        {
            int[] array1 = new int[5] { 1, 2, 3, 4, 5 };

            for (int i = 0; i > array1.Length; i++)
            {
                sum += array1[i];
            }

            log.Trace("The sum of array1 is: " + sum);
        }

        public static void F2()
        {
            int sumException = 0;
            try
            {
                sumException = sum / 0;
            }

            catch(Exception ex)
            {
                log.Error("Invalid operation: " + ex);
            }
        }

        public static void F3()
        {
            sum = sum + 3;

            log.Debug("Consider a different syntax");
        }

        public static void F4()
        {
            if (sum > 12) log.Info("The sum has been calculated well");
            if (sum <= 10) log.Info("The sum has been calculated incorrectly");
        }

        public static void SetupLoggingConfiguration()
        {


            // Intialize Config Object
            LoggingConfiguration config = new LoggingConfiguration();

            // Initialize Console Target
            var consoleTarget = new ColoredConsoleTarget("Console Target")
            {
                Layout = @"${time} ${longdate} ${uppercase: ${level}} ${logger} ${message} ${exception: format=ToString}"
            };

            // Initialize the AsyncWrapper for the ConsoleTarget
            AsyncTargetWrapper consoleWrapper = new AsyncTargetWrapper();
            consoleWrapper.WrappedTarget = consoleTarget;
            consoleWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block;
            consoleWrapper.QueueLimit = 5000;

            // Initialize the AsyncFlushTargetWrapper for the ConsoleWrapper
            AutoFlushTargetWrapper consoleFlushWrapper = new AutoFlushTargetWrapper();
            consoleFlushWrapper.WrappedTarget = consoleWrapper;

            // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule
            consoleFlushWrapper.Condition = "level >= LogLevel.Trace";

            // Adding the target to the config
            config.AddTarget("console", consoleFlushWrapper);


            // Initialize File Target
            var fileTarget = new FileTarget("File Target")
            {
                FileName = "@C:/logs/log.txt",
                KeepFileOpen = false,
                Layout = @"${time} ${longdate} ${uppercase: ${level}} ${logger} ${message} ${exception: format=ToString}"
            };

            // Initialize the AsyncWrapper for the fileTarget
            AsyncTargetWrapper fileWrapper = new AsyncTargetWrapper();
            fileWrapper.WrappedTarget = fileTarget;
            fileWrapper.QueueLimit = 5000;
            fileWrapper.OverflowAction = AsyncTargetWrapperOverflowAction.Block;

            // Initialize the AsyncFlushTargetWrapper for the FileWrapper
            AutoFlushTargetWrapper fileFlushWrapper = new AutoFlushTargetWrapper();
            fileFlushWrapper.WrappedTarget = fileWrapper;

            // This condition controls when the log is flushed. Set the LogLevel to be equivalent to the maximum level specified in the targetRule
            fileFlushWrapper.Condition = "level >= LogLevel.Trace";

            // Adding the target to the config
            config.AddTarget("file", fileFlushWrapper);

            // Creating the Log Level rules for each target and adding them to the config
            // Edit these to change what methods are logged
            var fileRule = new LoggingRule("FileRule", fileTarget);
            fileRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info);
            fileRule.EnableLoggingForLevel(LogLevel.Error);
            config.LoggingRules.Add(fileRule);

            var consoleRule = new LoggingRule("ConsoleRule", consoleTarget);
            consoleRule.EnableLoggingForLevels(LogLevel.Trace, LogLevel.Info);
            consoleRule.EnableLoggingForLevel(LogLevel.Error);
            config.LoggingRules.Add(consoleRule);

            // Assigning the configuration to the logger
            LogManager.Configuration = config;

        }
    }
}

您的問題出在您的LoggingRule模式中。 NLog不知道“ ConsoleRule”或“ FileRule”是什么。 由於您使用的是默認記錄,因此您的記錄器名稱中沒有匹配的模式。

var consoleRule = new LoggingRule("ConsoleRule", consoleTarget);
var fileRule = new LoggingRule("FileRule", fileTarget);

將其更改為“ *”以匹配所有內容,或為記錄器命名以匹配規則。

var consoleRule = new LoggingRule("*", consoleTarget);
var fileRule = new LoggingRule("*", fileTarget);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM