3
3
using Microsoft . Extensions . Configuration ;
4
4
using Microsoft . Extensions . DependencyInjection ;
5
5
using Microsoft . Extensions . Logging ;
6
+ using Microsoft . Extensions . Options ;
6
7
using Splunk ;
7
8
using Splunk . Configurations ;
8
- using Vtex . SplunkLogger ;
9
9
10
10
namespace Vtex . SampleWebAPI
11
11
{
12
12
public class Startup
13
13
{
14
- static readonly ILoggerFormatter formatter = new VTEXSplunkLoggerFormatter ( ) ;
15
-
16
14
public IConfiguration Configuration { get ; }
17
15
18
16
public Startup ( IConfiguration configuration )
@@ -25,6 +23,7 @@ public Startup(IConfiguration configuration)
25
23
/// </summary>
26
24
public void ConfigureServices ( IServiceCollection services )
27
25
{
26
+ services . Configure < SplunkLoggerConfiguration > ( Configuration . GetSection ( "Splunk" ) ) ;
28
27
services . AddMvc ( ) ;
29
28
}
30
29
@@ -37,41 +36,57 @@ public void ConfigureServices(IServiceCollection services)
37
36
/// <param name="loggerFactory">Logger factory.</param>
38
37
public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
39
38
{
40
- ILoggerExtensions . SetApplication ( "SplunkLoggerSampleWebAPI" ) ;
41
-
42
- loggerFactory . AddDebug ( ) ;
39
+ //loggerFactory.AddDebug();
43
40
44
- var splunkConfiguration = new SplunkLoggerConfiguration ( )
45
- {
46
- HecConfiguration = new HECConfiguration ( )
47
- {
48
- SplunkCollectorUrl = "https://localhost:8088/services/collector" ,
49
- Token = "753c5a9c-fb59-4da0-9064-947f99dc20ba"
50
- } ,
51
- SocketConfiguration = new SocketConfiguration ( )
52
- {
53
- HostName = "localhost" ,
54
- Port = 8111
55
- }
56
- } ;
41
+ var splunkLoggerConfigurationOption = app . ApplicationServices . GetService < IOptions < SplunkLoggerConfiguration > > ( ) ;
57
42
58
- /**************************** Define Your Logger ****************************/
59
- /* */
60
- //loggerFactory.AddHECRawSplunkLogger(splunkConfiguration, null); //
61
- loggerFactory . AddHECRawSplunkLogger ( splunkConfiguration , formatter ) ; //
43
+ /******************************** Define Your Logger *********************************/
44
+ /* */
45
+ loggerFactory . AddHECRawSplunkLogger ( splunkLoggerConfigurationOption . Value ) ; //
46
+ // //
47
+ // //
48
+ //loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration); //
49
+ // //
50
+ //loggerFactory.AddTcpSplunkLogger(splunkConfiguration); //
51
+ // //
52
+ //loggerFactory.AddUdpSplunkLogger(splunkConfiguration); //
53
+ /* */
54
+ /******************************** Define Your Logger *********************************/
62
55
63
- //loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration, null); //
64
- //loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration, formatter); //
56
+ app . UseMvc ( ) ;
57
+ }
65
58
66
- //loggerFactory.AddTcpSplunkLogger(splunkConfiguration, null); //
67
- //loggerFactory.AddTcpSplunkLogger(splunkConfiguration, formatter); //
59
+ /// <summary>
60
+ /// Demonstrate how can you provide configuration to your splunk logger addapter(s)
61
+ /// </summary>
62
+ SplunkLoggerConfiguration GetSplunkLoggerConfiguration ( IApplicationBuilder app )
63
+ {
64
+ SplunkLoggerConfiguration result = null ;
68
65
69
- //loggerFactory.AddUdpSplunkLogger(splunkConfiguration, null);
70
- //loggerFactory.AddUdpSplunkLogger(splunkConfiguration, formatter );
71
- /* */
72
- /**************************** Define Your Logger ****************************/
66
+ //Retrieving Splunk configuration from appsettings json configuration file
67
+ var splunkLoggerConfigurationOption = app . ApplicationServices . GetService < IOptions < SplunkLoggerConfiguration > > ( ) ;
68
+ if ( splunkLoggerConfigurationOption != null && splunkLoggerConfigurationOption . Value != null )
69
+ result = app . ApplicationServices . GetService < IOptions < SplunkLoggerConfiguration > > ( ) . Value ;
73
70
74
- app . UseMvc ( ) ;
71
+ //You can also provide a hard code configuration
72
+ //result = new SplunkLoggerConfiguration()
73
+ //{
74
+ // HecConfiguration = new HECConfiguration()
75
+ // {
76
+ // SplunkCollectorUrl = "https://localhost:8088/services/collector",
77
+ // BatchIntervalInMiliseconds = 5000,
78
+ // BatchSizeCount = 100,
79
+ // ChannelIdType = HECConfiguration.ChannelIdOption.None,
80
+
81
+ // Token = "753c5a9c-fb59-4da0-9064-947f99dc20ba"
82
+ // },
83
+ // SocketConfiguration = new SocketConfiguration()
84
+ // {
85
+ // HostName = "localhost",
86
+ // Port = 8111
87
+ // }
88
+ //};
89
+ return result ;
75
90
}
76
91
}
77
92
}
0 commit comments