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.
- log4net 22.214.171.124 – 260 KB Zip
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.