question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

See original GitHub issue

I’m seeing this error when I try and log to Seq. Any suggestions?

It’s a .NET 4.8 ASP.NET MVC App

Sample code I’m using AutoFac IoC to inject a logger and then triggering it from there:

public class SerilogLogger
    {
        public SerilogLogger()
        {
            Log.Logger = new LoggerConfiguration()
                       .ReadFrom.AppSettings()
                       .CreateLogger();

            Serilog.Debugging.SelfLog.Enable(
                msg => System.Diagnostics.Trace.WriteLine(msg));
        }

        public void Info(object message)
        {
            Log.Logger.Information(message.ToString());
            Log.CloseAndFlush();
        }

// in IoC class
builder.RegisterType<SerilogLogger>().As<ILogger>();

// Test to trigger log message
public class TestClass 
{
    private ILogger logger;
    public TestClass(ILogger logger)
    {
        this.logger = logger;
    }

    public void TestMethod()
    {
        logger.Info("Testing from website");
    }
}

Full stack trace from SelfLog:


2020-06-16T09:35:51.2143660Z Exception while emitting periodic batch from Serilog.Sinks.Seq.SeqSink: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Serilog.Sinks.Seq.SeqSink.<EmitBatchAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.<OnTick>d__16.MoveNext()
2020-06-16T09:35:51.3180195Z Exception while emitting periodic batch from Serilog.Sinks.Seq.SeqSink: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult)
   at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.PooledStream.EndWrite(IAsyncResult asyncResult)
   at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
   at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Serilog.Sinks.Seq.SeqSink.<EmitBatchAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.<OnTick>d__16.MoveNext()

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
grjwcommented, Jun 18, 2020

Hi @nblumhardt

Thanks for the suggestions, I really appreciate it. I have found now found the issue.

Googling the error, a few people mention not using TLS1.2 can trigger this. Someone else also said Seq needs TLS1.2. Looking at Wireshark, my prototype (which works) is using TLS1.2 and the website (which doesn’t) is using TLS1. Both are .NET 4.8

I had upgraded my MVC project to 4.8 for that reason but completely forgot about the httpruntime targetFramework attribute in web.config, It was still set to 4.5. Changing that to 4.8 has fixed it.

Thanks also for the suggestions about using SingleInstance - I’ll update that as well.

0reactions
nblumhardtcommented, Jun 17, 2020

Ah, sorry - also, Autofac will be disposing that InstancePerDependency() logger per request, which will also potentially cause trouble. Definitely SingleInstance() 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

An existing connection was forcibly closed by the remote host
This generally means that the remote side closed the connection (usually by sending a TCP/IP RST packet).
Read more >
How to solve "An existing connection was forcibly closed ...
The error means the service is not running or the client cannot reach the remote system. You'll want to get the IP address...
Read more >
An existing connection was forcibly closed (OS error 10054)
Describes scenarios in which an existing connection was forcibly closed by the remote host and provides resolutions for these scenarios.
Read more >
Fix 'An Existing Connection Was Forcibly Closed by the ...
Fix 'An Existing Connection Was Forcibly Closed by the Remote Host' Error | Minecraft 2023 · Try these fixes · Fix 1: Turn...
Read more >
An existing connection was forcibly closed by remote host
This error is triggered with a socket connection between a client and a server. In this article, we will provide some viable solutions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found