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
859 stars 327 forks source link

Configuration System Initialization Failure with PublishTrimmed in .NET 8 #165

Closed tmoonlight closed 2 months ago

tmoonlight commented 2 months ago

Hi,

I am using log4net in my project running on .NET 8. When I enable the “PublishTrimmed” setting in my project file (< PublishTrimmed >true< /PublishTrimmed >), I consistently receive the following errors during the configuration of log4net:


Starting up as a console service host
Service NSPServer started
The NSPServer service is now running, press Control+C to exit.
Another instance of the program is running. It may cause fatal error.
Initializing..
log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML.
System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize
 ---> System.MissingMethodException: Cannot dynamically create an instance of type 'System.Configuration.ClientConfigurationHost'. Reason: No parameterless constructor defined.
   at System.RuntimeType.ActivatorCache..ctor(RuntimeType)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean, Boolean)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Configuration.TypeUtil.CreateInstance(Type)
   at System.Configuration.Internal.ConfigSystem.System.Configuration.Internal.IConfigSystem.Init(Type, Object[] )
   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)
   at System.Configuration.ConfigurationManager.get_AppSettings()
   at log4net.Util.SystemInfo.GetAppSetting(String)

This issue persists even when I configure log4net purely through code, without using a log4net.config file. The problem does not occur in non-trimmed mode or older versions of the trimming mode (such as .NET Core 3.0).

To assist in diagnosing this issue, I have attached a file containing my program that can reliably reproduce the problem: https://github.com/tmoonlight/NSmartProxy/releases/download/v1.4.0/nspclient_scd_win_x86_v1.4.0.zip

FreeAndNil commented 2 months ago

related to #147 related to #142 maybe solved by #166

FreeAndNil commented 2 months ago

@tmoonlight can you check whether #166 fixes your problem? Just remove the .zip extension from log4net.3.0.0-preview.2a.nupkg.zip

In case it doesn't fix your problem, can you please create a tiny project reproducing the error? The best would be a net8.0 application which only depends on log4net.

tmoonlight commented 2 months ago

I have followed your method, but the issue with the PublishTrimmed program still persists. image

tmoonlight commented 2 months ago

Hi, The issue seems to be more complex than initially thought. I found that not only is log4net affected, but other parts of the code also encounter problems when trimming. For example, embedded strings and resources in my project are no longer accessible. I will try downgrading my .NET Core version and attempt packaging again. Thank you for your support:)

FreeAndNil commented 2 months ago

No problem - as you can see in #142 and #147 you are not the only one with those problems using aot. Feel free to reopen when you get new details.

hhxdestiny commented 3 weeks ago

The problem still exists on 3.0.1

FreeAndNil commented 3 weeks ago

@hhxdestiny nobody said otherwise. In case you have a sample application which reproduces the problem, feel free to reopen (or create a new issue).