apache / logging-log4net

Apache Log4net is a versatile, feature-rich, efficient logging API and backend for .NET
https://logging.apache.org/log4net
Apache License 2.0
848 stars 323 forks source link

PlatformNotSupportedExceptions when loading log4net #162

Open viktorgobbi opened 1 month ago

viktorgobbi commented 1 month ago

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:

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:

Any suggestions or workarounds would be appreciated!

FreeAndNil commented 1 month ago

@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 commented 1 month ago

@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 https://github.com/dotnet/runtime/issues/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 commented 1 month ago

@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.