public void DataTypesTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <extensions> <add type='" + typeof(MyTarget).AssemblyQualifiedName + @"' /> </extensions> <targets> <target type='MyTarget' name='myTarget' byteProperty='42' int16Property='42' int32Property='42' int64Property='42000000000' stringProperty='foobar' boolProperty='true' doubleProperty='3.14159' floatProperty='3.14159' enumProperty='Value3' flagsEnumProperty='Value1,Value3' encodingProperty='utf-8' cultureProperty='en-US' typeProperty='System.Int32' layoutProperty='${level}' conditionProperty=""starts-with(message, 'x')"" uriProperty='http://nlog-project.org' lineEndingModeProperty='default' /> </targets> </nlog>"); var myTarget = c.FindTargetByName("myTarget") as MyTarget; Assert.NotNull(myTarget); Assert.Equal((byte)42, myTarget.ByteProperty); Assert.Equal((short)42, myTarget.Int16Property); Assert.Equal(42, myTarget.Int32Property); Assert.Equal(42000000000L, myTarget.Int64Property); Assert.Equal("foobar", myTarget.StringProperty); Assert.Equal(true, myTarget.BoolProperty); Assert.Equal(3.14159, myTarget.DoubleProperty); Assert.Equal(3.14159f, myTarget.FloatProperty); Assert.Equal(MyEnum.Value3, myTarget.EnumProperty); Assert.Equal(MyFlagsEnum.Value1 | MyFlagsEnum.Value3, myTarget.FlagsEnumProperty); Assert.Equal(Encoding.UTF8, myTarget.EncodingProperty); Assert.Equal("en-US", myTarget.CultureProperty.Name); Assert.Equal(typeof(int), myTarget.TypeProperty); Assert.Equal("'${level}'", myTarget.LayoutProperty.ToString()); Assert.Equal("starts-with(message, 'x')", myTarget.ConditionProperty.ToString()); Assert.Equal(new Uri("http://nlog-project.org"), myTarget.UriProperty); Assert.Equal(LineEndingMode.Default, myTarget.LineEndingModeProperty); }
public void TargetAliasShouldWork(string typeName) { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString($@" <nlog> <targets> <target name='d' type='{typeName}' /> </targets> </nlog>"); var t = c.FindTargetByName <TraceTarget>("d"); Assert.NotNull(t); }
/// <summary> /// Device Logger 초기화. app.config 에 AsyncDevice 로거를 복사해서 사용 /// </summary> protected virtual void InitDeviceLogger(string loggerName) { IMapper mapper = GetMapper(); AsyncTargetWrapper newTargetWrapper = null; FileTarget newTarget = null; LoggingConfiguration logConfig = LogManager.Configuration; if (logConfig.FindTargetByName("AsyncDevice") is AsyncTargetWrapper asyncDeviceWrapper) { newTargetWrapper = mapper.Map <AsyncTargetWrapper>(asyncDeviceWrapper); newTarget = mapper.Map <FileTarget>(newTargetWrapper.WrappedTarget); } else { logger.Error($"app.config 로그설정에 AsyncDevice 타겟이 없습니다"); throw new ArgumentNullException(nameof(asyncDeviceWrapper)); } newTargetWrapper.Name = loggerName; newTargetWrapper.WrappedTarget = newTarget; newTarget.Name = $"{loggerName}_Sub"; newTarget.FileName = newTarget.FileName.ToString().Replace("{loggername}", loggerName).Replace("'", string.Empty); newTarget.ArchiveFileName = newTarget.ArchiveFileName.ToString().Replace("{loggername}", loggerName).Replace("{controllerid}", Controller.Name).Replace("'", string.Empty); logConfig.AddTarget(newTarget.Name, newTarget); logConfig.AddTarget(newTargetWrapper.Name, newTargetWrapper); var rule = logConfig.LoggingRules.FirstOrDefault(x => x.LoggerNamePattern == "Device.*"); if (rule != null) { var newRule = new LoggingRule(loggerName, rule.Levels.First(), rule.Levels.Last(), newTargetWrapper) { Final = true }; foreach (Target target in rule.Targets) { newRule.Targets.Add(target); } // Final 적용을 위해 0번째에 추가 logConfig.LoggingRules.Insert(0, newRule); } // 주의! 이렇게 해야 변경사항이 적용됨 LogManager.Configuration = logConfig; }
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")); }
public void TypedLayoutTargetTest() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <extensions> <add type='" + typeof(MyTypedLayoutTarget).AssemblyQualifiedName + @"' /> </extensions> <targets> <target type='MyTypedLayoutTarget' name='myTarget' byteProperty='42' int16Property='43' int32Property='44' int64Property='45000000000' stringProperty='foobar' boolProperty='true' doubleProperty='3.14159' floatProperty='3.24159' enumProperty='Value3' flagsEnumProperty='Value1,Value3' encodingProperty='utf-8' cultureProperty='en-US' typeProperty='System.Int32' uriProperty='https://nlog-project.org' lineEndingModeProperty='default' /> </targets> </nlog>"); var nullEvent = LogEventInfo.CreateNullEvent(); var myTarget = c.FindTargetByName("myTarget") as MyTypedLayoutTarget; Assert.NotNull(myTarget); Assert.Equal((byte)42, myTarget.ByteProperty.StaticValue); Assert.Equal((short)43, myTarget.Int16Property.StaticValue); Assert.Equal(44, myTarget.Int32Property.StaticValue); Assert.Equal(45000000000L, myTarget.Int64Property.StaticValue); Assert.Equal("foobar", myTarget.StringProperty.StaticValue); Assert.True(myTarget.BoolProperty.StaticValue); Assert.Equal(3.14159, myTarget.DoubleProperty.StaticValue); Assert.Equal(3.24159f, myTarget.FloatProperty.StaticValue); Assert.Equal(TargetConfigurationTests.MyEnum.Value3, myTarget.EnumProperty.StaticValue); Assert.Equal(TargetConfigurationTests.MyFlagsEnum.Value1 | TargetConfigurationTests.MyFlagsEnum.Value3, myTarget.FlagsEnumProperty.StaticValue); Assert.Equal(Encoding.UTF8, myTarget.EncodingProperty.StaticValue); Assert.Equal("en-US", myTarget.CultureProperty.StaticValue.Name); Assert.Equal(typeof(int), myTarget.TypeProperty.StaticValue); Assert.Equal(new Uri("https://nlog-project.org"), myTarget.UriProperty.StaticValue); Assert.Equal(LineEndingMode.Default, myTarget.LineEndingModeProperty.StaticValue); }
public void MultipleTargetsTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target name='d1' type='Debug' /> <target name='d2' type='Debug' /> <target name='d3' type='Debug' /> <target name='d4' type='Debug' /> </targets> <rules> <logger name='*' level='Warn' writeTo='d1,d2,d3' /> </rules> </nlog>"); Assert.Equal(1, c.LoggingRules.Count); var rule = c.LoggingRules[0]; Assert.Equal(3, rule.Targets.Count); Assert.Same(c.FindTargetByName("d1"), rule.Targets[0]); Assert.Same(c.FindTargetByName("d2"), rule.Targets[1]); Assert.Same(c.FindTargetByName("d3"), rule.Targets[2]); }
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")); }
public void MaxMessageLengthShouldBe16384_WhenNotSpecifyAnyOption() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog ThrowExceptions='true'> <targets> <target type='EventLog' name='eventLog1' layout='${message}' /> </targets> <rules> <logger name='*' writeTo='eventLog1'> </logger> </rules> </nlog>"); var eventLog1 = c.FindTargetByName <EventLogTarget>("eventLog1"); Assert.Equal(MaxMessageLength, eventLog1.MaxMessageLength); }
public void MaxMessageLengthShouldBeAsSpecifiedOption() { const int expectedMaxMessageLength = 1000; LoggingConfiguration c = CreateConfigurationFromString(string.Format(@" <nlog ThrowExceptions='true'> <targets> <target type='EventLog' name='eventLog1' layout='${{message}}' maxmessagelength='{0}' /> </targets> <rules> <logger name='*' writeTo='eventLog1'> </logger> </rules> </nlog>", expectedMaxMessageLength)); var eventLog1 = c.FindTargetByName <EventLogTarget>("eventLog1"); Assert.Equal(expectedMaxMessageLength, eventLog1.MaxMessageLength); }
/// <summary> /// Creates the error message and displays it. /// </summary> /// <param name="title"></param> /// <param name="e"></param> /// <returns></returns> private static DialogResult ShowThreadExceptionDialog(string title, Exception e) { //string logFilename = LogManager.Configuration.AllTargets[0]. LoggingConfiguration config = LogManager.Configuration; FileTarget standardTarget = config.FindTargetByName("logfile") as FileTarget; string logFilename = string.Empty; if (standardTarget != null) { logFilename = SimpleLayout.Evaluate(standardTarget.FileName.ToString()); } string errorMsg = string.Format( "An application error occurred.\nDescription: {0}, {1}\n\nFull details are available in the log file: {2}\n\nPlease contact the author at {3}.", e.GetType().Name, e.Message, logFilename, "http://code.google.com/p/practicesharp/issues/list"); return(MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)); }
public CwiLogger(string logPath, string logName, string className) { lock (createLogLock) { var fileTarget = _logConfig.FindTargetByName(logName); if (fileTarget == null) { var fileTarget = new FileTarget(logName); fileTarget.FileName = Path.Combine(logPath, logName); fileTarget.Layout = "${longdate}|${level:uppercase=true}|${logger}|${message}"; _logConfig.AddTarget(fileTarget); var rule = new LoggingRule(className, LogLevel.Debug, fileTarget); _logConfig.LoggingRules.Add(rule); LogManager.Configuration = _logConfig; } } this._log = LogManager.GetLogger(className); }
public void ArrayElementParameterTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target type='MethodCall' name='mct'> <parameter> <name>p1</name> <layout>${message}</layout> </parameter> <parameter> <name>p2</name> <layout type='CsvLayout'> <column name='x' layout='${message}' /> <column name='y' layout='${level}' /> </layout> </parameter> <parameter> <name>p3</name> <layout>${logger}</layout> </parameter> </target> </targets> </nlog>"); var t = c.FindTargetByName("mct") as MethodCallTarget; Assert.IsNotNull(t); Assert.AreEqual(3, t.Parameters.Count); Assert.AreEqual("p1", t.Parameters[0].Name); Assert.AreEqual("'${message}'", t.Parameters[0].Layout.ToString()); Assert.AreEqual("p2", t.Parameters[1].Name); CsvLayout csvLayout = t.Parameters[1].Layout as CsvLayout; Assert.IsNotNull(csvLayout); Assert.AreEqual(2, csvLayout.Columns.Count); Assert.AreEqual("x", csvLayout.Columns[0].Name); Assert.AreEqual("y", csvLayout.Columns[1].Name); Assert.AreEqual("p3", t.Parameters[2].Name); Assert.AreEqual("'${logger}'", t.Parameters[2].Layout.ToString()); }
[InlineData(16384)] // Acceptable value public void ConfigurationMaxKilobytes_ShouldBeAsSpecified_WhenMaxKilobytesIsValid(long?maxKilobytes) { var expectedMaxKilobytes = maxKilobytes; string configrationText = $@" <nlog ThrowExceptions='true'> <targets> <target type='EventLog' name='eventLog1' layout='${{message}}' maxKilobytes='{maxKilobytes}' /> </targets> <rules> <logger name='*' writeTo='eventLog1' /> </rules> </nlog>"; LoggingConfiguration configuration = XmlLoggingConfiguration.CreateFromXmlString(configrationText); var eventLog1 = configuration.FindTargetByName <EventLogTarget>("eventLog1"); Assert.Equal(expectedMaxKilobytes, eventLog1.MaxKilobytes); }
public void SimpleTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target name='d' type='Debug' layout='${message}' /> </targets> </nlog>"); DebugTarget t = c.FindTargetByName("d") as DebugTarget; Assert.IsNotNull(t); Assert.AreEqual(t.Name, "d"); SimpleLayout l = t.Layout as SimpleLayout; Assert.AreEqual("${message}", l.Text); Assert.IsNotNull(t.Layout); Assert.AreEqual(1, l.Renderers.Count); Assert.IsInstanceOfType(typeof(MessageLayoutRenderer), l.Renderers[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")); }
public void SimpleTest() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <targets> <target name='d' type='Debug' layout='${message}' /> </targets> </nlog>"); DebugTarget t = c.FindTargetByName("d") as DebugTarget; Assert.NotNull(t); Assert.Equal("d", t.Name); SimpleLayout l = t.Layout as SimpleLayout; Assert.Equal("${message}", l.Text); Assert.NotNull(t.Layout); Assert.Single(l.Renderers); Assert.IsType <MessageLayoutRenderer>(l.Renderers[0]); }
public void DefaultTargetParametersOnWrappedTargetTest() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <targets> <default-target-parameters type='Debug' layout='x${message}x' /> <target type='BufferingWrapper' name='buf1'> <target type='Debug' name='d1' /> </target> </targets> </nlog>"); var wrap = c.FindTargetByName("buf1") as BufferingTargetWrapper; Assert.NotNull(wrap); Assert.NotNull(wrap.WrappedTarget); var t = wrap.WrappedTarget as DebugTarget; Assert.NotNull(t); Assert.Equal("'x${message}x'", t.Layout.ToString()); }
public void SimpleTest2() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target name='d' type='Debug' layout='${message} ${level}' /> </targets> </nlog>"); DebugTarget t = c.FindTargetByName("d") as DebugTarget; Assert.IsNotNull(t); Assert.AreEqual(t.Name, "d"); SimpleLayout l = t.Layout as SimpleLayout; Assert.AreEqual("${message} ${level}", l.Text); Assert.IsNotNull(l); Assert.AreEqual(3, l.Renderers.Count); Assert.IsInstanceOfType(l.Renderers[0], typeof(MessageLayoutRenderer)); Assert.IsInstanceOfType(l.Renderers[1], typeof(LiteralLayoutRenderer)); Assert.IsInstanceOfType(l.Renderers[2], typeof(LevelLayoutRenderer)); Assert.AreEqual(" ", ((LiteralLayoutRenderer)l.Renderers[1]).Text); }
public void SimpleElementSyntaxTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target type='Debug'> <name>d</name> <layout>${message}</layout> </target> </targets> </nlog>"); DebugTarget t = c.FindTargetByName("d") as DebugTarget; Assert.NotNull(t); Assert.Equal(t.Name, "d"); SimpleLayout l = t.Layout as SimpleLayout; Assert.Equal("${message}", l.Text); Assert.NotNull(t.Layout); Assert.Equal(1, l.Renderers.Count); Assert.IsType(typeof(MessageLayoutRenderer), l.Renderers[0]); }
public void SimpleTest2() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <targets> <target name='d' type='Debug' layout='${message} ${level}' /> </targets> </nlog>"); DebugTarget t = c.FindTargetByName("d") as DebugTarget; Assert.NotNull(t); Assert.Equal("d", t.Name); SimpleLayout l = t.Layout as SimpleLayout; Assert.Equal("${message} ${level}", l.Text); Assert.NotNull(l); Assert.Equal(3, l.Renderers.Count); Assert.IsType <MessageLayoutRenderer>(l.Renderers[0]); Assert.IsType <LiteralLayoutRenderer>(l.Renderers[1]); Assert.IsType <LevelLayoutRenderer>(l.Renderers[2]); Assert.Equal(" ", ((LiteralLayoutRenderer)l.Renderers[1]).Text); }
public void WarningForObsoleteUseTransactions(string property, bool printWarning) { LoggingConfiguration c = CreateConfigurationFromString(string.Format(@" <nlog ThrowExceptions='true'> <targets> <target type='database' {0} name='t1' commandtext='fake sql' connectionstring='somewhere' /> </targets> <rules> <logger name='*' writeTo='t1'> </logger> </rules> </nlog>", property)); StringWriter writer1 = new StringWriter() { NewLine = "\n" }; InternalLogger.LogWriter = writer1; var t = c.FindTargetByName <DatabaseTarget>("t1"); t.Initialize(null); var internalLog = writer1.ToString(); if (printWarning) { Assert.Contains("obsolete", internalLog, StringComparison.InvariantCultureIgnoreCase); Assert.Contains("usetransactions", internalLog, StringComparison.InvariantCultureIgnoreCase); } else { Assert.DoesNotContain("obsolete", internalLog, StringComparison.InvariantCultureIgnoreCase); Assert.DoesNotContain("usetransactions", internalLog, StringComparison.InvariantCultureIgnoreCase); } }
public void NestedXmlConfigElementTest() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <extensions> <add type='" + typeof(StructuredDebugTarget).AssemblyQualifiedName + @"' /> </extensions> <targets> <target type='StructuredDebugTarget'> <name>structuredTgt</name> <layout>${message}</layout> <config platform='any'> <parameter name='param1' /> </config> </target> </targets> </nlog>"); var t = c.FindTargetByName("structuredTgt") as StructuredDebugTarget; Assert.NotNull(t); Assert.Equal("any", t.Config.Platform); Assert.Equal("param1", t.Config.Parameter.Name); }
public void CompoundRefTest() { LoggingConfiguration c = CreateConfigurationFromString(@" <nlog> <targets> <target name='d1' type='Debug' layout='${message}1' /> <target name='d2' type='Debug' layout='${message}2' /> <target name='d3' type='Debug' layout='${message}3' /> <target name='d4' type='Debug' layout='${message}4' /> <compound-target name='rr' type='RoundRobinGroup'> <target-ref name='d1' /> <target-ref name='d2' /> <target-ref name='d3' /> <target-ref name='d4' /> </compound-target> </targets> </nlog>"); Assert.IsNotNull(c.FindTargetByName("rr")); Assert.IsNotNull(c.FindTargetByName("d1")); Assert.IsNotNull(c.FindTargetByName("d2")); Assert.IsNotNull(c.FindTargetByName("d3")); Assert.IsNotNull(c.FindTargetByName("d4")); Assert.IsInstanceOfType(typeof(RoundRobinGroupTarget), c.FindTargetByName("rr")); Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d1")); Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d2")); Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d3")); Assert.IsInstanceOfType(typeof(DebugTarget), c.FindTargetByName("d4")); RoundRobinGroupTarget rr = c.FindTargetByName("rr") as RoundRobinGroupTarget; DebugTarget d1 = c.FindTargetByName("d1") as DebugTarget; DebugTarget d2 = c.FindTargetByName("d2") as DebugTarget; DebugTarget d3 = c.FindTargetByName("d3") as DebugTarget; DebugTarget d4 = c.FindTargetByName("d4") as DebugTarget; Assert.AreEqual(4, rr.Targets.Count); Assert.AreSame(d1, rr.Targets[0]); Assert.AreSame(d2, rr.Targets[1]); Assert.AreSame(d3, rr.Targets[2]); Assert.AreSame(d4, rr.Targets[3]); Assert.AreEqual(((SimpleLayout)d1.Layout).Text, "${message}1"); Assert.AreEqual(((SimpleLayout)d2.Layout).Text, "${message}2"); Assert.AreEqual(((SimpleLayout)d3.Layout).Text, "${message}3"); Assert.AreEqual(((SimpleLayout)d4.Layout).Text, "${message}4"); }
public void CompoundTest() { LoggingConfiguration c = XmlLoggingConfiguration.CreateFromXmlString(@" <nlog> <targets> <compound-target name='rr' type='RoundRobinGroup'> <target name='d1' type='Debug' layout='${message}1' /> <target name='d2' type='Debug' layout='${message}2' /> <target name='d3' type='Debug' layout='${message}3' /> <target name='d4' type='Debug' layout='${message}4' /> </compound-target> </targets> </nlog>"); Assert.NotNull(c.FindTargetByName("rr")); Assert.NotNull(c.FindTargetByName("d1")); Assert.NotNull(c.FindTargetByName("d2")); Assert.NotNull(c.FindTargetByName("d3")); Assert.NotNull(c.FindTargetByName("d4")); Assert.IsType <RoundRobinGroupTarget>(c.FindTargetByName("rr")); Assert.IsType <DebugTarget>(c.FindTargetByName("d1")); Assert.IsType <DebugTarget>(c.FindTargetByName("d2")); Assert.IsType <DebugTarget>(c.FindTargetByName("d3")); Assert.IsType <DebugTarget>(c.FindTargetByName("d4")); RoundRobinGroupTarget rr = c.FindTargetByName("rr") as RoundRobinGroupTarget; DebugTarget d1 = c.FindTargetByName("d1") as DebugTarget; DebugTarget d2 = c.FindTargetByName("d2") as DebugTarget; DebugTarget d3 = c.FindTargetByName("d3") as DebugTarget; DebugTarget d4 = c.FindTargetByName("d4") as DebugTarget; Assert.Equal(4, rr.Targets.Count); Assert.Same(d1, rr.Targets[0]); Assert.Same(d2, rr.Targets[1]); Assert.Same(d3, rr.Targets[2]); Assert.Same(d4, rr.Targets[3]); Assert.Equal("${message}1", ((SimpleLayout)d1.Layout).Text); Assert.Equal("${message}2", ((SimpleLayout)d2.Layout).Text); Assert.Equal("${message}3", ((SimpleLayout)d3.Layout).Text); Assert.Equal("${message}4", ((SimpleLayout)d4.Layout).Text); }