In a plugin I start a Task using Task.Factory.StartNew. The task does some processing and if it fails, it attempts to log the error using an ILogger (DefaultLogger in this case).
Internally it then calls _webHelper.GetCurrentIpAddress, which fails with a NullReferenceException - even though there's thorough null checking going on:
public virtual string GetCurrentIpAddress()
{
if (_httpContext != null &&
_httpContext.Request != null &&
_httpContext.Request.UserHostAddress != null) // <--- FAILS HERE
return _httpContext.Request.UserHostAddress;
return string.Empty;
}
Apparently UserHostAddress throws the NullReferenceException internally, so the explicit null check doesn't help. This could be considered an ASP.NET MVC bug, and a quick-and-dirty fix would be to simply catch and ignore the NullReferenceException.
However, I've posted this under bug reports since the logger should log the error even though an IP can't be retrieved.
Is there a more elegant way to handle this than a try-catch-ignore?