From d0ce69f7fbc08b196d2ed05710169713a1637e78 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Tue, 19 Apr 2016 22:17:21 +0200 Subject: [PATCH 1/2] Fix allLayouts is null Fix allLayouts is null after init. Auto fix this instead of exception. --- src/NLog/Targets/Target.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/NLog/Targets/Target.cs b/src/NLog/Targets/Target.cs index 8f52ee65a6..2ef39c48bc 100644 --- a/src/NLog/Targets/Target.cs +++ b/src/NLog/Targets/Target.cs @@ -310,9 +310,11 @@ internal void Initialize(LoggingConfiguration configuration) { this.InitializeTarget(); this.initializeException = null; - if (this.allLayouts == null) + if (!scannedForLayouts) { - throw new NLogRuntimeException("{0}.allLayouts is null. Call base.InitializeTarget() in {0}", this.GetType()); + InternalLogger.Debug("InitializeTarget is done but not scanned For Layouts"); + //this is critical, as we need the layouts. So if base.InitializeTarget() isn't called, we fix the layouts here. + FindAllLayouts(); } } catch (Exception exception) From 5fac28420795f394221a0363423eae0e85d65e12 Mon Sep 17 00:00:00 2001 From: Julian Verdurmen <304NotModified@users.noreply.github.com> Date: Tue, 19 Apr 2016 22:28:26 +0200 Subject: [PATCH 2/2] Update unit test --- tests/NLog.UnitTests/Targets/TargetTests.cs | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/tests/NLog.UnitTests/Targets/TargetTests.cs b/tests/NLog.UnitTests/Targets/TargetTests.cs index d0783f988f..cc87ff2ba6 100644 --- a/tests/NLog.UnitTests/Targets/TargetTests.cs +++ b/tests/NLog.UnitTests/Targets/TargetTests.cs @@ -348,22 +348,6 @@ public void BlockingOperation(int millisecondsTimeout) } } - - [Fact] - public void WrongMyTargetShouldThrowException() - { - - Assert.Throws(() => - { - var target = new WrongMyTarget(); - LogManager.ThrowExceptions = true; - SimpleConfigurator.ConfigureForTargetLogging(target); - var logger = LogManager.GetLogger("WrongMyTargetShouldThrowException"); - logger.Info("Testing"); - }); - - } - [Fact] public void WrongMyTargetShouldNotThrowExceptionWhenThrowExceptionsIsFalse() { @@ -372,6 +356,8 @@ public void WrongMyTargetShouldNotThrowExceptionWhenThrowExceptionsIsFalse() SimpleConfigurator.ConfigureForTargetLogging(target); var logger = LogManager.GetLogger("WrongMyTargetShouldThrowException"); logger.Info("Testing"); + var layouts = target.GetAllLayouts(); + Assert.NotNull(layouts); } @@ -383,7 +369,7 @@ public class WrongMyTarget : Target /// protected override void InitializeTarget() { - //this is wrong. base.InitializeTarget() should be called + //base.InitializeTarget() should be called } } }