File tree Expand file tree Collapse file tree 3 files changed +100
-2
lines changed
Serilog.Sinks.Network.Test
Serilog.Sinks.Network/Sinks/TCP Expand file tree Collapse file tree 3 files changed +100
-2
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 22
22
using System . Net . Sockets ;
23
23
using System . Security . Authentication ;
24
24
using System . Text ;
25
+ using System . Text . RegularExpressions ;
25
26
using System . Threading ;
26
27
using System . Threading . Tasks ;
27
28
@@ -58,9 +59,43 @@ public class TcpSocketWriter : IDisposable
58
59
/// </summary>
59
60
public event Action < Exception > LoggingFailureHandler = ex =>
60
61
{
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
+ } ) ;
62
80
} ;
63
81
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
+
64
99
/// <summary>
65
100
/// Construct a TCP _socket writer that writes to the given endPoint and _port.
66
101
/// </summary>
Original file line number Diff line number Diff line change 1
- semver = '2.0.1 '
1
+ semver = '2.0.2 '
2
2
3
3
BUILD_CONFIG = ENV [ 'Configuration' ] || 'Release'
4
4
BUILD_NUMBER = ENV [ 'APPVEYOR_BUILD_NUMBER' ] || '0'
You can’t perform that action at this time.
0 commit comments