Skip to content

Commit e2a4040

Browse files
committed
Compatibility with .net standard
1 parent 076b999 commit e2a4040

File tree

4 files changed

+124
-182
lines changed

4 files changed

+124
-182
lines changed

src/Serilog.Sinks.TCP/Serilog.Sinks.Splunk.TCP.csproj

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

33
<PropertyGroup>
44
<Description>The Splunk TCP 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.TCP</AssemblyName>
1010
<PackageId>Serilog.Sinks.Splunk.TCP</PackageId>
@@ -19,14 +19,24 @@
1919
<SignAssembly>true</SignAssembly>
2020
</PropertyGroup>
2121

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" />
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+
<PackageReference Include="Splunk.Logging.Common" Version="1.7.2" />
32+
</ItemGroup>
33+
34+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
35+
<!--<PackageReference Include="System.Net.Http" Version="4.3.0" />-->
36+
<PackageReference Include="Splunk.Logging.Common.Core" Version="1.0.0" />
37+
</ItemGroup>
38+
39+
<ItemGroup>
40+
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.3.0" />
3041
</ItemGroup>
31-
3242
</Project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 TCP.
22+
/// </summary>
23+
public class SplunkTcpSinkConnectionInfo
24+
{
25+
/// <summary>
26+
/// Default size of the socket writer queue.
27+
/// </summary>
28+
public const int DefaultMaxQueueSize = 10000;
29+
30+
/// <summary>
31+
/// Splunk host.
32+
/// </summary>
33+
public IPAddress Host { get; }
34+
35+
/// <summary>
36+
/// Splunk port.
37+
/// </summary>
38+
public int Port { get; }
39+
40+
/// <summary>
41+
/// Max Queue size for the TCP socket writer.
42+
/// See <see cref="DefaultMaxQueueSize"/> for default value (10000).
43+
/// </summary>
44+
public int MaxQueueSize { get; set; } = DefaultMaxQueueSize;
45+
46+
/// <summary>
47+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
48+
/// for defining connection info for connecting using TCP against Splunk.
49+
/// </summary>
50+
/// <param name="host">Splunk host.</param>
51+
/// <param name="port">Splunk TCP port.</param>
52+
public SplunkTcpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
53+
54+
/// <summary>
55+
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
56+
/// for defining connection info for connecting using TCP against Splunk.
57+
/// </summary>
58+
/// <param name="host">Splunk host.</param>
59+
/// <param name="port">Splunk TCP port.</param>
60+
public SplunkTcpSinkConnectionInfo(IPAddress host, int port)
61+
{
62+
Host = host;
63+
Port = port;
64+
}
65+
}
66+
}
Lines changed: 33 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if TCP
2-
3-
// Copyright 2016 Serilog Contributors
1+
// Copyright 2016 Serilog Contributors
42
//
53
// Licensed under the Apache License, Version 2.0 (the "License");
64
// you may not use this file except in compliance with the License.
@@ -14,14 +12,13 @@
1412
// See the License for the specific language governing permissions and
1513
// limitations under the License.
1614

17-
using System;
18-
using System.IO;
19-
using System.Net;
20-
using System.Text;
2115
using Serilog.Core;
2216
using Serilog.Events;
2317
using Serilog.Formatting;
2418
using Splunk.Logging;
19+
using System;
20+
using System.IO;
21+
using System.Text;
2522

2623
namespace Serilog.Sinks.Splunk
2724
{
@@ -30,7 +27,10 @@ namespace Serilog.Sinks.Splunk
3027
/// </summary>
3128
public class TcpSink : ILogEventSink, IDisposable
3229
{
33-
readonly ITextFormatter _formatter;
30+
private readonly ITextFormatter _formatter;
31+
private readonly SplunkTcpSinkConnectionInfo _connectionInfo;
32+
private bool disposedValue = false;
33+
3434
private TcpSocketWriter _writer;
3535

3636
/// <summary>
@@ -39,70 +39,41 @@ public class TcpSink : ILogEventSink, IDisposable
3939
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
4040
/// <param name="formatProvider">Optional format provider</param>
4141
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42-
public TcpSink(
43-
SplunkTcpSinkConnectionInfo connectionInfo,
44-
IFormatProvider formatProvider = null,
45-
bool renderTemplate = true)
42+
public TcpSink(SplunkTcpSinkConnectionInfo connectionInfo, IFormatProvider formatProvider = null, bool renderTemplate = true)
43+
: this(connectionInfo, CreateDefaultFormatter(formatProvider, renderTemplate))
4644
{
47-
_writer = CreateSocketWriter(connectionInfo);
48-
_formatter = CreateDefaultFormatter(formatProvider, renderTemplate);
4945
}
5046

5147
/// <summary>
5248
/// Creates an instance of the Splunk TCP Sink.
5349
/// </summary>
5450
/// <param name="connectionInfo">Connection info used for connecting against Splunk.</param>
5551
/// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
56-
public TcpSink(
57-
SplunkTcpSinkConnectionInfo connectionInfo,
58-
ITextFormatter formatter)
52+
public TcpSink(SplunkTcpSinkConnectionInfo connectionInfo, ITextFormatter formatter)
5953
{
60-
_writer = CreateSocketWriter(connectionInfo);
54+
_connectionInfo = connectionInfo;
6155
_formatter = formatter;
56+
_writer = CreateSocketWriter(connectionInfo);
6257
}
6358

64-
/// <summary>
65-
/// Creates an instance of the Splunk TCP Sink
66-
/// </summary>
67-
/// <param name="host">The Splunk Host</param>
68-
/// <param name="port">The TCP port configured in Splunk</param>
69-
/// <param name="formatProvider">Optional format provider</param>
70-
/// <param name="renderTemplate">If true, the message template will be rendered</param>
71-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
72-
public TcpSink(
73-
string host,
74-
int port,
75-
IFormatProvider formatProvider = null,
76-
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(host, port), formatProvider, renderTemplate)
77-
{
78-
}
79-
80-
/// <summary>
81-
/// Creates an instance of the Splunk TCP Sink
82-
/// </summary>
83-
/// <param name="hostAddress">The Splunk Host</param>
84-
/// <param name="port">The TCP port configured in Splunk</param>
85-
/// <param name="formatProvider">Optional format provider</param>
86-
/// <param name="renderTemplate">If true, the message template will be rendered</param>
87-
[Obsolete("Use the overload accepting a connection info object instead. This overload will be removed.", false)]
88-
public TcpSink(
89-
IPAddress hostAddress,
90-
int port,
91-
IFormatProvider formatProvider = null,
92-
bool renderTemplate = true) : this(new SplunkTcpSinkConnectionInfo(hostAddress, port), formatProvider, renderTemplate)
59+
/// <inheritdoc/>
60+
protected virtual void Dispose(bool disposing)
9361
{
62+
if (!disposedValue)
63+
{
64+
if (disposing)
65+
{
66+
_writer?.Dispose();
67+
_writer = null;
68+
}
69+
disposedValue = true;
70+
}
9471
}
9572

96-
private static TcpSocketWriter CreateSocketWriter(SplunkTcpSinkConnectionInfo connectionInfo)
73+
/// <inheritdoc/>
74+
public void Dispose()
9775
{
98-
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
99-
100-
return new TcpSocketWriter(connectionInfo.Host, connectionInfo.Port, reconnectionPolicy, connectionInfo.MaxQueueSize);
101-
}
102-
103-
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
104-
{
105-
return new SplunkJsonFormatter(renderTemplate, formatProvider);
76+
Dispose(true);
10677
}
10778

10879
/// <inheritdoc/>
@@ -116,62 +87,16 @@ public void Emit(LogEvent logEvent)
11687
_writer.Enqueue(sb.ToString());
11788
}
11889

119-
/// <inheritdoc/>
120-
public void Dispose()
90+
private static TcpSocketWriter CreateSocketWriter(SplunkTcpSinkConnectionInfo connectionInfo)
12191
{
122-
_writer?.Dispose();
123-
_writer = null;
124-
}
125-
}
126-
127-
128-
/// <summary>
129-
/// Defines connection info used to connect against Splunk
130-
/// using TCP.
131-
/// </summary>
132-
public class SplunkTcpSinkConnectionInfo
133-
{
134-
/// <summary>
135-
/// Default size of the socket writer queue.
136-
/// </summary>
137-
public const int DefaultMaxQueueSize = 10000;
138-
139-
/// <summary>
140-
/// Splunk host.
141-
/// </summary>
142-
public IPAddress Host { get; }
143-
144-
/// <summary>
145-
/// Splunk port.
146-
/// </summary>
147-
public int Port { get; }
148-
149-
/// <summary>
150-
/// Max Queue size for the TCP socket writer.
151-
/// See <see cref="DefaultMaxQueueSize"/> for default value (10000).
152-
/// </summary>
153-
public int MaxQueueSize { get; set; } = DefaultMaxQueueSize;
92+
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
15493

155-
/// <summary>
156-
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
157-
/// for defining connection info for connecting using TCP against Splunk.
158-
/// </summary>
159-
/// <param name="host">Splunk host.</param>
160-
/// <param name="port">Splunk TCP port.</param>
161-
public SplunkTcpSinkConnectionInfo(string host, int port) : this(IPAddress.Parse(host), port) { }
94+
return new TcpSocketWriter(connectionInfo.Host, connectionInfo.Port, reconnectionPolicy, connectionInfo.MaxQueueSize);
95+
}
16296

163-
/// <summary>
164-
/// Creates an instance of <see cref="SplunkTcpSinkConnectionInfo"/> used
165-
/// for defining connection info for connecting using TCP against Splunk.
166-
/// </summary>
167-
/// <param name="host">Splunk host.</param>
168-
/// <param name="port">Splunk TCP port.</param>
169-
public SplunkTcpSinkConnectionInfo(IPAddress host, int port)
97+
private static SplunkJsonFormatter CreateDefaultFormatter(IFormatProvider formatProvider, bool renderTemplate)
17098
{
171-
Host = host;
172-
Port = port;
99+
return new SplunkJsonFormatter(renderTemplate, formatProvider);
173100
}
174101
}
175102
}
176-
177-
#endif

0 commit comments

Comments
 (0)