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

nop
  • 4,711
  • 6
  • 32
  • 93
  • What does the file TestProject.exe.config in the published location look like? – Russ Jan 14 '21 at 13:17
  • Maybe publish that `.config` file too then? – l33t Jan 14 '21 at 13:18
  • @Russ, added that to the question. – nop Jan 14 '21 at 13:26
  • @l337, it is right there. – nop Jan 14 '21 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 '21 at 14:12
  • How long is the full path name including folder(s)? does code work if file is on c:\ drive? – jdweng Jan 14 '21 at 14:13
  • @jdweng, I specified an absolute path and it still didn't work. It's something to do with the file settings/config specifically? Because their docs were showing to add it to `App.config`, but Console Apps kinda doesn't use that one. – nop Jan 14 '21 at 14:15
  • @PanagiotisKanavos, I know. I think that's the problem. I used https://jakubwajs.wordpress.com/2019/11/28/logging-with-log4net-in-net-core-3-0-console-app/ – nop Jan 14 '21 at 14:26
  • I know python automatically converts paths to full paths even when a relative reference is made. I was on a corporate network with python and the path include the company namespace. Could not execute code on a network drive and had to use the c:\ drive. – jdweng Jan 14 '21 at 14:45
  • @jdweng, I have already tried it `C:\log4net.config`. Same thing happens. – nop Jan 14 '21 at 15:05
  • The config file uses : "logs/%property{LogName}.log" – jdweng Jan 14 '21 at 15:08
  • Your linked wordpress example uses `var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());` is there a specific reason why you deviate from the example in this regard? I don't know whether it makes any difference or not... – grek40 Jan 14 '21 at 15:13
  • The issue is caused by "Produce single file" option under the Publish. – nop Jan 14 '21 at 15:16
  • This seems to be related: https://stackoverflow.com/questions/64929114/deploy-as-single-file-in-net5-with-log4net-throws-exception-for-config-file. They have to update log4net for .NET 5, otherwise it won't work with "Produce in a single file". I tried `True` and no error message, but no log4net either :D. – nop Jan 14 '21 at 15:19

0 Answers0