Jrod111498
Jrod111498

Reputation: 11

How do I use my custom logging configuration?

using System;
using System.IO;
using NLog;
using NLog.Config;
using NLog.Targets;
using Newtonsoft.Json.Linq;

namespace LoggingExample
{
    class Logging
    {

        static void Logging()
        {

                var config = new LoggingConfiguration();

                var consoleTarget = new ColoredConsoleTarget();
                config.AddTarget("console", consoleTarget);
                var fileTarget = new FileTarget();
                config.AddTarget("file", fileTarget);

                consoleTarget.Layout = "${message}";

                fileTarget.FileName = "C:/log.txt";
                fileTarget.Layout = "${message}";


                consoleTarget.UseDefaultRowHighlightingRules = true;

                string json = File.ReadAllText("config.json");
                JObject jo = JObject.Parse(json);
                string debugLevel = (string) jo.SelectToken("Logging_Properties.LogLevel");

                LogLevel level = TestDebugLevel(debugLevel) ? LogLevel.FromString(debugLevel) : LogLevel.Info;

                LoggingRule rule1 = new LoggingRule("Console", LogLevel.Debug, consoleTarget);
                LoggingRule rule2 = new LoggingRule("File", level, fileTarget);

                config.LoggingRules.Add(rule1);
                config.LoggingRules.Add(rule2);

                LogManager.Configuration = config;



        }

        static bool TestDebugLevel(string level)
        {
            switch (level.ToLower())
            {
                case "info":
                    return true;
                case "debug":
                    return true;
                case "warn":
                    return true;
                case "error":
                    return true;
                case "fatal":
                    return true;
                default:
                    return false;

            }
        }
    }

My application must contain a programatically configured logger, the only issue is that I have no idea how to use it. I think everything is written correctly but I have no idea how to actually call the the logger within the application. I know I have to use Logmanager.GetLogger() with the logger I want to use, either the console or file logger but when I try to nothing gets logged.

Upvotes: 0

Views: 662

Answers (2)

Julian
Julian

Reputation: 36720

You aren't logging anything, but only configuring NLog.

You need to log to the logger, in this case, after calling Log() (this could be in another class)

var appConsoleLogger = LogManager.GetLogger("ConsoleLogger");
appConsoleLogger.Info("info log to console");

var appFileLogger = LogManager.GetLogger("FileLogger");
appFileLogger.Info("info log to file");

PS: The name of your method is a bit confusing, I think it should "SetupLogging" instead of "Log"

Upvotes: 1

Michael
Michael

Reputation: 2123

I think you're missing one LogManager.ReconfigExistingLoggers()

Upvotes: 0

Related Questions