Skip to content

Commit 34b3bf6

Browse files
authored
Log exception messages on .NET 6+ when dynamic library loading fails (#204)
1 parent b0f288d commit 34b3bf6

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

source/icu.net/NativeMethods/NativeMethods.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)
347347
var libPath = string.IsNullOrEmpty(_IcuPath) ? libName : Path.Combine(_IcuPath, libName);
348348

349349
#if NET6_0_OR_GREATER
350+
string exceptionErrorMessage = null;
350351
loadMethod = "NativeLibrary.Load";
351352
try
352353
{
353354
handle = NativeLibrary.Load(libPath);
354355
}
355-
catch (DllNotFoundException)
356+
catch (DllNotFoundException ex)
356357
{
357358
handle = IntPtr.Zero;
359+
exceptionErrorMessage = ex.Message;
358360
}
359361
#else
360362
if (IsWindows)
@@ -385,14 +387,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)
385387

386388
lastError = Marshal.GetLastWin32Error();
387389
#if NET6_0_OR_GREATER
390+
if (!string.IsNullOrEmpty(exceptionErrorMessage))
391+
exceptionErrorMessage = $" ({exceptionErrorMessage})";
388392
var errorMsg = IsWindows
389-
? new Win32Exception(lastError).Message
390-
: $"{lastError}";
393+
? $"{new Win32Exception(lastError).Message}{exceptionErrorMessage}"
394+
: $"{lastError}({exceptionErrorMessage})";
391395
#else
392396
var errorMsg = IsWindows
393397
? new Win32Exception(lastError).Message
394398
: IsMac
395-
? $"{lastError}"
399+
? $"{lastError} (macOS loading requires .NET 6 or greater)"
396400
: $"{lastError} ({dlerror()})";
397401
#endif
398402
Trace.WriteLineIf(lastError != 0, $"Unable to load [{libPath}]. Error: {errorMsg}");

0 commit comments

Comments
 (0)