You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Martin Storø Nyfløtt (Jira)" <ji...@apache.org> on 2020/11/22 21:04:00 UTC
[jira] [Created] (LOG4NET-658) ColoredConsoleAppender throws
exception on .NET Core
Martin Storø Nyfløtt created LOG4NET-658:
--------------------------------------------
Summary: ColoredConsoleAppender throws exception on .NET Core
Key: LOG4NET-658
URL: https://issues.apache.org/jira/browse/LOG4NET-658
Project: Log4net
Issue Type: Bug
Components: Appenders
Affects Versions: 2.0.8
Environment: Windows, .NET Core 3.1
Reporter: Martin Storø Nyfløtt
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:
{noformat}
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.{noformat}
The reason I am seeing this is because [ColoredConsoleAppender will call|https://github.com/apache/logging-log4net/blob/rel/2.0.12/src/log4net/Appender/ColoredConsoleAppender.cs#L462] 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:
{code:java}
static void Main()
{
var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
XmlConfigurator.Configure(new FileInfo(configurationPath));
}
{code}
and the following log4net.config:
{noformat}
<?xml version="1.0" encoding="utf-8"?><log4net>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
</appender> <root>
<appender-ref ref="ColoredConsoleAppender" />
</root>
</log4net>{noformat}
In order to go around this problem, I have added a reference to the Nuget package [System.Text.Encoding.CodePages|https://www.nuget.org/packages/System.Text.Encoding.CodePages/] and registered the code pages before I configure log4net:
{code:java}
static void Main()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var configurationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log4net.config");
XmlConfigurator.Configure(new FileInfo(configurationPath));
}
{code}
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.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)