Skip to content

Making log4net.Appender.UdpAppender work under netcoreapp3.0

Notifications You must be signed in to change notification settings

AleRoe/Log4netUdpAppenderNetCore

Repository files navigation

Log4netUdpAppenderNetCore

Making log4net.Appender.UdpAppender work under netcoreapp3.0

When using log4net.logging.UdpAppender under netcoreapp3.0, logging messages are not properly emitted due to the fact that log4net uses Unicode as the default encoding for netcoreapp3.0.

This results in UDP packets which cannot be properly interpreted:

Wireshark image

A simple workaround is to derive from UpdAppender and conditionally modify the encoding in the constructor and then referencing the new appender in your log4netconfig.

    public class UdpAppenderNetCore : UdpAppender
    {
        public UdpAppenderNetCore() : base()
        {
#if NETCOREAPP3_0
            //By default, UdpAppender uses Unicode encoding when running under netcoreapp,
            //resulting in UDP-packets which cannot be properly interpreted.
            //To resolve, we set the proper encoding (UTF8)
            this.Encoding = Encoding.Default;
#endif
        }
    }

With this change, the logging messages are sent in the proper format and can be interpreted by common logviewers (in my case using Log4View).

Wireshark image

The code includes a sample implementation and console testapp for NET472 and NETCOREAPP3.0.

Hope this helps!

About

Making log4net.Appender.UdpAppender work under netcoreapp3.0

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages