Skip to content

Commit f98a383

Browse files
authored
Merge pull request #59 from tbdrake/fix-ambiguous-overloads
Adjust overloads and parameter order to resolve potential ambiguous method call errors
2 parents 947f03c + be5b72c commit f98a383

File tree

4 files changed

+39
-41
lines changed

4 files changed

+39
-41
lines changed

PingPonger/Program.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ public static int Main(string[] args)
3939
try
4040
{
4141
var options = new Options();
42-
if (Parser.Default.ParseArguments<Options>(args).Tag == ParserResultType.NotParsed)
42+
var parseResult = Parser.Default.ParseArguments<Options>(args)
43+
.WithNotParsed(errs =>
44+
{
45+
Console.WriteLine("Failed parsing command line arguments:");
46+
errs.ToList().ForEach(e => Console.WriteLine(@"error: {0}", e));
47+
args.ToList().ForEach(a => Console.WriteLine(@"arg: {0}", a));
48+
})
49+
.WithParsed(opts => options = opts);
50+
51+
if (parseResult.Tag == ParserResultType.NotParsed)
4352
{
44-
Console.WriteLine(@"Failed parsing command line arguments");
45-
args.ToList().ForEach(a => Console.WriteLine(@"arg: {0}", a));
4653
return 1;
4754
}
4855

@@ -64,11 +71,19 @@ public static int Main(string[] args)
6471
{
6572
if (options.UDP)
6673
{
67-
logConfig.WriteTo.UDPSink(options.Url, options.Port, new LogstashJsonFormatter());
74+
logConfig.WriteTo.UDPSink(options.Url, options.Port);
6875
}
6976
if (options.TCP)
7077
{
71-
logConfig.WriteTo.TCPSink(options.Url, options.Port, null, null, new LogstashJsonFormatter());
78+
if (options.Port != 0)
79+
{
80+
logConfig.WriteTo.TCPSink(options.Url, options.Port);
81+
}
82+
else
83+
{
84+
// URL option value may already have a port in it, e.g. "tcp://localhost:8888"
85+
logConfig.WriteTo.TCPSink(options.Url);
86+
}
7287
}
7388
}
7489
else if (options.IP?.Length > 0)
@@ -81,11 +96,11 @@ public static int Main(string[] args)
8196

8297
if (options.UDP)
8398
{
84-
logConfig.WriteTo.UDPSink(ipAddress, options.Port, new LogstashJsonFormatter());
99+
logConfig.WriteTo.UDPSink(ipAddress, options.Port);
85100
}
86101
if (options.TCP)
87102
{
88-
logConfig.WriteTo.TCPSink(ipAddress, options.Port,null,null, new LogstashJsonFormatter());
103+
logConfig.WriteTo.TCPSink(ipAddress, options.Port);
89104
}
90105
}
91106
else

Serilog.Sinks.Network.Test/JsonFormatter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ public class JsonFormatter
1717
{
1818
private static LoggerAndSocket ConfigureTestLogger(
1919
ITextFormatter? formatter = null,
20-
ILogEventEnricher[] enrichers = null
20+
ILogEventEnricher[]? enrichers = null
2121
)
2222
{
2323
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2424
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
2525
socket.Listen();
2626

2727
var loggerConfiguration = new LoggerConfiguration()
28-
.WriteTo.TCPSink(IPAddress.Loopback, ((IPEndPoint)socket.LocalEndPoint!).Port, null, null, formatter);
28+
.WriteTo.TCPSink(IPAddress.Loopback, ((IPEndPoint)socket.LocalEndPoint!).Port, formatter);
2929
if (enrichers != null)
3030
{
3131
loggerConfiguration.Enrich.With(enrichers);
@@ -79,7 +79,7 @@ public async Task IncludesCurrentActivityTraceAndSpanIds()
7979

8080
var receivedData = await ServerPoller.PollForReceivedData(fixture.Socket);
8181

82-
receivedData.Should().Contain($"\"traceId\":\"{activity.TraceId}\"");
82+
receivedData.Should().Contain($"\"traceId\":\"{activity!.TraceId}\"");
8383
receivedData.Should().Contain($"\"spanId\":\"{activity.SpanId}\"");
8484
}
8585

@@ -119,7 +119,7 @@ public async Task WritesTraceAndSpanIdsBeforeDuplicatePropertiesFromEnrichers()
119119

120120
var receivedData = await ServerPoller.PollForReceivedData(fixture.Socket);
121121

122-
var indexOfTraceId = receivedData.IndexOf($"\"traceId\":\"{activity.TraceId}\"");
122+
var indexOfTraceId = receivedData.IndexOf($"\"traceId\":\"{activity!.TraceId}\"");
123123
var indexOfTraceIdFromEnricher = receivedData.IndexOf("\"traceId\":\"traceId-from-enricher\"");
124124
indexOfTraceId.Should().BePositive().And.BeLessThan(indexOfTraceIdFromEnricher);
125125

@@ -139,4 +139,4 @@ private static ActivityListener CreateAndAddActivityListener(string sourceName)
139139
return activityListener;
140140
}
141141
}
142-
}
142+
}

Serilog.Sinks.Network.Test/WhenLoggingViaTcp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static LoggerAndSocket ConfigureTestLogger(ITextFormatter? formatter = n
2222
socket.Listen();
2323

2424
var logger = new LoggerConfiguration()
25-
.WriteTo.TCPSink(IPAddress.Loopback, ((IPEndPoint)socket.LocalEndPoint!).Port, null, null, formatter)
25+
.WriteTo.TCPSink(IPAddress.Loopback, ((IPEndPoint)socket.LocalEndPoint!).Port, formatter)
2626
.CreateLogger();
2727

2828
return new LoggerAndSocket { Logger = logger, Socket = socket };

Serilog.Sinks.Network/NetworkLoggerConfigurationExtensions.cs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,33 @@ public static LoggerConfiguration TCPSink(
4141
this LoggerSinkConfiguration loggerConfiguration,
4242
IPAddress ipAddress,
4343
int port,
44-
int? writeTimeoutMs = null,
45-
int? disposeTimeoutMs = null,
4644
ITextFormatter? textFormatter = null,
47-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
48-
{
49-
return TCPSink(loggerConfiguration, $"tcp://{ipAddress}:{port}", writeTimeoutMs, disposeTimeoutMs, textFormatter, restrictedToMinimumLevel);
50-
}
51-
52-
public static LoggerConfiguration TCPSink(
53-
this LoggerSinkConfiguration loggerConfiguration,
54-
IPAddress ipAddress,
55-
int port,
56-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
45+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
46+
int? writeTimeoutMs = null,
47+
int? disposeTimeoutMs = null)
5748
{
58-
return TCPSink(loggerConfiguration, ipAddress, port,null, null, null, restrictedToMinimumLevel);
49+
return TCPSink(loggerConfiguration, $"tcp://{ipAddress}:{port}", textFormatter, restrictedToMinimumLevel, writeTimeoutMs, disposeTimeoutMs);
5950
}
6051

6152
public static LoggerConfiguration TCPSink(
6253
this LoggerSinkConfiguration loggerConfiguration,
6354
string host,
6455
int port,
65-
int? writeTimeoutMs = null,
66-
int? disposeTimeoutMs = null,
6756
ITextFormatter? textFormatter = null,
68-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
57+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
58+
int? writeTimeoutMs = null,
59+
int? disposeTimeoutMs = null)
6960
{
70-
return TCPSink(loggerConfiguration, $"{host}:{port}", writeTimeoutMs, disposeTimeoutMs, textFormatter, restrictedToMinimumLevel);
61+
return TCPSink(loggerConfiguration, $"{host}:{port}", textFormatter, restrictedToMinimumLevel, writeTimeoutMs, disposeTimeoutMs);
7162
}
7263

7364
public static LoggerConfiguration TCPSink(
7465
this LoggerSinkConfiguration loggerConfiguration,
7566
string uri,
76-
int? writeTimeoutMs = null,
77-
int? disposeTimeoutMs = null,
7867
ITextFormatter? textFormatter = null,
79-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
68+
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
69+
int? writeTimeoutMs = null,
70+
int? disposeTimeoutMs = null)
8071
{
8172
var socketWriter = new TcpSocketWriter(
8273
BuildUri(uri),
@@ -86,14 +77,6 @@ public static LoggerConfiguration TCPSink(
8677
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
8778
}
8879

89-
public static LoggerConfiguration TCPSink(
90-
this LoggerSinkConfiguration loggerConfiguration,
91-
string uri,
92-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
93-
{
94-
return TCPSink(loggerConfiguration, uri, null, null, null, restrictedToMinimumLevel);
95-
}
96-
9780
private static IPAddress ResolveAddress(string uri)
9881
{
9982
// Check if it is IP address

0 commit comments

Comments
 (0)