Skip to content

log4net logs PlatformNotSupportedException on Android #239

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

Closed
arunjose696 opened this issue Apr 24, 2025 · 22 comments
Closed

log4net logs PlatformNotSupportedException on Android #239

arunjose696 opened this issue Apr 24, 2025 · 22 comments
Assignees
Milestone

Comments

@arunjose696
Copy link

arunjose696 commented Apr 24, 2025

I'm experiencing the same problem described in issue #153 . I've put together a minimal example using a basic .NET MAUI app to reproduce it.

In this simple MAUI app, log4net doesn't seem to work. All I did was create a new .NET MAUI project in Visual Studio and install log4net via the NuGet package manager. Then, inside MauiProgram.CreateMauiApp(), I attempted to use log4net for logging. Even when I use BasicConfigurator—which, according to https://logging.apache.org/log4net/release/manual/configuration.html shouldn't require any XML configuration, it still fails.

Find below my code and exception

using Microsoft.Extensions.Logging;
using log4net;
using log4net.Config;
namespace MauiApp3
{
    public static class MauiProgram
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MauiProgram));
        public static MauiApp CreateMauiApp()
        {

            BasicConfigurator.Configure();
            log.Info("Entering application.");
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

#if DEBUG
    		builder.Logging.AddDebug();
#endif

            return builder.Build();
        }
    }
}

In the debug logs I see this exception .

[DOTNET] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[0:] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[DOTNET] System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
[DOTNET]  ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
[DOTNET]    at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
[DOTNET]    at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
[DOTNET]    at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
[DOTNET]    at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
[DOTNET]    at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
[DOTNET]    at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
[DOTNET]    at System.Configuration.ClientConfigurationSystem..ctor()
[DOTNET]    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
[DOTNET]    --- End of inner exception stack trace ---
[DOTNET]    at System.Configuration.ConfigurationManager.PrepareConfigSystem()
[DOTNET]    at System.Configuration.ConfigurationManager.GetSection(String sectionName)
[DOTNET]    at System.Configuration.ConfigurationManager.get_AppSettings()
[DOTNET]    at log4net.Util.SystemInfo.GetAppSetting(String key) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651
[0:] 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.Internal.DelegatingConfigHost.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) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651
[DOTNET] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[0:] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[DOTNET] System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
[DOTNET]  ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   --- 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) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651
[DOTNET]    at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
[DOTNET]    at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
[DOTNET]    at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
[DOTNET]    at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
[DOTNET]    at System.Configuration.ClientConfigurationHost.get_IsAppConfigHttp()
[DOTNET]    at System.Configuration.Internal.DelegatingConfigHost.get_IsAppConfigHttp()
[DOTNET]    at System.Configuration.ClientConfigurationSystem..ctor()
[DOTNET]    at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
[DOTNET]    --- End of inner exception stack trace ---
[DOTNET]    at System.Configuration.ConfigurationManager.PrepareConfigSystem()
[DOTNET]    at System.Configuration.ConfigurationManager.GetSection(String sectionName)
[DOTNET]    at System.Configuration.ConfigurationManager.get_AppSettings()
[DOTNET]    at log4net.Util.SystemInfo.GetAppSetting(String key) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651

It also fails when I use XmlConfigurator.Configure() and provide that the config file must be copied as is to the target directory (and using the default build action for config files) as mentioned in #153 (comment)

I use maui -> 9.0.4
log4net-> 3.0.4
.net -> 9

@arunjose696 arunjose696 changed the title log4net not working on MAUI app log4net throws exception on MAUI app Apr 25, 2025
@arunjose696
Copy link
Author

@FreeAndNil any suggestions?

@FreeAndNil
Copy link
Contributor

@arunjose696 can you post your config file?
Sometimes Check your .config file is well formed XML. means your config file is not well formed ;-)

@arunjose696
Copy link
Author

arunjose696 commented Apr 25, 2025

Do I even need a config file in BasicConfigurator.Configure()?;, I was using BasicConfigurator.Configure() to make the reproducer minimal so for this case I did not use a config file.

I also tried using the below example with XmlConfigurator.Configure

<log4net>
    <appender name="Console" type="log4net.Appender.ConsoleAppender">
        <layout type="log4net.Layout.PatternLayout">
            <!-- Pattern to output the caller's file name and line number -->
            <conversionPattern value="%5level [%thread] (%file:%line) - %message%newline" />
        </layout>
    </appender>
    
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
        <file value="example.log" />
        <appendToFile value="true" />
        <maximumFileSize value="100KB" />
        <maxSizeRollBackups value="2" />
 
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level %thread %logger - %message%newline" />
        </layout>
    </appender>
    
    <root>
        <level value="DEBUG" />
        <appender-ref ref="Console" />
        <appender-ref ref="RollingFile" />
    </root>
</log4net>

@FreeAndNil
Copy link
Contributor

This is a valid log4net configuration, but not a valid app.config.
Can you add a configuration element and a config section element?

@arunjose696
Copy link
Author

I am using MAUI with dotnet 9, there is no app.config file,

From what I understand from here app.config is a older style https://learn.microsoft.com/en-us/answers/questions/1510926/why-do-some-c-applications-have-an-app-config-file

Do you have any documentation on how to do the cofiguration for MAUI apps?

@FreeAndNil
Copy link
Contributor

You just add a config file via "Add ..." and paste the content from #153 (comment)

@arunjose696
Copy link
Author

I tried adding the app.config file still I see the Exception

@FreeAndNil
Copy link
Contributor

Can you zip your test project and post it here?

@FreeAndNil FreeAndNil self-assigned this Apr 25, 2025
@arunjose696
Copy link
Author

Here is the zip file, There are just very minor changes from the basic MAUI app created in visual studio.

MauiAppLog4net.zip

@FreeAndNil
Copy link
Contributor

Your app runs fine on my PC (Win11 24H2 / VS 17.13.6).
For another test I have added a file appender and moved from BasicConfigurator to XmlConfigurator.

2025-04-26 21:11:18,276| INFO | Entering application.

Please take a look at the adjusted sample:

MauiApp3.zip

@arunjose696
Copy link
Author

Thank you for the response. I also tried using this zip file.

The code runs perfectly fine when executed on the Windows machine in Visual Studio, but the exception only occurs when I try running it on an Android device or emulator. I change the run configuration in Visual Studio from the Windows machine to the Android emulator. Could you please try running it on an Android emulator in Visual Studio?

Apologies for not mentioning earlier that I am running on Android.

@FreeAndNil
Copy link
Contributor

FreeAndNil commented Apr 28, 2025

You need to use xml (config) files for android.
log4net supports xml files on all plattforms.

See the attached example.

MauiApp4.zip

More details for log4net config files can be found here

@arunjose696
Copy link
Author

arunjose696 commented Apr 28, 2025

You need to use xml (config) files for android. log4net supports xml files on all plattforms.

See the attached example.

MauiApp4.zip

Do you have the logs being written with this project? Because in my end it is just the same exception as before in the Debug logs. I see the complaint that the config file is not well formed even when using the same project without any changes.

Could you share the path of the log file created? Also are there no exceptions like below in the debug logs in visual studio?

[0:] 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.Internal.DelegatingConfigHost.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) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651
[DOTNET] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[0:] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
[DOTNET] System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
[DOTNET]  ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
[0:] 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.Internal.DelegatingConfigHost.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) in D:\Git\apache\logging-log4net\src\log4net\Util\SystemInfo.cs:line 651


@FreeAndNil
Copy link
Contributor

I have no android device for testing.
You can adjust the path of the log file (for testing full qualified) in log4net.xml (log4net/appender(FileAppender)/file.value

The message above is "just" a warning. I will fix this, when your main problem is solved.

@arunjose696
Copy link
Author

I see maui.log is the value , I dont see this file being created in the Android device or the emulator.

@FreeAndNil
Copy link
Contributor

Can you fully qualify the file name in the xml file?

@arunjose696
Copy link
Author

As directly giving the fully quallified path in xml wont work in maui, I think I will need to provide this somehow through the code which I need to figure out, If I dont give the filename too shouldnt it log to the console? I donmt see the logs being logged to console as well.

@FreeAndNil
Copy link
Contributor

You can read the log4net config from an embedded resource: MauiApp5.zip

This way (and changing the file name to fully qualified) works for me:

Image

@arunjose696
Copy link
Author

arunjose696 commented Apr 29, 2025

Thanks very much. Making the app as embedded resource works for me.

The fully quallified name though works on the android emulator did not work on the real android device. However just using maui.log as the filename writes the log to this path /data/data/com.companyname.mauiapp3/files/maui.log. Which means the logging defenitely works without issues.

Still in the debug logs we have the warning/traceback which says [DOTNET] log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML. which I think needs to be fixed. I assume there are no issues in perormance with this.

@FreeAndNil
Copy link
Contributor

Thanks for the confirmation.
I will provide a pull request to fix the warning / error message on android.

FreeAndNil added a commit that referenced this issue Apr 29, 2025
@FreeAndNil FreeAndNil changed the title log4net throws exception on MAUI app log4net logs PlatformNotSupportedException on Android Apr 29, 2025
@FreeAndNil FreeAndNil added this to the 3.1.0 milestone Apr 29, 2025
FreeAndNil added a commit that referenced this issue Apr 29, 2025
FreeAndNil added a commit that referenced this issue Apr 29, 2025
@FreeAndNil
Copy link
Contributor

@arunjose696 see #240
Can you check with https://www.nuget.org/packages/log4net/3.1.0-preview.3 ?

FreeAndNil added a commit that referenced this issue Apr 29, 2025
#239 detect Android and use environment variables instead of AppSettings
@arunjose696
Copy link
Author

Thanks the traceback is gone now, checked with prerelease.

FreeAndNil added a commit that referenced this issue May 7, 2025
FreeAndNil added a commit that referenced this issue May 7, 2025
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