Skip to content

Commit 78d63e4

Browse files
committed
Compatibility with .net standard
Use Serilog.Sinks.Splunk 3.3.0 Remove obsolete methods and constructors Dispose pattern
1 parent 6d75f18 commit 78d63e4

File tree

4 files changed

+100
-143
lines changed

4 files changed

+100
-143
lines changed

src/Serilog.Sinks.UDP/Serilog.Sinks.Splunk.UDP.csproj

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<Description>The Splunk UDP Sink for Serilog</Description>
5-
<VersionPrefix>1.2.0</VersionPrefix>
5+
<VersionPrefix>1.3.0</VersionPrefix>
66
<Authors>Matthew Erbs, Serilog Contributors</Authors>
7-
<TargetFrameworks>net45</TargetFrameworks>
7+
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<AssemblyName>Serilog.Sinks.Splunk.UDP</AssemblyName>
1010
<PackageId>Serilog.Sinks.Splunk.UDP</PackageId>
@@ -18,15 +18,23 @@
1818
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1919
<SignAssembly>true</SignAssembly>
2020
</PropertyGroup>
21-
22-
<ItemGroup>
23-
<PackageReference Include="Serilog.Sinks.Splunk" Version="2.5.0" />
24-
<PackageReference Include="Splunk.Logging.Common" Version="1.6.0" />
25-
<Reference Include="System.Net.Http" />
21+
22+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
23+
<!-- Don't reference unused System assemblies -->
24+
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
25+
</PropertyGroup>
26+
27+
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
2628
<Reference Include="System" />
27-
<Reference Include="Microsoft.CSharp" />
28-
<PackageReference Include="Serilog" Version="2.6.0" />
29-
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
29+
<Reference Include="System.Core" />
30+
<Reference Include="System.Net.Http" />
31+
</ItemGroup>
32+
33+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
34+
<PackageReference Include="System.Net.Http" Version="4.3.0" />
35+
</ItemGroup>
36+
37+
<ItemGroup>
38+
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.3.0" />
3039
</ItemGroup>
31-
3240
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2016 Serilog Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System.Net;
16+
17+
namespace Serilog.Sinks.Splunk
18+
{
19+
/// <summary>
20+
/// Defines connection info used to connect against Splunk
21+
/// using UDP.
22+
/// </summary>
23+
public class SplunkUdpSinkConnectionInfo
24+
{
25+
/// <summary>
26+
/// Splunk host.
27+
/// </summary>
28+
public IPAddress Host { get; }
29+
30+
/// <summary>
31+
/// Splunk port.
32+
/// </summary>
33+
public int Port { get; }
34+
35+
/// <summary>
36+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
37+
/// for defining connection info for connecting using UDP against Splunk.
38+
/// </summary>
39+
/// <param name="host">Splunk host.</param>
40+
/// <param name="port">Splunk UDP port.</param>
41+
public SplunkUdpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
42+
43+
/// <summary>
44+
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
45+
/// for defining connection info for connecting using UDP against Splunk.
46+
/// </summary>
47+
/// <param name="host">Splunk host.</param>
48+
/// <param name="port">Splunk UDP port.</param>
49+
public SplunkUdpSinkConnectionInfo(IPAddress host, int port)
50+
{
51+
Host = host;
52+
Port = port;
53+
}
54+
}
55+
}
56+
Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
#if UDP
2-
3-
4-
// Copyright 2016 Serilog Contributors
1+
// Copyright 2016 Serilog Contributors
52
//
63
// Licensed under the Apache License, Version 2.0 (the "License");
74
// you may not use this file except in compliance with the License.
@@ -15,24 +12,24 @@
1512
// See the License for the specific language governing permissions and
1613
// limitations under the License.
1714

15+
using Serilog.Core;
16+
using Serilog.Events;
17+
using Serilog.Formatting;
1818
using System;
1919
using System.IO;
20-
using System.Net;
2120
using System.Net.Sockets;
2221
using System.Text;
23-
using Serilog.Core;
24-
using Serilog.Events;
25-
using Serilog.Formatting;
2622

2723
namespace Serilog.Sinks.Splunk
2824
{
2925
/// <summary>
3026
/// A sink that logs to Splunk over UDP
31-
/// </summary>
27+
/// </summary>r
3228
public class UdpSink : ILogEventSink, IDisposable
3329
{
34-
Socket _socket;
35-
readonly ITextFormatter _formatter;
30+
private Socket _socket;
31+
private readonly ITextFormatter _formatter;
32+
private bool disposedValue = false;
3633

3734
/// <summary>
3835
/// Creates an instance of the Splunk UDP Sink.
@@ -68,32 +65,6 @@ private void Connect(SplunkUdpSinkConnectionInfo connectionInfo)
6865
_socket.Connect(connectionInfo.Host, connectionInfo.Port);
6966
}
7067

71-
/// <summary>
72-
/// Creates an instance of the Splunk UDP Sink
73-
/// </summary>
74-
/// <param name="host">The Splunk Host</param>
75-
/// <param name="port">The UDP port configured in Splunk</param>
76-
/// <param name="formatProvider">Optional format provider</param>
77-
/// <param name="renderTemplate">If true, the message template is rendered</param>
78-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
79-
public UdpSink(string host, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
80-
: this(new SplunkUdpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
81-
{
82-
}
83-
84-
/// <summary>
85-
/// Creates an instance of the Splunk UDP Sink
86-
/// </summary>
87-
/// <param name="hostAddress">The Splunk Host</param>
88-
/// <param name="port">The UDP port configured in Splunk</param>
89-
/// <param name="formatProvider">Optional format provider</param>
90-
/// <param name="renderTemplate">If true, the message template will be rendered</param>
91-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
92-
public UdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
93-
: this(new SplunkUdpSinkConnectionInfo(hostAddress, port), formatProvider, renderTemplate)
94-
{
95-
}
96-
9768
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
9869
{
9970
return new SplunkJsonFormatter(renderTemplate, formatProvider);
@@ -111,50 +82,26 @@ public void Emit(LogEvent logEvent)
11182
}
11283

11384
/// <inheritdoc/>
114-
public void Dispose()
85+
protected virtual void Dispose(bool disposing)
11586
{
116-
_socket?.Close();
117-
_socket?.Dispose();
118-
_socket = null;
87+
if (!disposedValue)
88+
{
89+
if (disposing)
90+
{
91+
_socket?.Close();
92+
_socket?.Dispose();
93+
_socket = null;
94+
}
95+
96+
disposedValue = true;
97+
}
11998
}
120-
}
121-
122-
/// <summary>
123-
/// Defines connection info used to connect against Splunk
124-
/// using UDP.
125-
/// </summary>
126-
public class SplunkUdpSinkConnectionInfo
127-
{
128-
/// <summary>
129-
/// Splunk host.
130-
/// </summary>
131-
public IPAddress Host { get; }
13299

133-
/// <summary>
134-
/// Splunk port.
135-
/// </summary>
136-
public int Port { get; }
137-
138-
/// <summary>
139-
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
140-
/// for defining connection info for connecting using UDP against Splunk.
141-
/// </summary>
142-
/// <param name="host">Splunk host.</param>
143-
/// <param name="port">Splunk UDP port.</param>
144-
public SplunkUdpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
145-
146-
/// <summary>
147-
/// Creates an instance of <see cref="SplunkUdpSinkConnectionInfo"/> used
148-
/// for defining connection info for connecting using UDP against Splunk.
149-
/// </summary>
150-
/// <param name="host">Splunk host.</param>
151-
/// <param name="port">Splunk UDP port.</param>
152-
public SplunkUdpSinkConnectionInfo(IPAddress host, int port)
100+
/// <inheritdoc/>
101+
public void Dispose()
153102
{
154-
Host = host;
155-
Port = port;
103+
Dispose(true);
156104
}
157105
}
158106
}
159107

160-
#endif

src/Serilog.Sinks.UDP/SplunkLoggingConfigurationExtensions.cs

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
using System;
16-
using System.Net;
1715
using Serilog.Configuration;
1816
using Serilog.Events;
1917
using Serilog.Formatting;
20-
using Serilog.Formatting.Display;
2118
using Serilog.Sinks.Splunk;
19+
using System;
2220

2321
namespace Serilog
2422
{
@@ -63,60 +61,8 @@ public static LoggerConfiguration SplunkViaUdp(
6361
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum)
6462
{
6563
var sink = new UdpSink(connectionInfo, formatter);
66-
67-
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
68-
}
69-
70-
/// <summary>
71-
/// Adds a sink that writes log events as to a Splunk instance via UDP.
72-
/// </summary>
73-
/// <param name="loggerConfiguration">The logger config</param>
74-
/// <param name="host">The Splunk host that is configured for UDP logging</param>
75-
/// <param name="port">The UDP port</param>
76-
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
77-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
78-
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
79-
/// <returns></returns>
80-
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
81-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
82-
public static LoggerConfiguration SplunkViaUdp(
83-
this LoggerSinkConfiguration loggerConfiguration,
84-
string host,
85-
int port,
86-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
87-
IFormatProvider formatProvider = null,
88-
bool renderTemplate = true)
89-
{
90-
var sink = new UdpSink(host, port, formatProvider, renderTemplate);
91-
92-
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
93-
}
94-
95-
96-
/// <summary>
97-
/// Adds a sink that writes log events as to a Splunk instance via UDP.
98-
/// </summary>
99-
/// <param name="loggerConfiguration">The logger config</param>
100-
/// <param name="hostAddresss">The Splunk host that is configured for UDP logging</param>
101-
/// <param name="port">The UDP port</param>
102-
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
103-
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
104-
/// <param name="renderTemplate">If ture, the message template is rendered</param>
105-
/// <returns>The logger configuration</returns>
106-
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
107-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
108-
public static LoggerConfiguration SplunkViaUdp(
109-
this LoggerSinkConfiguration loggerConfiguration,
110-
IPAddress hostAddresss,
111-
int port,
112-
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
113-
IFormatProvider formatProvider = null,
114-
bool renderTemplate = true)
115-
{
116-
var sink = new UdpSink(hostAddresss, port, formatProvider, renderTemplate);
117-
64+
11865
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
11966
}
120-
12167
}
12268
}

0 commit comments

Comments
 (0)