Beispiel #1
0
        static CreekController()
        {
            MethodCallTarget target = new MethodCallTarget();
            // Log only the message from the same assembly of the class belongs to!!
            LoggingRule rule = new LoggingRule(
                typeof(CreekController).Assembly.GetName().Name + ".*",
                NLog.LogLevel.Error,
                target);

            // Initialize and add the MethodCallTarget to the NLog engine
            target.ClassName  = typeof(CreekController).AssemblyQualifiedName;
            target.MethodName = "LogError";
            target.Parameters.Add(new MethodCallParameter("level", typeof(string), "${level}"));
            target.Parameters.Add(new MethodCallParameter("message", typeof(string), "${message}"));
            if (File.Exists("NLog.config"))
            {
                // Read the settings from NLog.config and add the new target & rule
                XmlLoggingConfiguration oConfig = new XmlLoggingConfiguration("NLog.config");
                oConfig.AddTarget("MemoryTarget", target);
                oConfig.LoggingRules.Add(rule);
                NLog.LogManager.Configuration = oConfig;
            }
            else
            {
                throw new ApplicationException(AppResource.NLogConfigNotFound);
            }
        }
Beispiel #2
0
        private void ConfigureNLog()
        {
            // this is because RichTextBox target from config file opens new form
            // so it should be configured from the code
            var target = new RichTextBoxTarget()
            {
                Name        = "Rich",
                ControlName = "txtLog",
                FormName    = "MainForm",
                UseDefaultRowColoringRules = true,
                AutoScroll = true,
                Layout     = "${message}"
            };

            var xlc = new XmlLoggingConfiguration("NLog.config");

            xlc.AddTarget("Rich", target);
            xlc.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, target));
            LogManager.Configuration = xlc;
        }