Skip to content

Commit 57024d3

Browse files
committed
Dispose StringWriter
1 parent 78d63e4 commit 57024d3

File tree

1 file changed

+52
-80
lines changed

1 file changed

+52
-80
lines changed

src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkJsonFormatter.cs

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

15+
using Serilog.Events;
16+
using Serilog.Formatting;
17+
using Serilog.Formatting.Json;
1518
using System;
1619
using System.Collections.Generic;
1720
using System.Globalization;
1821
using System.IO;
19-
using Serilog.Events;
20-
using Serilog.Formatting;
21-
using Serilog.Formatting.Json;
2222

2323
namespace Serilog.Sinks.Splunk
2424
{
@@ -63,38 +63,8 @@ public SplunkJsonFormatter(
6363
string sourceType,
6464
string host,
6565
string index)
66+
: this(renderTemplate, formatProvider, source, sourceType, host, index, null)
6667
{
67-
_renderTemplate = renderTemplate;
68-
_formatProvider = formatProvider;
69-
70-
var suffixWriter = new StringWriter();
71-
suffixWriter.Write("}"); // Terminates "event"
72-
73-
if (!string.IsNullOrWhiteSpace(source))
74-
{
75-
suffixWriter.Write(",\"source\":");
76-
JsonValueFormatter.WriteQuotedJsonString(source, suffixWriter);
77-
}
78-
79-
if (!string.IsNullOrWhiteSpace(sourceType))
80-
{
81-
suffixWriter.Write(",\"sourcetype\":");
82-
JsonValueFormatter.WriteQuotedJsonString(sourceType, suffixWriter);
83-
}
84-
85-
if (!string.IsNullOrWhiteSpace(host))
86-
{
87-
suffixWriter.Write(",\"host\":");
88-
JsonValueFormatter.WriteQuotedJsonString(host, suffixWriter);
89-
}
90-
91-
if (!string.IsNullOrWhiteSpace(index))
92-
{
93-
suffixWriter.Write(",\"index\":");
94-
JsonValueFormatter.WriteQuotedJsonString(index, suffixWriter);
95-
}
96-
suffixWriter.Write('}'); // Terminates the payload
97-
_suffix = suffixWriter.ToString();
9868
}
9969

10070
/// <summary>
@@ -119,63 +89,65 @@ public SplunkJsonFormatter(
11989
_renderTemplate = renderTemplate;
12090
_formatProvider = formatProvider;
12191

122-
var suffixWriter = new StringWriter();
123-
suffixWriter.Write("}"); // Terminates "event"
124-
125-
if (!string.IsNullOrWhiteSpace(source))
92+
using (var suffixWriter = new StringWriter())
12693
{
127-
suffixWriter.Write(",\"source\":");
128-
JsonValueFormatter.WriteQuotedJsonString(source, suffixWriter);
129-
}
94+
suffixWriter.Write("}"); // Terminates "event"
13095

131-
if (!string.IsNullOrWhiteSpace(sourceType))
132-
{
133-
suffixWriter.Write(",\"sourcetype\":");
134-
JsonValueFormatter.WriteQuotedJsonString(sourceType, suffixWriter);
135-
}
96+
if (!string.IsNullOrWhiteSpace(source))
97+
{
98+
suffixWriter.Write(",\"source\":");
99+
JsonValueFormatter.WriteQuotedJsonString(source, suffixWriter);
100+
}
136101

137-
if (!string.IsNullOrWhiteSpace(host))
138-
{
139-
suffixWriter.Write(",\"host\":");
140-
JsonValueFormatter.WriteQuotedJsonString(host, suffixWriter);
141-
}
102+
if (!string.IsNullOrWhiteSpace(sourceType))
103+
{
104+
suffixWriter.Write(",\"sourcetype\":");
105+
JsonValueFormatter.WriteQuotedJsonString(sourceType, suffixWriter);
106+
}
142107

143-
if (!string.IsNullOrWhiteSpace(index))
144-
{
145-
suffixWriter.Write(",\"index\":");
146-
JsonValueFormatter.WriteQuotedJsonString(index, suffixWriter);
147-
}
148-
if (customFields != null)
149-
{
150-
// "fields": {"club":"glee", "wins",["regionals","nationals"]}
151-
suffixWriter.Write(",\"fields\": {");
152-
var lastFieldIndex = customFields.CustomFieldList.Count;
153-
foreach (var customField in customFields.CustomFieldList)
108+
if (!string.IsNullOrWhiteSpace(host))
154109
{
155-
if (customField.ValueList.Count == 1)
156-
{
157-
//only one value e.g "club":"glee",
158-
suffixWriter.Write($"\"{customField.Name}\":");
159-
suffixWriter.Write($"\"{customField.ValueList[0]}\"");
160-
}
161-
else
110+
suffixWriter.Write(",\"host\":");
111+
JsonValueFormatter.WriteQuotedJsonString(host, suffixWriter);
112+
}
113+
114+
if (!string.IsNullOrWhiteSpace(index))
115+
{
116+
suffixWriter.Write(",\"index\":");
117+
JsonValueFormatter.WriteQuotedJsonString(index, suffixWriter);
118+
}
119+
if (customFields != null)
120+
{
121+
// "fields": {"club":"glee", "wins",["regionals","nationals"]}
122+
suffixWriter.Write(",\"fields\": {");
123+
var lastFieldIndex = customFields.CustomFieldList.Count;
124+
foreach (var customField in customFields.CustomFieldList)
162125
{
163-
//array of values e.g "wins",["regionals","nationals"]
164-
suffixWriter.Write($"\"{customField.Name}\":[");
165-
var lastArrIndex = customField.ValueList.Count;
166-
foreach (var cf in customField.ValueList)
126+
if (customField.ValueList.Count == 1)
167127
{
168-
suffixWriter.Write($"\"{cf}\"");
169-
//Different behaviour if it is the last one
170-
suffixWriter.Write(--lastArrIndex > 0 ? "," : "]");
128+
//only one value e.g "club":"glee",
129+
suffixWriter.Write($"\"{customField.Name}\":");
130+
suffixWriter.Write($"\"{customField.ValueList[0]}\"");
171131
}
132+
else
133+
{
134+
//array of values e.g "wins",["regionals","nationals"]
135+
suffixWriter.Write($"\"{customField.Name}\":[");
136+
var lastArrIndex = customField.ValueList.Count;
137+
foreach (var cf in customField.ValueList)
138+
{
139+
suffixWriter.Write($"\"{cf}\"");
140+
//Different behaviour if it is the last one
141+
suffixWriter.Write(--lastArrIndex > 0 ? "," : "]");
142+
}
143+
}
144+
suffixWriter.Write(--lastFieldIndex > 0 ? "," : "}");
172145
}
173-
suffixWriter.Write(--lastFieldIndex > 0 ? "," : "}");
146+
174147
}
175-
148+
suffixWriter.Write('}'); // Terminates the payload
149+
_suffix = suffixWriter.ToString();
176150
}
177-
suffixWriter.Write('}'); // Terminates the payload
178-
_suffix = suffixWriter.ToString();
179151
}
180152

181153
/// <inheritdoc/>

0 commit comments

Comments
 (0)