Skip to content

Commit 418062c

Browse files
committed
#239 detect Android and use environment variables instead of AppSettings
1 parent 9562bae commit 418062c

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

src/log4net.Tests/Util/SystemInfoTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ public void EqualsIgnoringCase_SameStringsDifferentCase_true()
166166
[Test]
167167
public void EqualsIgnoringCase_DifferentStrings_false()
168168
=> Assert.That(SystemInfo.EqualsIgnoringCase("foo", "foobar"), Is.False);
169-
}
169+
170+
[Test]
171+
[Platform(Include = "Win,Linux,MacOsX")]
172+
public void IsAndoid()
173+
=> Assert.That(typeof(SystemInfo).GetProperty("IsAndroid", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null), Is.False);
174+
}

src/log4net/Appender/AdoNetAppender.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,7 @@ protected override void SendBuffer(LoggingEvent[] events)
449449
/// Adds a parameter to the ordered list of command parameters.
450450
/// </para>
451451
/// </remarks>
452-
public void AddParameter(AdoNetAppenderParameter parameter)
453-
{
454-
m_parameters.Add(parameter);
455-
}
452+
public void AddParameter(AdoNetAppenderParameter parameter) => m_parameters.Add(parameter);
456453

457454
/// <summary>
458455
/// Writes the events to the database using the transaction specified.

src/log4net/Core/DefaultRepositorySelector.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,8 @@ public void AliasRepository(string repositoryAlias, ILoggerRepository repository
469469
/// Raises the <see cref="LoggerRepositoryCreatedEvent"/> event.
470470
/// </para>
471471
/// </remarks>
472-
protected virtual void OnLoggerRepositoryCreatedEvent(ILoggerRepository repository)
473-
{
474-
LoggerRepositoryCreatedEvent?.Invoke(this, new LoggerRepositoryCreationEventArgs(repository));
475-
}
472+
protected virtual void OnLoggerRepositoryCreatedEvent(ILoggerRepository repository)
473+
=> LoggerRepositoryCreatedEvent?.Invoke(this, new(repository));
476474

477475
/// <summary>
478476
/// Gets the repository name and repository type for the specified assembly.

src/log4net/Util/SystemInfo.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public static class SystemInfo
3636
private const string DefaultNullText = "(null)";
3737
private const string DefaultNotAvailableText = "NOT AVAILABLE";
3838

39+
/// <summary>
40+
/// Is OperatingSystem Android
41+
/// </summary>
42+
internal static bool IsAndroid { get; } = IsAndroidCore();
43+
3944
/// <summary>
4045
/// Initialize default values for private static fields.
4146
/// </summary>
@@ -68,6 +73,34 @@ static SystemInfo()
6873
NullText = nullText;
6974
}
7075

76+
private static bool IsAndroidCore() // https://stackoverflow.com/questions/47521008/how-can-i-distinguish-between-unix-and-android-on-netstandard-2-0
77+
{
78+
if (Environment.OSVersion.Platform != PlatformID.Unix)
79+
return false;
80+
using System.Diagnostics.Process process = new()
81+
{
82+
StartInfo = new()
83+
{
84+
FileName = "getprop",
85+
Arguments = "ro.build.user",
86+
RedirectStandardOutput = true,
87+
UseShellExecute = false,
88+
CreateNoWindow = true
89+
}
90+
};
91+
92+
try
93+
{
94+
process.Start();
95+
string output = process.StandardOutput.ReadToEnd();
96+
return !string.IsNullOrEmpty(output);
97+
}
98+
catch (Exception ex) when (!ex.IsFatal())
99+
{
100+
return false;
101+
}
102+
}
103+
71104
/// <summary>
72105
/// Gets the system dependent line terminator.
73106
/// </summary>
@@ -414,10 +447,8 @@ public static string AssemblyFileName(Assembly myAssembly)
414447
/// then all the loaded assemblies will be searched for the type.
415448
/// </para>
416449
/// </remarks>
417-
public static Type? GetTypeFromString(Type relativeType, string typeName, bool throwOnError, bool ignoreCase)
418-
{
419-
return GetTypeFromString(relativeType.EnsureNotNull().Assembly, typeName, throwOnError, ignoreCase);
420-
}
450+
public static Type? GetTypeFromString(Type relativeType, string typeName, bool throwOnError, bool ignoreCase)
451+
=> GetTypeFromString(relativeType.EnsureNotNull().Assembly, typeName, throwOnError, ignoreCase);
421452

422453
/// <summary>
423454
/// Loads the type specified in the type string.
@@ -438,10 +469,8 @@ public static string AssemblyFileName(Assembly myAssembly)
438469
/// in the assembly then all the loaded assemblies will be searched for the type.
439470
/// </para>
440471
/// </remarks>
441-
public static Type? GetTypeFromString(string typeName, bool throwOnError, bool ignoreCase)
442-
{
443-
return GetTypeFromString(Assembly.GetCallingAssembly(), typeName, throwOnError, ignoreCase);
444-
}
472+
public static Type? GetTypeFromString(string typeName, bool throwOnError, bool ignoreCase)
473+
=> GetTypeFromString(Assembly.GetCallingAssembly(), typeName, throwOnError, ignoreCase);
445474

446475
/// <summary>
447476
/// Loads the type specified in the type string.
@@ -646,6 +675,8 @@ public static bool TryParse(string s, out short val)
646675
/// <returns>the value for the key, or <c>null</c></returns>
647676
public static string? GetAppSetting(string key)
648677
{
678+
if (IsAndroid)
679+
return Environment.GetEnvironmentVariable(key); // Android does not support config files
649680
try
650681
{
651682
return ConfigurationManager.AppSettings[key];

src/site/antora/modules/ROOT/pages/manual/configuration.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ The section must specify the `log4net.Config.Log4NetConfigurationSectionHandler,
349349
350350
The following is a simple example configuration file that specifies the correct section handler to use for the `log4net` section.
351351
352-
353352
[source,xml]
354353
----
355354
<configuration>
@@ -427,6 +426,15 @@ The example shows how to embed configuration data in a .config file while allowi
427426
Since the .NET config parser throws errors for unregistered elements, the `log4net` section is registered using `System.Configuration.IgnoreSectionHandler`.
428427
This tells .NET to ignore the section, as it will be processed by log4net instead.
429428
429+
[#android]
430+
== Android
431+
432+
Android does not support `.config` files.
433+
434+
Instead, you can set `AppSettings` values as environment variables for your process — this works only on Android.
435+
436+
You must also load the `log4net` configuration manually, for example by reading it from an XML file at runtime.
437+
430438
[#configuration-syntax]
431439
== Configuration Syntax
432440

0 commit comments

Comments
 (0)