Skip to content

Commit

Permalink
PlatformDetector.IsMono is only for legacy .NET Framework (#4919)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed May 12, 2022
1 parent 5871087 commit ceca8c2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
62 changes: 16 additions & 46 deletions src/NLog/Internal/MutexDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,62 +49,32 @@ internal static class MutexDetector
/// <summary>
/// Gets a value indicating whether current runtime supports use of mutex
/// </summary>
public static bool SupportsSharableMutex
{
get
{
#if NETSTANDARD1_5
return RunTimeSupportsSharableMutex;
#elif !NETSTANDARD1_3
if (PlatformDetector.IsMono && Environment.Version.Major < 4)
return false; // MONO ver. 4 is needed for named Mutex to work
else
return RunTimeSupportsSharableMutex;
#else
return false;
#endif
}
}

/// <summary>
/// Will creating a mutex succeed runtime?
/// "Cached" detection
/// </summary>
private static bool? _runTimeSupportsSharableMutex;
public static bool SupportsSharableMutex => _supportsSharableMutex ?? (_supportsSharableMutex = ResolveSupportsSharableMutex()).Value;
private static bool? _supportsSharableMutex;

/// <summary>
/// Will creating a mutex succeed runtime?
/// </summary>
private static bool RunTimeSupportsSharableMutex
private static bool ResolveSupportsSharableMutex()
{
get
try
{
if (_runTimeSupportsSharableMutex.HasValue)
{
return _runTimeSupportsSharableMutex.Value;
}


try
{
#if SupportsMutex
var mutex = BaseMutexFileAppender.ForceCreateSharableMutex("NLogMutexTester");
mutex.Close(); //"dispose"

_runTimeSupportsSharableMutex = true;
#else
_runTimeSupportsSharableMutex = false;
#if !NETSTANDARD
if (Environment.Version.Major < 4 && PlatformDetector.IsMono)
return false; // MONO ver. 4 is needed for named Mutex to work
#endif
}
catch (Exception ex)
{
InternalLogger.Debug(ex, "Failed to create sharable mutex processes");
_runTimeSupportsSharableMutex = false;
}

return _runTimeSupportsSharableMutex.Value;
var mutex = BaseMutexFileAppender.ForceCreateSharableMutex("NLogMutexTester");
mutex.Close(); //"dispose"
return true;
#endif
}
catch (Exception ex)
{
InternalLogger.Debug(ex, "Failed to create sharable mutex processes");
}

return false;
}
}
}
11 changes: 6 additions & 5 deletions src/NLog/Internal/PlatformDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,29 @@ namespace NLog.Internal
/// </summary>
internal static class PlatformDetector
{
private static readonly RuntimeOS currentOS = GetCurrentRuntimeOS();

/// <summary>
/// Gets the current runtime OS.
/// </summary>
public static RuntimeOS CurrentOS => currentOS;
public static RuntimeOS CurrentOS => _currentOS ?? (_currentOS = GetCurrentRuntimeOS()).Value;
private static RuntimeOS? _currentOS;

/// <summary>
/// Gets a value indicating whether current OS is Win32-based (desktop or mobile).
/// </summary>
public static bool IsWin32 => currentOS == RuntimeOS.Windows || currentOS == RuntimeOS.WindowsNT;
public static bool IsWin32 => CurrentOS == RuntimeOS.Windows || CurrentOS == RuntimeOS.WindowsNT;

/// <summary>
/// Gets a value indicating whether current OS is Unix-based.
/// </summary>
public static bool IsUnix => currentOS == RuntimeOS.Linux || currentOS == RuntimeOS.MacOSX;
public static bool IsUnix => CurrentOS == RuntimeOS.Linux || CurrentOS == RuntimeOS.MacOSX;

#if !NETSTANDARD
/// <summary>
/// Gets a value indicating whether current runtime is Mono-based
/// </summary>
public static bool IsMono => _isMono ?? (_isMono = Type.GetType("Mono.Runtime") != null).Value;
private static bool? _isMono;
#endif

private static RuntimeOS GetCurrentRuntimeOS()
{
Expand Down
8 changes: 4 additions & 4 deletions src/NLog/LogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,11 +1423,11 @@ private void OnStopLogging(object sender, EventArgs args)
//Exception: System.AppDomainUnloadedException
//Message: Attempted to access an unloaded AppDomain.
InternalLogger.Info("AppDomain Shutting down. LogFactory closing...");
// Finalizer thread has about 2 secs on Windows-platform, before being terminated
// MONO (and friends) have a hard time with spinning up flush threads/timers during domain unload
// Domain-Unload has to complete in about 2 secs on Windows-platform, before being terminated.
// Other platforms like Linux will fail when trying to spin up new threads at domain unload.
var flushTimeout =
#if !NETSTANDARD1_3 && !MONO
PlatformDetector.IsWin32 && !PlatformDetector.IsMono ? TimeSpan.FromMilliseconds(1500) :
#if !NETSTANDARD1_3
PlatformDetector.IsWin32 ? TimeSpan.FromMilliseconds(1500) :
#endif
TimeSpan.Zero;
Close(flushTimeout);
Expand Down
2 changes: 2 additions & 0 deletions src/NLog/Targets/ConsoleTargetHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public static bool IsConsoleAvailable(out string reason)
{
if (!Environment.UserInteractive)
{
#if !NETSTANDARD
if (Internal.PlatformDetector.IsMono && Console.In is StreamReader)
return true; // Extra bonus check for Mono, that doesn't support Environment.UserInteractive
#endif

reason = "Environment.UserInteractive = False";
return false;
Expand Down
2 changes: 2 additions & 0 deletions tests/NLog.UnitTests/Internal/PlatformDetectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace NLog.UnitTests.Internal
{
public class PlatformDetectorTests
{
#if !NETSTANDARD
[Fact]
public void IsMonoTest()
{
Expand All @@ -47,6 +48,7 @@ public void IsMonoTest()
Assert.False(PlatformDetector.IsMono);
#endif
}
#endif

[Fact]
public void GetCurrentOSTest()
Expand Down

0 comments on commit ceca8c2

Please sign in to comment.