log4net breaks when deployed with Publish

0

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

c#
log4net
.net-5
asked on Stack Overflow Jan 14, 2021 by nop • edited Jan 14, 2021 by nop

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0