1- // Copyright 2014 Serilog Contriutors
1+ // Copyright 2016 Serilog Contributors
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
1010// distributed under the License is distributed on an "AS IS" BASIS,
1111// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212// See the License for the specific language governing permissions and
13- // limitations under the License.using System;
13+ // limitations under the License.
1414
1515using System ;
16+ using System . Collections . Generic ;
1617using System . IO ;
18+ using Serilog . Events ;
19+ using Serilog . Formatting ;
1720using Serilog . Formatting . Json ;
1821
1922namespace Serilog . Sinks . Splunk
2023{
2124 /// <summary>
22- /// A json formatter to allow conditional rendering of properties
25+ /// Renders log events into a default JSON format for consumption by Splunk.
2326 /// </summary>
24- public class SplunkJsonFormatter : JsonFormatter
27+ class SplunkJsonFormatter : ITextFormatter
2528 {
26- private readonly bool _renderTemplate ;
29+ static readonly JsonValueFormatter ValueFormatter = new JsonValueFormatter ( ) ;
30+
31+ readonly bool _renderTemplate ;
32+ readonly bool _renderMessage ;
33+ readonly IFormatProvider _formatProvider ;
2734
2835 /// <summary>
29- /// Construct a <see cref="JsonFormatter "/>.
36+ /// Construct a <see cref="SplunkJsonFormatter "/>.
3037 /// </summary>
31- /// <param name="omitEnclosingObject">If true, the properties of the event will be written to
32- /// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
33- /// JSON object.</param>
34- /// <param name="closingDelimiter">A string that will be written after each log event is formatted.
35- /// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
36- /// is true.</param>
3738 /// <param name="renderMessage">If true, the message will be rendered and written to the output as a
3839 /// property named RenderedMessage.</param>
3940 /// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
4041 /// <param name="renderTemplate">If true, the template used will be rendered and written to the output as a property named MessageTemplate</param>
4142 public SplunkJsonFormatter (
42- bool omitEnclosingObject = false ,
43- string closingDelimiter = null ,
43+ bool renderTemplate = true ,
4444 bool renderMessage = false ,
45- IFormatProvider formatProvider = null ,
46- bool renderTemplate = true )
47- : base ( omitEnclosingObject , closingDelimiter , renderMessage , formatProvider )
45+ IFormatProvider formatProvider = null )
4846 {
4947 _renderTemplate = renderTemplate ;
48+ _renderMessage = renderMessage ;
49+ _formatProvider = formatProvider ;
5050 }
5151
52+ public void Format ( LogEvent logEvent , TextWriter output )
53+ {
54+ if ( logEvent == null ) throw new ArgumentNullException ( nameof ( logEvent ) ) ;
55+ if ( output == null ) throw new ArgumentNullException ( nameof ( output ) ) ;
5256
53- /// <summary>
54- /// Writes the message with or without the message template
55- /// </summary>
56- /// <param name="template"></param>
57- /// <param name="delim"></param>
58- /// <param name="output"></param>
59- protected override void WriteMessageTemplate ( string template , ref string delim , TextWriter output )
57+ output . Write ( "{\" Timestamp\" :\" " ) ;
58+ output . Write ( logEvent . Timestamp . ToString ( "o" ) ) ;
59+ output . Write ( "\" ,\" Level\" :\" " ) ;
60+ output . Write ( logEvent . Level ) ;
61+
62+ if ( _renderTemplate )
63+ {
64+ output . Write ( "\" ,\" MessageTemplate\" :" ) ;
65+ JsonValueFormatter . WriteQuotedJsonString ( logEvent . MessageTemplate . Text , output ) ;
66+ }
67+
68+ if ( _renderMessage )
69+ {
70+ output . Write ( "\" ,\" RenderedMessage\" :" ) ;
71+ JsonValueFormatter . WriteQuotedJsonString ( logEvent . RenderMessage ( _formatProvider ) , output ) ;
72+ }
73+
74+ if ( logEvent . Exception != null )
75+ {
76+ output . Write ( ",\" Exception\" :" ) ;
77+ JsonValueFormatter . WriteQuotedJsonString ( logEvent . Exception . ToString ( ) , output ) ;
78+ }
79+
80+ if ( logEvent . Properties . Count != 0 )
81+ WriteProperties ( logEvent . Properties , output ) ;
82+
83+ output . Write ( '}' ) ;
84+ output . WriteLine ( ) ;
85+ }
86+
87+ static void WriteProperties ( IReadOnlyDictionary < string , LogEventPropertyValue > properties , TextWriter output )
6088 {
61- if ( _renderTemplate )
62- base . WriteMessageTemplate ( template , ref delim , output ) ;
89+ output . Write ( ",\" Properties\" :{" ) ;
90+
91+ var precedingDelimiter = "" ;
92+ foreach ( var property in properties )
93+ {
94+ output . Write ( precedingDelimiter ) ;
95+ precedingDelimiter = "," ;
96+
97+ JsonValueFormatter . WriteQuotedJsonString ( property . Key , output ) ;
98+ output . Write ( ':' ) ;
99+ ValueFormatter . Format ( property . Value , output ) ;
100+ }
101+
102+ output . Write ( '}' ) ;
63103 }
64104 }
65- }
105+ }
0 commit comments