Skip to content

Commit

Permalink
ObjectGraphScanner - Handle property-getter that throws when AOT (#5652)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Oct 21, 2024
1 parent e260830 commit fbf791e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 36 deletions.
11 changes: 0 additions & 11 deletions .bettercodehub.yml

This file was deleted.

16 changes: 0 additions & 16 deletions .coderabbit.yaml

This file was deleted.

22 changes: 19 additions & 3 deletions src/NLog/Internal/ObjectGraphScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ private static void ScanProperties<T>(ConfigurationItemFactory configFactory, bo
if (string.IsNullOrEmpty(configProp.Key))
continue; // Ignore default values

if (!PropertyHelper.IsConfigurationItemType(configFactory, configProp.Value.PropertyType))
var propInfo = configProp.Value;
if (!PropertyHelper.IsConfigurationItemType(configFactory, propInfo.PropertyType))
continue;

var propInfo = configProp.Value;
var propValue = propInfo.GetValue(targetObject, null);
object propValue = ScanPropertyValue(targetObject, type, propInfo);
if (propValue is null)
continue;

Expand All @@ -122,6 +122,22 @@ private static void ScanProperties<T>(ConfigurationItemFactory configFactory, bo
}
}

private static object ScanPropertyValue(object targetObject, Type type, PropertyInfo propInfo)
{
try
{
return propInfo.GetValue(targetObject, null);
}
catch (Exception exception)
{
InternalLogger.Warn(exception, "Failed scanning property: {0}.{1}", type, propInfo.Name);
if (exception.MustBeRethrownImmediately())
throw;

return null;
}
}

private static void ScanPropertyForObject<T>(ConfigurationItemFactory configFactory, bool aggressiveSearch, object propValue, PropertyInfo prop, List<T> result, int level, HashSet<object> visitedObjects) where T : class
{
if (InternalLogger.IsTraceEnabled)
Expand Down
35 changes: 31 additions & 4 deletions tests/NLog.Database.Tests/DatabaseTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1707,11 +1707,24 @@ public void SqlServer_NoTargetInstallException()
}

bool isAppVeyor = IsAppVeyor();
SqlServerTest.TryDropDatabase(isAppVeyor);

try
{
SqlServerTest.CreateDatabase(isAppVeyor);
for (int i = 1; i <= 3; ++i)
{
try
{
SqlServerTest.TryDropDatabase(isAppVeyor);
SqlServerTest.CreateDatabase(isAppVeyor);
break;
}
catch
{
if (i >= 3)
throw;
System.Threading.Thread.Sleep(i * 5000);
}
}

var connectionString = SqlServerTest.GetConnectionString(isAppVeyor);

Expand Down Expand Up @@ -1759,11 +1772,25 @@ public void SqlServer_InstallAndLogMessage()
}

bool isAppVeyor = IsAppVeyor();
SqlServerTest.TryDropDatabase(isAppVeyor);

try
{
SqlServerTest.CreateDatabase(isAppVeyor);
for (int i = 1; i <= 3; ++i)
{
try
{
SqlServerTest.TryDropDatabase(isAppVeyor);
SqlServerTest.CreateDatabase(isAppVeyor);
break;
}
catch
{
if (i >= 3)
throw;

System.Threading.Thread.Sleep(i * 5000);
}
}

var connectionString = SqlServerTest.GetConnectionString(IsAppVeyor());

Expand Down
6 changes: 6 additions & 0 deletions tests/NLog.Targets.Network.Tests/NetworkTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,14 @@ public void NetworkTargetUdpSplitEnabledTest()
try
{
NetworkTargetUdpTest(true);
break;
}
catch
{
if (i == 3)
throw;

System.Threading.Thread.Sleep(1000 * i);
}
}
}
Expand All @@ -673,11 +676,14 @@ public void NetworkTargetUdpSplitDisabledTest()
try
{
NetworkTargetUdpTest(false);
break;
}
catch
{
if (i == 3)
throw;

System.Threading.Thread.Sleep(1000 * i);
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions tests/NLog.UnitTests/Targets/Wrappers/AsyncTargetWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ public void AsyncTargetWrapperInitTest2()
[Fact]
public void AsyncTargetWrapperSyncTest_WithLock_WhenTimeToSleepBetweenBatchesIsEqualToZero()
{
AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero(true);
RetryingIntegrationTest(3, () =>
{
AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero(true);
});
}

[Fact]
public void AsyncTargetWrapperSyncTest_NoLock_WhenTimeToSleepBetweenBatchesIsEqualToZero()
{
AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero(false);
RetryingIntegrationTest(3, () =>
{
AsyncTargetWrapperSyncTest_WhenTimeToSleepBetweenBatchesIsEqualToZero(false);
});
}

/// <summary>
Expand Down

0 comments on commit fbf791e

Please sign in to comment.