1+ // Copyright 2018 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 ;
16+ using System . Net ;
17+ using Serilog . Configuration ;
18+ using Serilog . Events ;
19+ using Serilog . Formatting ;
20+ using Serilog . Formatting . Display ;
21+ using Serilog . Sinks . Splunk ;
22+
23+ namespace Serilog
24+ {
25+ /// <summary>
26+ /// Adds the WriteTo.SplunkViaEventCollector() extension method to <see cref="LoggerConfiguration"/>.
27+ /// </summary>
28+ public static class LoggerConfigurationSplunkExtensions
29+ {
30+ /// <summary>
31+ /// Adds a sink that writes log events as to a Splunk instance via TCP.
32+ /// </summary>
33+ /// <param name="loggerConfiguration">The logger config</param>
34+ /// <param name="connectionInfo"></param>
35+ /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
36+ /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
37+ /// <param name="renderTemplate">If true, the message template is rendered</param>
38+ /// <returns></returns>
39+ public static LoggerConfiguration SplunkViaTcp (
40+ this LoggerSinkConfiguration loggerConfiguration ,
41+ SplunkTcpSinkConnectionInfo connectionInfo ,
42+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
43+ IFormatProvider formatProvider = null ,
44+ bool renderTemplate = true )
45+ {
46+ var sink = new TcpSink ( connectionInfo , formatProvider , renderTemplate ) ;
47+
48+ return loggerConfiguration . Sink ( sink , restrictedToMinimumLevel ) ;
49+ }
50+
51+ /// <summary>
52+ /// Adds a sink that writes log events as to a Splunk instance via TCP.
53+ /// </summary>
54+ /// <param name="loggerConfiguration">The logger config</param>
55+ /// <param name="connectionInfo"></param>
56+ /// <param name="formatter">Custom formatter to use if you e.g. do not want to use the JsonFormatter.</param>
57+ /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
58+ /// <returns></returns>
59+ public static LoggerConfiguration SplunkViaTcp (
60+ this LoggerSinkConfiguration loggerConfiguration ,
61+ SplunkTcpSinkConnectionInfo connectionInfo ,
62+ ITextFormatter formatter ,
63+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum )
64+ {
65+ var sink = new TcpSink ( 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 TCP.
72+ /// </summary>
73+ /// <param name="loggerConfiguration">The logger config</param>
74+ /// <param name="hostAddresss">The Splunk host that is configured for UDP logging</param>
75+ /// <param name="port">The TCP 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 true, the message template is 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 SplunkViaTcp (
83+ this LoggerSinkConfiguration loggerConfiguration ,
84+ IPAddress hostAddresss ,
85+ int port ,
86+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
87+ IFormatProvider formatProvider = null ,
88+ bool renderTemplate = true )
89+ {
90+ var sink = new TcpSink ( hostAddresss , port , formatProvider , renderTemplate ) ;
91+
92+ return loggerConfiguration . Sink ( sink , restrictedToMinimumLevel ) ;
93+ }
94+
95+ /// <summary>
96+ /// Adds a sink that writes log events as to a Splunk instance via TCP.
97+ /// </summary>
98+ /// <param name="loggerConfiguration">The logger config</param>
99+ /// <param name="host">The Splunk host that is configured for UDP logging</param>
100+ /// <param name="port">The TCP port</param>
101+ /// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
102+ /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
103+ /// <param name="renderTemplate">If ture, the message template is rendered</param>
104+ /// <returns></returns>
105+ /// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
106+ [ Obsolete ( "Use the overload accepting a connection info object instead. This overload will be removed." , false ) ]
107+ public static LoggerConfiguration SplunkViaTcp (
108+ this LoggerSinkConfiguration loggerConfiguration ,
109+ string host ,
110+ int port ,
111+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
112+ IFormatProvider formatProvider = null ,
113+ bool renderTemplate = true )
114+ {
115+ var sink = new TcpSink ( host , port , formatProvider , renderTemplate ) ;
116+
117+ return loggerConfiguration . Sink ( sink , restrictedToMinimumLevel ) ;
118+ }
119+ }
120+ }
0 commit comments