33using Microsoft . Extensions . Configuration ;
44using Microsoft . Extensions . DependencyInjection ;
55using Microsoft . Extensions . Logging ;
6+ using Microsoft . Extensions . Options ;
67using Splunk ;
78using Splunk . Configurations ;
8- using Vtex . SplunkLogger ;
99
1010namespace Vtex . SampleWebAPI
1111{
1212 public class Startup
1313 {
14- static readonly ILoggerFormatter formatter = new VTEXSplunkLoggerFormatter ( ) ;
15-
1614 public IConfiguration Configuration { get ; }
1715
1816 public Startup ( IConfiguration configuration )
@@ -25,6 +23,7 @@ public Startup(IConfiguration configuration)
2523 /// </summary>
2624 public void ConfigureServices ( IServiceCollection services )
2725 {
26+ services . Configure < SplunkLoggerConfiguration > ( Configuration . GetSection ( "Splunk" ) ) ;
2827 services . AddMvc ( ) ;
2928 }
3029
@@ -37,41 +36,57 @@ public void ConfigureServices(IServiceCollection services)
3736 /// <param name="loggerFactory">Logger factory.</param>
3837 public void Configure ( IApplicationBuilder app , IHostingEnvironment env , ILoggerFactory loggerFactory )
3938 {
40- ILoggerExtensions . SetApplication ( "SplunkLoggerSampleWebAPI" ) ;
41-
42- loggerFactory . AddDebug ( ) ;
39+ //loggerFactory.AddDebug();
4340
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 > > ( ) ;
5742
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 *********************************/
6255
63- //loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration, null); //
64- //loggerFactory.AddHECJsonSplunkLogger(splunkConfiguration, formatter); //
56+ app . UseMvc ( ) ;
57+ }
6558
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 ;
6865
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 ;
7370
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 ;
7590 }
7691 }
7792}
0 commit comments