1

When I publish my project (.NET 5 Console Application) and open it from bin\Release\net5.0\win-x64\publish\TestProject.exe, log4net breaks with the following error message. It works when I use the build from Debug or Release, but not publish.

log4net:ERROR Exception while reading ConfigurationSettings. Check your .config file is well formed XML. System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.IO.FileNotFoundException: The system cannot find the file specified. (0x80070002) at System.Reflection.RuntimeModule.GetFullyQualifiedName() at System.Reflection.RuntimeModule.get_Name() 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)

log4net.config

<log4net>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="ColoredConsoleAppender" />
    <appender-ref ref="file" />
  </root>
  <appender name="ColoredConsoleAppender" type="log4net.Appender.ManagedColoredConsoleAppender">
    <mapping>
      <level value="FATAL" />
      <foreColor value="Red" />
      <backColor value="White" />
    </mapping>
    <mapping>
      <level value="ERROR" />
      <foreColor value="Red" />
    </mapping>
    <mapping>
      <level value="WARN" />
      <foreColor value="Yellow" />
    </mapping>
    <mapping>
      <level value="INFO" />
      <foreColor value="White" />
    </mapping>
    <mapping>
      <level value="DEBUG" />
      <foreColor value="Green" />
    </mapping>
    <layout type="log4net.Layout.PatternLayout">
      <conversionpattern value="%d{yyyy-MM-dd HH:mm:ss} %-5level %message%newline" />
    </layout>
    <threshold value="DEBUG" />
  </appender>
  <appender name="file" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="logs/%property{LogName}.log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="-yyyy-MM-dd" />
    <preserveLogFileNameExtension value="true" />
    <staticLogFileName value="false" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %message%newline" />
    </layout>
    <threshold value="DEBUG" />
  </appender>
</log4net>

Code

namespace TestProject
{
    public class Test
    {
        private static readonly ILog _logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public void Run()
        {
            _logger.Debug("test");
        }
    }

    class Program
    {
        static async Task Main(string[] args)
        {
            IConfiguration configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .Build();

            GlobalContext.Properties["LogName"] = "Test Project";
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));

            Test test = new Test();
            test.Run();
            
            Console.ReadLine();
        }
    }
}

Publish settings

enter image description here

Publish location

enter image description here

14
  • What does the file TestProject.exe.config in the published location look like?
    – Russ
    Jan 14, 2021 at 13:17
  • Maybe publish that .config file too then?
    – l33t
    Jan 14, 2021 at 13:18
  • @Russ, added that to the question.
    – nop
    Jan 14, 2021 at 13:26
  • @l337, it is right there.
    – nop
    Jan 14, 2021 at 13:26
  • Does the exception occur at the log4net.Config.XmlConfigurator.Configure(...) line? I ask because your stack trace ends at log4net.Util.SystemInfo.GetAppSetting(String key).
    – grek40
    Jan 14, 2021 at 14:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.