Skip to content

Commit

Permalink
Log exception messages on .NET 6+ when dynamic library loading fails (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil authored Aug 29, 2024
1 parent b0f288d commit 34b3bf6
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions source/icu.net/NativeMethods/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)
var libPath = string.IsNullOrEmpty(_IcuPath) ? libName : Path.Combine(_IcuPath, libName);

#if NET6_0_OR_GREATER
string exceptionErrorMessage = null;
loadMethod = "NativeLibrary.Load";
try
{
handle = NativeLibrary.Load(libPath);
}
catch (DllNotFoundException)
catch (DllNotFoundException ex)
{
handle = IntPtr.Zero;
exceptionErrorMessage = ex.Message;
}
#else
if (IsWindows)
Expand Down Expand Up @@ -385,14 +387,16 @@ private static IntPtr GetIcuLibHandle(string basename, int icuVersion)

lastError = Marshal.GetLastWin32Error();
#if NET6_0_OR_GREATER
if (!string.IsNullOrEmpty(exceptionErrorMessage))
exceptionErrorMessage = $" ({exceptionErrorMessage})";
var errorMsg = IsWindows
? new Win32Exception(lastError).Message
: $"{lastError}";
? $"{new Win32Exception(lastError).Message}{exceptionErrorMessage}"
: $"{lastError}({exceptionErrorMessage})";
#else
var errorMsg = IsWindows
? new Win32Exception(lastError).Message
: IsMac
? $"{lastError}"
? $"{lastError} (macOS loading requires .NET 6 or greater)"
: $"{lastError} ({dlerror()})";
#endif
Trace.WriteLineIf(lastError != 0, $"Unable to load [{libPath}]. Error: {errorMsg}");
Expand Down

0 comments on commit 34b3bf6

Please sign in to comment.