locked
Nlog add / remove rules RRS feed

  • Question

  • User-588203678 posted

    Hi<br>
    I am using Nlog.how to add rule and remove rule dynamically in c# not in nlog.config file directly.
    Wednesday, October 23, 2019 1:24 PM

All replies

  • User-719153870 posted

    Hi guhananth1,

    i want to add or remove rule dynamically from c# code

    As for how to add target and rule logger dynamically, please refer to Add / remove logfiles during runtime in NLog.

    Also, please check below demo built based on above thread which tested working fine:

    static void Main(string[] args)
            {
                try
                {
                    int zero = 0;
                    int result = 5 / zero;
                }
                catch (DivideByZeroException ex)
                {
                    //initialize the logger
                    Logger logger = NLog.LogManager.GetCurrentClassLogger();
                    //initialize the configuration
                    LoggingConfiguration config = LogManager.Configuration;
                    //initialize the target
                    var logFile = new FileTarget();
                    config.AddTarget("file", logFile);
    
                    logFile.FileName = @"C:\logs\example.log";//target log file
                    logFile.Layout = "${date} | ${message}";//log layout
                    //initialize the rule
                    var rule = new LoggingRule("*", LogLevel.Info, logFile);
                    config.LoggingRules.Add(rule);
                    //if you want to remove above rule, just use the Remove() method
                    //config.LoggingRules.Remove(rule); 
                    LogManager.Configuration = config;
                    //fire the logger
                    logger.Info("File converted!");
                }
            }

    As i mentioned above comment, if you want ti remove a rule you created dynamically, just use the config.LoggingRules.Remove() method.

    However, if you want to remove a rule already existed in your NLog.config file, it's seems impossile (I'm not sure, but i tested several methods and they didn't work as expected), this you can see at here.

    Hope it could help.

    Best Regard,

    Yang Shen

    Thursday, October 24, 2019 6:33 AM
  • User-588203678 posted
    Will it write in Nlog.config file?I want to write in it
    Thursday, October 24, 2019 6:37 AM
  • User-588203678 posted
    It shows error at logmanager.configuration

    Required parameter Filename on file target was not specified.
    Thursday, October 24, 2019 7:25 AM
  • User288213138 posted

    Hi guhananth1,

    I am using Nlog.how to add rule and remove rule dynamically in c# not in nlog.config file directly.

    You can use LoggingConfiguration.LoggingRules.Add/remove method to add/remove rules.

    I made demo for you as a reference

    // Step 1. Create configuration object
    
                LoggingConfiguration config = new LoggingConfiguration();
    
                // Step 2. Create targets and add them to the configuration
    
                FileTarget fileTarget = new FileTarget();
                config.AddTarget("file", fileTarget);
    
                // Step 3. Set target properties
    
                fileTarget.FileName = "${basedir}/file.txt";
                fileTarget.Layout = "${message}";
    
                // Step 4. Define rules
    
                LoggingRule rule1 = new LoggingRule("*", LogLevel.Info, fileTarget);
                config.LoggingRules.Add(rule1);
                //config.LoggingRules.Remove(rule);
    
                // Step 5. Activate the configuration
    
                LogManager.Configuration = config;
    
                Logger logger = LogManager.GetLogger("Example");
                logger.Info("add rule");

    Best regards,

    Sam

    Thursday, October 24, 2019 8:13 AM
  • User-719153870 posted

    Hi guhananth1,

    Will it write in Nlog.config file?I want to write in it

    Seems you trying to update your NLog.config, a config file is actually an xml file, so what you really want is to read and add tags to an xml file with c#.

    In this case, please refer to: Adding elements to an xml file in C#.

    You can also refer to below demo, in this demo, we will add <target> and <logger> tags to the right place:

    static void Main(string[] args)
            {
                XDocument doc = XDocument.Load("../../NLog.config");
    
                var ns = doc.Root.GetDefaultNamespace();
    
                XElement root = new XElement("logger");
                root.Add(new XAttribute("level", "Error"));
                root.Add(new XAttribute("name", "fileLogger"));
                root.Add(new XAttribute("writeTo", "fileTarget"));
                doc.Descendants(ns+"rules").FirstOrDefault().Add(root);
                //doc.Root.Element("rules").Add(root);
    
                XElement root2 = new XElement("target");
                root2.Add(new XAttribute("name", "fileTarget"));
                //root2.Add(new XAttribute("xsi:type", "File"));
                root2.Add(new XAttribute("fileName", @"C:\logs\example.log"));
                root2.Add(new XAttribute("layout", "${date} | ${message}"));
                doc.Descendants(ns + "targets").FirstOrDefault().Add(root2);
                //doc.Root.Element("targets").Add(root2);
    
                doc.Save("../../NLog.config");
            }

    However, i won't suggest you to change your server file( especially the config file), this could lead to unforeseen errors.

    Best Regard,

    Yang Shen

    Thursday, October 24, 2019 9:28 AM
  • User-588203678 posted
    I want to write to text file with following 1.exception type,2.exception message 3.stack trace 4. Inner exception

    Thursday, October 24, 2019 12:42 PM
  • User-588203678 posted
    Want to read nlog.config to see target but while writing log file log.txt write like
    A. Inner exception:
    B. Stack trace
    C. Source
    Thursday, October 24, 2019 12:50 PM