Skip to content

Commit 8703d1a

Browse files
dnatovdnatovFantasticFiasco
authored
feat: create configuration option for broadcasting (#175)
Co-authored-by: dnatov <dnatov@gsdx.us> Co-authored-by: FantasticFiasco <mattias@kindb.org>
1 parent 0d66878 commit 8703d1a

File tree

6 files changed

+26
-12
lines changed

6 files changed

+26
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and is followi
66

77
## Unreleased
88

9+
- [BREAKING CHANGE] Support for broadcast packages (contributed by [@dnatov](https://github.com/dnatov))
10+
911
### :zap: Added
1012

1113
- [BREAKING CHANGE] Support for specifying UDP datagram encoding (contributed by [@pedoc](https://github.com/pedoc))

src/Serilog.Sinks.Udp/LoggerSinkConfigurationExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public static class LoggerSinkConfigurationExtensions
5555
/// The TCP port from which the UDP client will communicate. The default is 0 and will
5656
/// cause the UDP client not to bind to a local port.
5757
/// </param>
58+
/// <param name="enableBroadcast">
59+
/// Whether the <see cref="UdpClient"/> may send broadcast packets.
60+
/// </param>
5861
/// <param name="restrictedToMinimumLevel">
5962
/// The minimum level for events passed through the sink. The default is
6063
/// <see cref="LevelAlias.Minimum"/>.
@@ -81,6 +84,7 @@ public static LoggerConfiguration Udp(
8184
int remotePort,
8285
AddressFamily family,
8386
int localPort = 0,
87+
bool enableBroadcast = false,
8488
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
8589
LoggingLevelSwitch? levelSwitch = null,
8690
string outputTemplate = DefaultOutputTemplate,
@@ -99,6 +103,7 @@ public static LoggerConfiguration Udp(
99103
family,
100104
formatter,
101105
localPort,
106+
enableBroadcast,
102107
restrictedToMinimumLevel,
103108
levelSwitch,
104109
encoding);
@@ -131,6 +136,9 @@ public static LoggerConfiguration Udp(
131136
/// The TCP port from which the UDP client will communicate. The default is 0 and will
132137
/// cause the UDP client not to bind to a local port.
133138
/// </param>
139+
/// <param name="enableBroadcast">
140+
/// Whether the <see cref="UdpClient"/> may send broadcast packets.
141+
/// </param>
134142
/// <param name="restrictedToMinimumLevel">
135143
/// The minimum level for events passed through the sink. The default is
136144
/// <see cref="LevelAlias.Minimum"/>.
@@ -151,6 +159,7 @@ public static LoggerConfiguration Udp(
151159
AddressFamily family,
152160
ITextFormatter formatter,
153161
int localPort = 0,
162+
bool enableBroadcast = false,
154163
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
155164
LoggingLevelSwitch? levelSwitch = null,
156165
Encoding? encoding = null)
@@ -161,7 +170,7 @@ public static LoggerConfiguration Udp(
161170

162171
try
163172
{
164-
var client = UdpClientFactory.Create(localPort, family);
173+
var client = UdpClientFactory.Create(localPort, family, enableBroadcast);
165174
var sink = new BatchingSink(new UdpSink(client, remoteAddress, remotePort, formatter, encoding));
166175

167176
return sinkConfiguration.Sink(sink, restrictedToMinimumLevel, levelSwitch);

src/Serilog.Sinks.Udp/Sinks/Udp/Private/UdpClientFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public static class UdpClientFactory
2525
/// <summary>
2626
/// Gets or sets the factory creating instances of <see cref="IUdpClient"/>.
2727
/// </summary>
28-
public static Func<int, AddressFamily, IUdpClient> Create { get; set; }
29-
= (localPort, family) => new UdpClientWrapper(localPort, family);
30-
}
28+
public static Func<int, AddressFamily, bool, IUdpClient> Create { get; set; }
29+
= (localPort, family, enableBroadcast) => new UdpClientWrapper(localPort, family, enableBroadcast);
30+
}

src/Serilog.Sinks.Udp/Sinks/Udp/Private/UdpClientWrapper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal class UdpClientWrapper : IUdpClient
2323
{
2424
private readonly UdpClient client;
2525

26-
public UdpClientWrapper(int localPort, AddressFamily family)
26+
public UdpClientWrapper(int localPort, AddressFamily family, bool enableBroadcast)
2727
{
2828
if (localPort < IPEndPoint.MinPort || localPort > IPEndPoint.MaxPort) throw new ArgumentOutOfRangeException(nameof(localPort));
2929

@@ -36,6 +36,9 @@ public UdpClientWrapper(int localPort, AddressFamily family)
3636
{
3737
client.Client.DualMode = true;
3838
}
39+
40+
// Enable broadcasting
41+
client.EnableBroadcast = enableBroadcast;
3942
}
4043

4144
public Socket Client => client.Client;
@@ -61,4 +64,4 @@ public void Dispose()
6164
client.Dispose();
6265
}
6366
#endif
64-
}
67+
}

test/Serilog.Sinks.Udp.Tests/SinkFixture.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ namespace Serilog;
1111

1212
public abstract class SinkFixture : IDisposable
1313
{
14-
private readonly Func<int, AddressFamily, IUdpClient> originalFactory;
14+
private readonly Func<int, AddressFamily, bool, IUdpClient> originalFactory;
1515
private readonly UdpClientMock client;
1616

1717
protected SinkFixture()
1818
{
1919
originalFactory = UdpClientFactory.Create;
2020

2121
client = new UdpClientMock();
22-
UdpClientFactory.Create = (_, __) => client.Object;
22+
UdpClientFactory.Create = (_, __, ___) => client.Object;
2323
}
2424

2525
protected abstract string RemoteAddress { get; }
@@ -85,4 +85,4 @@ public void Dispose()
8585

8686
UdpClientFactory.Create = originalFactory;
8787
}
88-
}
88+
}

test/Serilog.Sinks.Udp.Tests/Sinks/Udp/Private/UdpClientFactoryShould.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class UdpClientFactoryShould : IDisposable
1919
public void UseDualModeOnInterNetworkV6()
2020
{
2121
// Act
22-
client = UdpClientFactory.Create(0, AddressFamily.InterNetworkV6);
22+
client = UdpClientFactory.Create(0, AddressFamily.InterNetworkV6, false);
2323

2424
// Assert
2525
client.Client.DualMode.ShouldBeTrue();
@@ -31,7 +31,7 @@ public void UseDualModeOnInterNetworkV6()
3131
public async void SendPayload(string address, AddressFamily family)
3232
{
3333
// Arrange
34-
client = UdpClientFactory.Create(0, family);
34+
client = UdpClientFactory.Create(0, family, false);
3535

3636
var ipAddress = IPAddress.Parse(address);
3737

@@ -55,4 +55,4 @@ public void Dispose()
5555
{
5656
client.Dispose();
5757
}
58-
}
58+
}

0 commit comments

Comments
 (0)