NLog: NetworkTarget with TCP KeepConnection=true should retry on error

When using the NetworkTarget with the KeepConnection=true, the connection to the server is kept open so that it can be reused. Using the TCP protocol, if the server is configured to close the connection after a period of inactivity (a common configuration to avoid DOS), we then have a NetworkSender holding a closed Socket. The problem is that it seems that the next log message we try to send will not be received by the server (executing https://github.com/NLog/NLog/blob/master/src/NLog/Targets/NetworkTarget.cs#L291).

One workaround would be to set KeepConnection=false but in case of a TCP connection this can be expensive when the application starts to write more logs. Another solution would be to use RetryingTargetWrapper, but it seems a bit odd to configure a retry for that particular case since no error really occurred.

So do you think it is possible to check the status of the socket before using it to send the message and try to open a new one in case it was cleanly closed by the server ?

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

@snakefoot Sorry for the delay, got quite busy the past few days … 😦 I’ve managed to scramble something together, I haven’t tested it out but I think it’s a start: https://github.com/NLog/NLog/commit/39d39cff047e83a3e18be823804b0d8e530a3c03

@martinmine Been able to decipher the NLog-code for NetworkTarget? Or maybe it is easier for you to present your idea as sample-code here in this issue?

@snakefoot Actually, neither. I don’t want to mess with any socket parameters since I want to get a general solution to this problem. Different systems (routers/endpoints/whatever) may act differently with the keep-alive flag for example, and the root cause of this problem is lack of error-handling which is what I want to improve. Give me a few hours and I might have something going on a proof-of-concept branch 😃