Uploaded image for project: 'Log4net'
  1. Log4net
  2. LOG4NET-658

ColoredConsoleAppender throws exception on .NET Core

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.0.8
    • None
    • Appenders
    • None
    • Windows, .NET Core 3.1

    Description

      Moving from .NET Framework to .NET Core, I discovered that ColoredConsoleAppender starts logging the following error messages to my console if I reference it in a log4net.config:

      log4net:ERROR Could not create Appender [ColoredConsoleAppender] of type [log4net.Appender.ColoredConsoleAppender]. Reported error follows.
      System.NotSupportedException: No data is available for encoding 437. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
       at System.Text.Encoding.GetEncoding(Int32 codepage)
       at log4net.Appender.ColoredConsoleAppender.ActivateOptions()
       at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement appenderElement)
      log4net:ERROR Appender named [ColoredConsoleAppender] not found.

       The reason I am seeing this is because ColoredConsoleAppender will call GetConsoleOutputCP() in Kernel32.dll to get the current encoding being used in the console to configure a stream writer for writing to the console with the appropriate text encoding. In my case, this is 437 (OEM United States). When GetEncoding with this code-page, the exception above gets thrown because this encoding is not included out-of-the-box in .NET Core.

      This can be reproduced by having an application with the following Main:

      static void Main()
      {
          var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
          XmlConfigurator.Configure(new FileInfo(configurationPath));
      }
      

       and the following log4net.config:

      <?xml version="1.0" encoding="utf-8"?><log4net>
        <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
        </appender>  <root>
          <appender-ref ref="ColoredConsoleAppender" />
        </root>
      </log4net>

      In order to go around this problem, I have added a reference to the Nuget package System.Text.Encoding.CodePages and registered the code pages before I configure log4net:

      static void Main()
      {
          Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
          var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
          XmlConfigurator.Configure(new FileInfo(configurationPath));
      }
      

      I think this is also how you would fix this in log4net. This is also a show-stopper from getting this functionality working on Linux (or anything non-Windows), since Kernel32.dll is a Windows-specific library.

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            martinmine Martin Storø Nyfløtt
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: