1
1
using System ;
2
2
using System . Linq ;
3
+ using System . Net . Http ;
4
+ using System . Threading ;
3
5
using Microsoft . Extensions . Logging ;
4
6
using Splunk ;
5
7
6
8
namespace VTEX . SampleWebAPI . Logging
7
9
{
10
+ /// <summary>
11
+ /// This class contains all methods to format a Log event into a VTEX standard text.
12
+ /// </summary>
13
+ /// <remarks>
14
+ /// At our Splunk environment at VTEX we expect certain standard of text to be processed and
15
+ /// we also have field extraction rules to be applied.
16
+ /// Thats the reason why we use a custom LoggerFormmater.
17
+ /// </remarks>
8
18
public class VTEXSplunkLoggerFormatter : ILoggerFormatter
9
19
{
10
20
const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffZ" ;
11
21
12
22
readonly string appVersion ;
13
23
readonly string host ;
14
24
15
- public VTEXSplunkLoggerFormatter ( string appVersion , string host )
25
+ /// <summary>
26
+ /// Initializes a new instance of the <see cref="T:VTEX.SampleWebAPI.Logging.VTEXSplunkLoggerFormatter"/> class.
27
+ /// </summary>
28
+ public VTEXSplunkLoggerFormatter ( )
16
29
{
17
- this . appVersion = appVersion ;
18
- this . host = host ;
30
+ appVersion = Microsoft . Extensions . PlatformAbstractions . PlatformServices . Default . Application . ApplicationVersion ;
31
+ host = GetHost ( ) ;
19
32
}
20
33
34
+ /// <summary>
35
+ /// Format the specified logLevel, eventId, state and exception into log string entry.
36
+ /// </summary>
37
+ /// <returns>Formatted log string.</returns>
38
+ /// <param name="logLevel">Log level.</param>
39
+ /// <param name="eventId">Event identifier.</param>
40
+ /// <param name="state">Log object state.</param>
41
+ /// <param name="exception">Log exception.</param>
42
+ /// <typeparam name="T">Log entry.</typeparam>
21
43
public string Format < T > ( LogLevel logLevel , EventId eventId , T state , Exception exception )
22
44
{
23
45
string log ;
@@ -46,11 +68,53 @@ public string Format<T>(LogLevel logLevel, EventId eventId, T state, Exception e
46
68
return log ;
47
69
}
48
70
71
+ /// <summary>
72
+ /// Formats the specified logLevel, eventId, state and exception into json entry.
73
+ /// </summary>
74
+ /// <returns>The json.</returns>
75
+ /// <param name="logLevel">Log level.</param>
76
+ /// <param name="eventId">Event identifier.</param>
77
+ /// <param name="state">Log object state.</param>
78
+ /// <param name="exception">Log exception.</param>
79
+ /// <typeparam name="T">Log entry.</typeparam>
49
80
public SplunkJSONEntry FormatJson < T > ( LogLevel logLevel , EventId eventId , T state , Exception exception )
50
81
{
51
82
return new SplunkJSONEntry ( Format ( logLevel , eventId , state , exception ) , 0 , host , string . Empty , "Log" ) ;
52
83
}
53
84
85
+ /// <summary>
86
+ /// Method created to get AWS EC2 host Id, or set `dev` as host if AWS internal call fails.
87
+ /// </summary>
88
+ string GetHost ( )
89
+ {
90
+ string ec2Host = string . Empty ;
91
+ try
92
+ {
93
+ using ( HttpClient httpClient = new HttpClient ( ) )
94
+ {
95
+ TimeSpan timeSpan = new TimeSpan ( 0 , 0 , 5 ) ;
96
+ var cancellationTokenSource = new CancellationTokenSource ( ( int ) timeSpan . TotalMilliseconds ) ;
97
+ httpClient . Timeout = timeSpan ;
98
+ httpClient . BaseAddress = new Uri ( "http://169.254.169.254/latest/meta-data/" ) ;
99
+ ec2Host = httpClient
100
+ . GetAsync ( "instance-id" , cancellationTokenSource . Token )
101
+ . Result
102
+ . Content
103
+ . ReadAsStringAsync ( )
104
+ . Result ;
105
+ }
106
+ }
107
+ catch
108
+ {
109
+ ec2Host = "dev" ;
110
+ }
111
+ return ec2Host ;
112
+ }
113
+
114
+ /// <summary>
115
+ /// Based on LogLevel we define the correspondent VTEX event level.
116
+ /// </summary>
117
+ /// <returns>VTEX event level.</returns>
54
118
string GetVTEXEventLevel ( LogLevel logLevel )
55
119
{
56
120
VTEXEventLevel eventLevel = VTEXEventLevel . Debug ;
@@ -75,6 +139,10 @@ string GetVTEXEventLevel(LogLevel logLevel)
75
139
return eventLevel . ToString ( ) . ToLower ( ) ;
76
140
}
77
141
142
+ /// <summary>
143
+ /// Based on LogLevel we define the correspondent VTEX log type.
144
+ /// </summary>
145
+ /// <returns>VTEX log type.</returns>
78
146
string GetVTEXLogType ( LogLevel logLevel )
79
147
{
80
148
VTEXLogType logType = VTEXLogType . Info ;
@@ -97,4 +165,4 @@ string GetVTEXLogType(LogLevel logLevel)
97
165
return logType . ToString ( ) . ToLower ( ) ;
98
166
}
99
167
}
100
- }
168
+ }
0 commit comments