log4net UdpAppender with IPv6 on Windows Vista and 7

log4net is a great logging framework for .NET, but it hasn’t been updated in years. When we moved to Windows Vista, we noticed that the UdpAppender stopped working with Chainsaw and with our internal logging tools when logging to localhost. After some Googling, we discovered that replacing localhost with 127.0.0.2 got everything working and we promptly forgot about it.

Earlier this week, we moved one of our projects over to .NET 4.0 and once again logging failed. In the console window while debugging the app, I noticed the following,

log4net.Util.TypeConverters.ConversionNotSupportedException: Cannot convert from type [System.String]
  value [127.0.0.2] to type [System.Net.IPAddress] --->
  System.Net.Sockets.SocketException: No such host is known
       at System.Net.Dns.InternalGetHostByAddress(IPAddress address, Boolean includeIPv6)
       at System.Net.Dns.GetHostEntry(String hostNameOrAddress)
       at log4net.Util.TypeConverters.IPAddressConverter.ConvertFrom(Object source)

So, I tried changing back to localhost and the error changed to,

log4net:ERROR [UdpAppender] Unable to send logging event to remote host ::1 on port 7071.
      System.Net.Sockets.SocketException (0x80004005): An address incompatible with
      the requested protocol was used
       at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags
         socketFlags, EndPoint remoteEP)
       at System.Net.Sockets.UdpClient.Send(Byte[] dgram, Int32 bytes, IPEndPoint endPoint)
       at log4net.Appender.UdpAppender.Append(LoggingEvent loggingEvent)

At this point, I have some clues, specifically that localhost was being resolved as the IPv6 address ::1, not the IPv4 address 127.0.0.1. Looking through the reported issues I found that this problem had been reported and fixed in 2007. Unfortunately, that code isn’t in the release, so I downloaded the latest source and recompiled log4net. Of course, they don’t release the signing key, so I generated my own and signed the assembly myself.

This fixed my problems with the UdpAppender, although, if you use localhost on a Windows Vista or Windows 7 machine, it will resolve to the IPv6 address, so make sure that your receiver is listening on the IPv6 address. For example, in log2console, under Receivers, set Use IPv6 Addresses to true for the UDP receiver. If your receiver does not support IPv6, use 127.0.0.2 for the address.

To save other people the hassle of recompiling log4net themselves to get the latest fixes, I have uploaded a release version of the assembly. Download it here.

We are using this version of the assembly in our own projects, but I make no guarantees as to its stability.

Update: Just to  be clear, this is not an official log4net release and it is only compiled against .NET 2.0. I have made no code changes, it is just the code that is currently in the repository. This is only intended to save you having to download and compile if you run into the same problems I did. It is also not extensively tested. We are using it and File, Rolling File, Event Log and UDP Appenders seem to be working fine.

Update 2: Since I have released this, I have found several bugs in the current source. For example, setting Append=true to a File or Rolling File causes every log message to truncate the file! Because of that I would recommend that you don’t download this unless you really need it. I have finally given up on log4net and converted all of my projects over to NLog.

  • upgrading to log4net 1.2.11 appears to resolve all issues related to the Udp appender problems in 1.2.10. A couple of bug fixes in that release related to the IP address conversion and specific support added for IPv6 took care of it. See the release notes at http://logging.apache.org/log4net/release/release-notes.html

  • Pingback: log4net UdpAppender with IPv6 on Windows Vista and 7 | EntLib.net 技术分享平台()

  • Pingback: Create a .NET Windows Service in 5 steps with Topshelf » Christoph De Baene()

  • I had the same problem.
    Here’s a quick solution for those of you who still want to use log4net 😉
    Edit your Windows hosts file, and change the line:
    # 127.0.0.1 localhost
    to:
    127.0.0.1 localhost
    You’ll need a simple text editor, but I guess you should run it as an administrator, otherwise your hosts file won’t be saved.
    Worked for me.
    Cheers!

  • Shlomi

    Hi!

    Though your already moved to NLog – maybe for someone else who read this post.

    We found a simple solution for this issue:
    instead of using the UdpAppender on RemteAddress ‘127.0.0.2’:

    for local machine:
    use address such as ‘log4view-local’ and add on your hosts file (C:\Windows\System32\drivers\etc\hosts)
    127.0.0.1 log4view-local

    don’t forget to flush dns with ipconfig /flushdns, and ping log4view-local to make sure.

    for other environments such as QA/Production – just set in your DNS – for example: log4view.qa.yourdomain.com – the related log4view machine’s IP, and set in the UdpAppender RemoteAddress value ‘log4view.qa.yourdomain.com’.

    worked great for us!

    cheers,
    Shlomi

  • Just found this – many thanks for putting the revised build up there!