viktorgobbi opened a new issue, #162:
URL: https://github.com/apache/logging-log4net/issues/162

   Hi,
   we recently ran into a problem with log4net where 
`PlatformNotSupportedException` are logged after we switched from the `net45` 
assembly to the `netstandard2.0` assembly.
   
   We ran into this issue with class libraries ( to provide powerShell cmdlets) 
where we use log4net. We are currently in the process of migrating our projects 
to target .net standard2.0 to support powerShell Core but after the migration 
we noticed error messages when log4net is loaded in a powershell console 
(powershell.exe) process. The following is logged to the console:
   
   > log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   log4net:ERROR Exception while reading ConfigurationSettings. Check your 
.config file is well formed XML.
   System.Configuration.ConfigurationErrorsException: Configuration system 
failed to initialize ---> System.PlatformNotSupportedException: Operation is 
not supported on this platform.
      at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean 
includeUserConfig)
      at System.Configuration.ClientConfigPaths.GetPaths(String exePath, 
Boolean includeUserConfig)
      at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
      at System.Configuration.ClientConfigurationHost.GetStreamName(String 
configPath)
      at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
      at System.Configuration.ClientConfigurationSystem..ctor()
      at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
      --- End of inner exception stack trace ---
      at System.Configuration.ConfigurationManager.PrepareConfigSystem()
      at System.Configuration.ConfigurationManager.GetSection(String 
sectionName)
      at System.Configuration.ConfigurationManager.get_AppSettings()
      at log4net.Util.SystemInfo.GetAppSetting(String key)
   
   This issue is not reproducible with the log4net assembly targeting `net45`.
   
   I have tried to analyze this issue and traced it down to the following 
points:
     * The powershell.exe itself is not a manged application and therefore 
`System.Reflection.Assembly.GetEntryAssembly()` returns `null` (easily 
observable by starting a powershell.exe console and running 
`[System.Reflection.Assembly]::GetEntryAssembly()`)
     * The log4net build for .netstandard2.0 uses the 
`System.Configuration.ConfigurationManager` in Version 4.5.0 which throws a 
`PlatformNotSupportedException` when the `Assembly.GetEntryAssembly()` call 
returns `null` but this was addressed with Version >= 5.0.0 and was tracked in 
[dotnet/runtime#25027](https://github.com/dotnet/runtime/issues/25027)
     * Finally the **static constructors** in:
       * 
https://github.com/apache/logging-log4net/blob/ad0180f9bfb712cd52bda28b74ec7d64ea7f3bb8/src/log4net/Core/LoggerManager.cs#L108
       * 
https://github.com/apache/logging-log4net/blob/ad0180f9bfb712cd52bda28b74ec7d64ea7f3bb8/src/log4net/Util/SystemInfo.cs#L85
       * and 
https://github.com/apache/logging-log4net/blob/ad0180f9bfb712cd52bda28b74ec7d64ea7f3bb8/src/log4net/Util/LogLog.cs#L174
     call `log4net.Util.SystemInfo.GetAppSetting(string)` which for 
netstandard2.0 calls `ConfigurationManager.AppSettings[key]` which ultimately 
causes the exception that is logged.
   
   I've tried using a newer version of the 
`System.Configuration.ConfigurationManager` package but ran into similar issues 
but with other error messages and since we only provide class libraries and 
cannot modify the app.config for the powershell.exe we have no good way to 
influence the binding behavior to get log4net working with a newer 
`System.Configuration.ConfigurationManager` version in a production 
environment. I did not figure out why the issue is not present in the log4net 
assembly that is targeting net45 but I assume it has to do with the 
implementation of `System.Configuration.ConfigurationManager` in .NET Framework 
versus the one provided by the Nuget package.
   
   We are using log4net 2.0.14 but the issue is also reproducible with the 
latest version (v2.0.17 as of writing this).
   I've attached a very simple solution to reproduce the issue 
[log4netConfigErrorSln.zip](https://github.com/user-attachments/files/16444582/log4netConfigErrorSln.zip)
   To reproduce it:
     * Build the solution
     * Start a powershell.exe and load the assembly in the powershell console 
with `Add-Type -Path "<pathToSolutionOutput>\netstandard2.0\Lib1.dll"`
     * Run `[Lib1.Lib1]::LogSomething()` in the powershell console to call the 
static method and force log4net to initialize
   
   Any suggestions or workarounds would be appreciated!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to