Skip to content

Commit

Permalink
Merge pull request NLog#1386 from NLog/fix-InitializeTarget-exception
Browse files Browse the repository at this point in the history
Fix "allLayouts is null" exception
  • Loading branch information
304NotModified committed Apr 20, 2016
2 parents 66f445a + 5fac284 commit 2e155a6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/NLog/Targets/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 3 additions & 17 deletions tests/NLog.UnitTests/Targets/TargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,22 +348,6 @@ public void BlockingOperation(int millisecondsTimeout)
}
}


[Fact]
public void WrongMyTargetShouldThrowException()
{

Assert.Throws<NLogRuntimeException>(() =>
{
var target = new WrongMyTarget();
LogManager.ThrowExceptions = true;
SimpleConfigurator.ConfigureForTargetLogging(target);
var logger = LogManager.GetLogger("WrongMyTargetShouldThrowException");
logger.Info("Testing");
});

}

[Fact]
public void WrongMyTargetShouldNotThrowExceptionWhenThrowExceptionsIsFalse()
{
Expand All @@ -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);
}


Expand All @@ -383,7 +369,7 @@ public class WrongMyTarget : Target
/// </summary>
protected override void InitializeTarget()
{
//this is wrong. base.InitializeTarget() should be called
//base.InitializeTarget() should be called
}
}
}
Expand Down

0 comments on commit 2e155a6

Please sign in to comment.