Skip to content

Commit 4f627b7

Browse files
author
Paul D'Ambra
committed
add speculative more detailed logging of socket exceptions
1 parent 8fcb0ce commit 4f627b7

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Net.Sockets;
3+
using FluentAssertions;
4+
using Serilog.Sinks.Network.Sinks.TCP;
5+
using Xunit;
6+
7+
namespace Serilog.Sinks.Network.Test
8+
{
9+
public class LoggingFailureHandlerTest
10+
{
11+
[Fact]
12+
public void canHandleAnExceptionWithNoInnerException()
13+
{
14+
var exception = new Exception("the message");
15+
TcpSocketWriter.UnexpectedErrorLogger(
16+
exception,
17+
(x, socketError) =>
18+
{
19+
x.Should().BeSameAs(exception);
20+
socketError.Should().BeNull();
21+
});
22+
}
23+
24+
[Fact]
25+
public void canHandleAnExceptionWithAnInnerException()
26+
{
27+
var exception = new Exception("the outer", new Exception("the inner"));
28+
TcpSocketWriter.UnexpectedErrorLogger(
29+
exception,
30+
(x, socketError) =>
31+
{
32+
x.Should().BeSameAs(exception);
33+
socketError.Should().BeNull();
34+
});
35+
}
36+
37+
[Fact]
38+
public void canHandleASocketExceptionWithNoInnerException()
39+
{
40+
var exception = new SocketException(997);
41+
TcpSocketWriter.UnexpectedErrorLogger(
42+
exception,
43+
(x, socketError) =>
44+
{
45+
x.Should().BeSameAs(exception);
46+
socketError.Should().Be((SocketError) 997);
47+
});
48+
}
49+
50+
[Fact]
51+
public void canHandleASocketExceptionWithAnInnerException()
52+
{
53+
var exception = new Exception("the outer", new SocketException(10044));
54+
TcpSocketWriter.UnexpectedErrorLogger(
55+
exception,
56+
(x, socketError) =>
57+
{
58+
x.Should().BeSameAs(exception);
59+
socketError.Should().Be((SocketError) 10044);
60+
});
61+
}
62+
}
63+
}

Serilog.Sinks.Network/Sinks/TCP/TCPSocketWriter.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Net.Sockets;
2323
using System.Security.Authentication;
2424
using System.Text;
25+
using System.Text.RegularExpressions;
2526
using System.Threading;
2627
using System.Threading.Tasks;
2728

@@ -58,9 +59,43 @@ public class TcpSocketWriter : IDisposable
5859
/// </summary>
5960
public event Action<Exception> LoggingFailureHandler = ex =>
6061
{
61-
Log.Error(ex, "failure inside TCP socket: {message}", ex.Message);
62+
UnexpectedErrorLogger(
63+
ex,
64+
(x, socketError) =>
65+
{
66+
if (socketError == null)
67+
{
68+
Log.Error(x, "failure inside TCP socket: {message}", x.Message);
69+
}
70+
else
71+
{
72+
Log.Error(
73+
x,
74+
"failure inside TCP socket: {message} - socket error found {socketErrorCode}",
75+
x.Message,
76+
socketError);
77+
}
78+
79+
});
6280
};
6381

82+
public static void UnexpectedErrorLogger(Exception ex, Action<Exception, SocketError?> log)
83+
{
84+
SocketError? socketErrorCode = null;
85+
var current = ex;
86+
do
87+
{
88+
if (current is SocketException)
89+
{
90+
socketErrorCode = ((SocketException) current).SocketErrorCode;
91+
}
92+
93+
current = current.InnerException;
94+
} while (socketErrorCode == null && current != null);
95+
96+
log(ex, socketErrorCode);
97+
}
98+
6499
/// <summary>
65100
/// Construct a TCP _socket writer that writes to the given endPoint and _port.
66101
/// </summary>

rakefile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semver = '2.0.1'
1+
semver = '2.0.2'
22

33
BUILD_CONFIG = ENV['Configuration'] || 'Release'
44
BUILD_NUMBER = ENV['APPVEYOR_BUILD_NUMBER'] || '0'

0 commit comments

Comments
 (0)