Skip to content

Commit f674080

Browse files
committed
Target reports when disabled because failed to initialize
1 parent f4f5e93 commit f674080

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/NLog/Targets/Target.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ public void WriteAsyncLogEvents(IList<AsyncLogEventInfo> logEvents)
403403
/// </summary>
404404
protected virtual void WriteFailedNotInitialized(AsyncLogEventInfo logEvent, Exception initializeException)
405405
{
406+
if (!_scannedForLayouts)
407+
{
408+
_scannedForLayouts = true;
409+
InternalLogger.Error(_initializeException, "{0}: Disabled because NLog Target failed to initialize.", this);
410+
}
411+
else
412+
{
413+
InternalLogger.Debug("{0}: Disabled because NLog Target failed to initialize. {1} {2}", this, _initializeException?.GetType(), _initializeException?.Message);
414+
}
406415
var initializeFailedException = new NLogRuntimeException($"Target {this} failed to initialize.", initializeException);
407416
logEvent.Continuation(initializeFailedException);
408417
}
@@ -421,6 +430,8 @@ internal void Initialize(LoggingConfiguration configuration)
421430
{
422431
try
423432
{
433+
_scannedForLayouts = false;
434+
424435
PropertyHelper.CheckRequiredParameters(ConfigurationItemFactory.Default, this);
425436

426437
InitializeTarget();
@@ -436,13 +447,15 @@ internal void Initialize(LoggingConfiguration configuration)
436447
{
437448
// Target is now in disabled state, and cannot be used for writing LogEvents
438449
_initializeException = exception;
450+
_scannedForLayouts = false;
439451
if (ExceptionMustBeRethrown(exception))
440452
throw;
441453
}
442454
catch (Exception exception)
443455
{
444456
// Target is now in disabled state, and cannot be used for writing LogEvents
445457
_initializeException = exception;
458+
_scannedForLayouts = false;
446459
if (ExceptionMustBeRethrown(exception))
447460
throw;
448461

tests/NLog.UnitTests/Targets/TargetWithContextTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void TargetWithContextAsyncTest()
127127
private static bool WaitForLastMessage(CustomTargetWithContext target)
128128
{
129129
System.Threading.Thread.Sleep(1);
130-
for (int i = 0; i < 1000; ++i)
130+
for (int i = 0; i < 5000; ++i)
131131
{
132132
if (target.LastMessage != null)
133133
return true;
@@ -328,7 +328,7 @@ public void TargetWithContextJsonTest()
328328
logger.Error("log message");
329329
var target = logFactory.Configuration.AllTargets.OfType<CustomTargetWithContext>().FirstOrDefault();
330330
System.Threading.Thread.Sleep(1);
331-
for (int i = 0; i < 1000; ++i)
331+
for (int i = 0; i < 5000; ++i)
332332
{
333333
if (target.LastMessage != null)
334334
break;

0 commit comments

Comments
 (0)