猿问

编程式 NLog 配置不起作用

我正在使用 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");

        }

        }

    }

}


千万里不及你
浏览 197回答 1
1回答

阿波罗的战车

您的问题出在您的 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);
随时随地看视频慕课网APP
我要回答