Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlatformNotSupportedExceptions when loading log4net #162

Open
viktorgobbi opened this issue Jul 31, 2024 · 3 comments
Open

PlatformNotSupportedExceptions when loading log4net #162

viktorgobbi opened this issue Jul 31, 2024 · 3 comments
Assignees

Comments

@viktorgobbi
Copy link

viktorgobbi commented Jul 31, 2024

Hi,
we recently ran into a problem with log4net where PlatformNotSupportedExceptions are logged after switching from the net45 build to the netstandard2.0 assembly of log4net.

We ran into this issue with class libraries ( to provide windows powerShell cmdlets) where we use log4net. We are currently in the process of migrating our projects to target .net standard2.0 but after the migration we noticed error messages when log4net is loaded in a windows 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
  • Finally the static constructors in:

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
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!

@FreeAndNil
Copy link
Contributor

@viktorgobbi I can reproduce your problem.

Are you trying to use log4net netstandard2.0 on net48?

This will probably not work.
I can't even create a console app on net48 which uses log4net netstandard2.0.
The compiler always prefers the net461 version for the output directory.

My proposal would be using log4net net461 for windows powershell and log4net netstandard2.0 for powershell core.
You can create a console app for those two frameworks to find out which additional assemblies you need to deploy with your application.

@viktorgobbi
Copy link
Author

@FreeAndNil Thanks for the quick reply!

Are you trying to use log4net netstandard2.0 on net48?

Yes, the idea was to provide one assembly targeting .netstandard2.0 for both windows powerShell and powerShell core to keep the deployment simple. Other than this issue it also worked quite well. It is probably worth mentioning that log4net seems to work correctly, the error messages are only logged directly to the console which is the main problem.

I can't even create a console app on net48 which uses log4net netstandard2.0.
The compiler always prefers the net461 version for the output directory.

In our case we have an intermediate nuget package which targets .netstandard2.0 and depends on log4net and our assemblies containing the powershell cmdlets also target .netstandard2.0 which is why it has to use the .netstandard2.0 assembly of log4net.

My proposal would be using log4net net461 for windows powershell and log4net netstandard2.0 for powershell core.

I came to the same conclusion but I am trying to avoid that as it would complicate our build and deployment quite a lot and as long as it's only an internal log message it doesn't pose that big of an issue for us.

I did some more research and found out why it wouldn't work with newer versions of the System.Configuration.ConfigurationManager package (>= 5.0.0) and it turns out it is related to the content of the powerShell.exe.config which contains a section that is no longer supported.

I also managed to get the .netstandard2.0 assembly working without errors on .net framework but only through reflection so not really a solution but more like a fragile workaround.

FYI:
But I still think that this could be an issue in other scenarios that needs to be addressed in the log4net package since System.Reflection.Assembly.GetEntryAssembly() can return null and System.Configuration.ConfigurationManager in version 4.5.0 will throw a PlatformNotSupportedException in that case as mentioned in dotnet/runtime#25027 which will lead, in my opinion, to the same error messages I originally posted. I currently have no example project since the powerShell core console is a managed application but whenever a powershell core is hosted in a native process this should be reproducible similar to the windows powershell case.

@FreeAndNil
Copy link
Contributor

@viktorgobbi Thanks for the detailed explanation and your reasoning.
We had exactly this error message in our native application hosting .net core and couldn't find out what caused this error.
I will take a closer look and get back to you.

@FreeAndNil FreeAndNil self-assigned this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants