FindTargetByName() public method

Finds the target with the specified name.
public FindTargetByName ( string name ) : Target
name string /// The name of the target to be found. ///
return NLog.Targets.Target
        public LoggingConfiguration(IProgram program, IOperatingSystem os, IVSServices vsservice)
        {
            NLog.Config.LoggingConfiguration conf;
            string assemblyFolder = program.ExecutingAssemblyDirectory;

            try
            {
                conf = new XmlLoggingConfiguration(Path.Combine(assemblyFolder, "NLog.config"), true);
            }
            catch (Exception ex)
            {
                vsservice.ActivityLogError(string.Format(CultureInfo.InvariantCulture, "Error loading nlog.config. {0}", ex));
                conf = new NLog.Config.LoggingConfiguration();
            }

            var fileTarget = conf.FindTargetByName("file") as FileTarget;

            if (fileTarget == null)
            {
                fileTarget = new FileTarget();
                conf.AddTarget(Path.GetRandomFileName(), fileTarget);
                conf.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, fileTarget));
            }
            fileTarget.FileName = Path.Combine(os.Environment.GetLocalGitHubApplicationDataPath(), "extension.log");
            fileTarget.Layout   = layout;

            try
            {
                LogManager.Configuration = conf;
            }
            catch (Exception ex)
            {
                vsservice.ActivityLogError(string.Format(CultureInfo.InvariantCulture, "Error configuring the log. {0}", ex));
            }
        }
        public void LogglyTargetContextPropertyTest()
        {
            NLog.LogFactory logFactory = new NLog.LogFactory();
            NLog.Config.LoggingConfiguration logConfig = CreateConfigurationFromString(
                @"<nlog throwExceptions='true'>
                    <extensions>
	                    <add assembly='NLog.Targets.Loggly' />
                    </extensions>
                    <targets>
                        <target name='Loggly' type='Loggly' layout='${message}' taskDelayMilliseconds='10'>
                            <contextproperty name='hello' layout='${logger}' />
                        </target>
                    </targets>
	                <rules>
	                    <logger name='*' minlevel='Info' writeTo='Loggly' />
                    </rules>
                  </nlog>", logFactory);
            var logglyTarget     = logConfig.FindTargetByName("Loggly") as NLog.Targets.LogglyTarget;
            var logglyClientMock = new LogglyClientMock();

            logglyTarget.ClientFactory = () => logglyClientMock;
            logFactory.Configuration   = logConfig;
            NLog.Logger logger = logFactory.GetLogger(MethodInfo.GetCurrentMethod().Name);
            logger.Info("Hello World");
            logglyClientMock.LogWritten.WaitOne(1000);
            Assert.AreEqual(1, logglyClientMock.LogglyEvents.Count);
            Assert.Contains("hello", logglyClientMock.LogglyEvents[0].Data.KeyList);
            Assert.AreEqual(MethodInfo.GetCurrentMethod().Name, logglyClientMock.LogglyEvents[0].Data["hello"]);
        }
        public void LogglyTargetTagsTest()
        {
            NLog.LogFactory logFactory = new NLog.LogFactory();
            NLog.Config.LoggingConfiguration logConfig = CreateConfigurationFromString(
                @"<nlog throwExceptions='true'>
                    <extensions>
	                    <add assembly='NLog.Targets.Loggly' />
                    </extensions>
                    <targets>
                        <target name='Loggly' type='Loggly' layout='${message}'>
                            <tag name='hello' />
                            <tag name='${logger}' />
                        </target>
                    </targets>
	                <rules>
	                    <logger name='*' minlevel='Info' writeTo='Loggly' />
                    </rules>
                  </nlog>", logFactory);
            var logglyTarget     = logConfig.FindTargetByName("Loggly") as NLog.Targets.LogglyTarget;
            var logglyClientMock = new LogglyClientMock();

            logglyTarget.ClientFactory = () => logglyClientMock;
            logFactory.Configuration   = logConfig;
            NLog.Logger logger = logFactory.GetLogger(MethodInfo.GetCurrentMethod().Name);
            logger.Info("Hello World");
            Assert.AreEqual(1, logglyClientMock.LogglyEvents.Count);
            Assert.AreEqual(2, logglyClientMock.LogglyEvents[0].Options.Tags.Count);
            Assert.AreEqual("hello", logglyClientMock.LogglyEvents[0].Options.Tags[0].Value);
            Assert.AreEqual(MethodInfo.GetCurrentMethod().Name, logglyClientMock.LogglyEvents[0].Options.Tags[1].Value);
        }
        public void LogglyTargetMdlcTest()
        {
            NLog.LogFactory logFactory = new NLog.LogFactory();
            NLog.Config.LoggingConfiguration logConfig = CreateConfigurationFromString(
                @"<nlog throwExceptions='true'>
                    <extensions>
	                    <add assembly='NLog.Targets.Loggly' />
                    </extensions>
                    <targets>
                        <target name='Loggly' type='Loggly' layout='${message}' includeMdlc='true'>
                        </target>
                    </targets>
	                <rules>
	                    <logger name='*' minlevel='Info' writeTo='Loggly' />
                    </rules>
                  </nlog>", logFactory);
            var logglyTarget     = logConfig.FindTargetByName("Loggly") as NLog.Targets.LogglyTarget;
            var logglyClientMock = new LogglyClientMock();

            logglyTarget.ClientFactory = () => logglyClientMock;
            logFactory.Configuration   = logConfig;
            NLog.Logger logger = logFactory.GetLogger(MethodInfo.GetCurrentMethod().Name);
            using (NLog.MappedDiagnosticsLogicalContext.SetScoped("hello", logger.Name))
            {
                logger.Info("Hello World");
                Assert.AreEqual(1, logglyClientMock.LogglyEvents.Count);
                Assert.Contains("hello", logglyClientMock.LogglyEvents[0].Data.KeyList);
                Assert.AreEqual(MethodInfo.GetCurrentMethod().Name, logglyClientMock.LogglyEvents[0].Data["hello"]);
            }
        }
        public LoggingConfiguration(IProgram program, IOperatingSystem os, IVSServices vsservice)
        {
            NLog.Config.LoggingConfiguration conf;
            string assemblyFolder = program.ExecutingAssemblyDirectory;
            try
            {
                conf = new XmlLoggingConfiguration(Path.Combine(assemblyFolder, "NLog.config"), true);
            }
            catch (Exception ex)
            {
                vsservice.ActivityLogError(string.Format(CultureInfo.InvariantCulture, "Error loading nlog.config. {0}", ex));
                conf = new NLog.Config.LoggingConfiguration();
            }

            var fileTarget = conf.FindTargetByName("file") as FileTarget;
            if (fileTarget == null)
            {
                fileTarget = new FileTarget();
                conf.AddTarget(Path.GetRandomFileName(), fileTarget);
                conf.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, fileTarget));
            }
            fileTarget.FileName = Path.Combine(os.Environment.GetLocalGitHubApplicationDataPath(), "extension.log");
            fileTarget.Layout = layout;

            try
            {
                LogManager.Configuration = conf;
            }
            catch (Exception ex)
            {
                vsservice.ActivityLogError(string.Format(CultureInfo.InvariantCulture, "Error configuring the log. {0}", ex));
            }
        }
Ejemplo n.º 6
0
        public void AddTarget_testname_param()
        {
            var config = new LoggingConfiguration();
            config.AddTarget("name1", new FileTarget {Name = "name2"});
            var allTargets = config.AllTargets;
            Assert.NotNull(allTargets);
            Assert.Equal(1, allTargets.Count);

            //maybe confusing, but the name of the target is not changed, only the one of the key.
            Assert.Equal("name2", allTargets.First().Name);
            Assert.NotNull(config.FindTargetByName<FileTarget>("name1"));
        }
Ejemplo n.º 7
0
 public void AddTarget_testname_fromtarget()
 {
     var config = new LoggingConfiguration();
     config.AddTarget(new FileTarget {Name = "name2"});
     var allTargets = config.AllTargets;
     Assert.NotNull(allTargets);
     Assert.Equal(1, allTargets.Count);
     Assert.Equal("name2", allTargets.First().Name);
     Assert.NotNull(config.FindTargetByName<FileTarget>("name2"));
 }
Ejemplo n.º 8
0
 public void AddRule_with_target()
 {
     var config = new LoggingConfiguration();
     var fileTarget = new FileTarget {Name = "File"};
     config.AddRuleForOneLevel(LogLevel.Error, fileTarget, "*a");
     Assert.NotNull(config.LoggingRules);
     Assert.Equal(1, config.LoggingRules.Count);
     config.AddTarget(new FileTarget {Name = "File"});
     var allTargets = config.AllTargets;
     Assert.NotNull(allTargets);
     Assert.Equal(1, allTargets.Count);
     Assert.Equal("File", allTargets.First().Name);
     Assert.NotNull(config.FindTargetByName<FileTarget>("File"));
 }
Ejemplo n.º 9
0
        private void ConfigureRulesFromElement(LoggingConfiguration config, LoggingRuleCollection rules, XmlElement element)
        {
            if (element == null)
            {
                return;
            }

            foreach (XmlElement el in PropertyHelper.GetChildElements(element, "logger"))
            {
                XmlElement ruleElement = el;

                LoggingRule rule        = new LoggingRule();
                string      namePattern = GetCaseInsensitiveAttribute(ruleElement, "name");
                if (namePattern == null)
                {
                    namePattern = "*";
                }

                string appendTo = GetCaseInsensitiveAttribute(ruleElement, "appendTo");
                if (appendTo == null)
                {
                    appendTo = GetCaseInsensitiveAttribute(ruleElement, "writeTo");
                }

                rule.LoggerNamePattern = namePattern;
                if (appendTo != null)
                {
                    foreach (string t in appendTo.Split(','))
                    {
                        string targetName = t.Trim();
                        Target target     = config.FindTargetByName(targetName);

                        if (target != null)
                        {
                            rule.Targets.Add(target);
                        }
                        else
                        {
                            throw new NLogConfigurationException("Target " + targetName + " not found.");
                        }
                    }
                }
                rule.Final = false;

                if (HasCaseInsensitiveAttribute(ruleElement, "final"))
                {
                    rule.Final = true;
                }

                if (HasCaseInsensitiveAttribute(ruleElement, "level"))
                {
                    LogLevel level = LogLevel.FromString(GetCaseInsensitiveAttribute(ruleElement, "level"));
                    rule.EnableLoggingForLevel(level);
                }
                else if (HasCaseInsensitiveAttribute(ruleElement, "levels"))
                {
                    string levelsString = GetCaseInsensitiveAttribute(ruleElement, "levels");
                    levelsString = CleanWhitespace(levelsString);

                    string[] tokens = levelsString.Split(',');
                    foreach (string s in tokens)
                    {
                        if (s != "")
                        {
                            LogLevel level = LogLevel.FromString(s);
                            rule.EnableLoggingForLevel(level);
                        }
                    }
                }
                else
                {
                    int minLevel = 0;
                    int maxLevel = LogLevel.MaxLevel.Ordinal;

                    if (HasCaseInsensitiveAttribute(ruleElement, "minlevel"))
                    {
                        minLevel = LogLevel.FromString(GetCaseInsensitiveAttribute(ruleElement, "minlevel")).Ordinal;
                    }

                    if (HasCaseInsensitiveAttribute(ruleElement, "maxlevel"))
                    {
                        maxLevel = LogLevel.FromString(GetCaseInsensitiveAttribute(ruleElement, "maxlevel")).Ordinal;
                    }

                    for (int i = minLevel; i <= maxLevel; ++i)
                    {
                        rule.EnableLoggingForLevel(LogLevel.FromOrdinal(i));
                    }
                }

                foreach (XmlElement el2 in PropertyHelper.GetChildElements(ruleElement, "filters"))
                {
                    ConfigureRuleFiltersFromXmlElement(rule, el2);
                }

                ConfigureRulesFromElement(config, rule.ChildRules, ruleElement);

                rules.Add(rule);
            }
        }
Ejemplo n.º 10
0
 protected NLog.Targets.DebugTarget GetDebugTarget(string targetName, LoggingConfiguration configuration)
 {
     var debugTarget = (NLog.Targets.DebugTarget)configuration.FindTargetByName(targetName);
     Assert.NotNull(debugTarget);
     return debugTarget;
 }