1- using Microsoft . AspNetCore . Builder ;
1+ using System ;
2+ using System . Net . Http ;
3+ using System . Threading ;
4+ using Microsoft . AspNetCore . Builder ;
25using Microsoft . AspNetCore . Hosting ;
36using Microsoft . Extensions . Configuration ;
47using Microsoft . Extensions . DependencyInjection ;
58using Microsoft . Extensions . Logging ;
9+ using Splunk ;
610using Splunk . Configurations ;
11+ using VTEX . SampleWebAPI . Logging ;
712
813namespace VTEX . SampleWebAPI
914{
1015 public class Startup
1116 {
17+ static readonly ILoggerFormatter formatter = new VTEXSplunkLoggerFormatter ( Microsoft . Extensions . PlatformAbstractions . PlatformServices . Default . Application . ApplicationVersion , GetHost ( ) ) ;
18+
1219 public Startup ( IConfiguration configuration )
1320 {
1421 Configuration = configuration ;
@@ -43,5 +50,34 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
4350
4451 app . UseMvc ( ) ;
4552 }
53+
54+ /// <summary>
55+ /// Method created to get AWS EC2 host Id, or set `dev` as host if AWS internal call fails.
56+ /// </summary>
57+ static string GetHost ( )
58+ {
59+ string host = string . Empty ;
60+ try
61+ {
62+ using ( HttpClient httpClient = new HttpClient ( ) )
63+ {
64+ TimeSpan timeSpan = new TimeSpan ( 0 , 0 , 5 ) ;
65+ var cancellationTokenSource = new CancellationTokenSource ( ( int ) timeSpan . TotalMilliseconds ) ;
66+ httpClient . Timeout = timeSpan ;
67+ httpClient . BaseAddress = new Uri ( "http://169.254.169.254/latest/meta-data/" ) ;
68+ host = httpClient
69+ . GetAsync ( "instance-id" , cancellationTokenSource . Token )
70+ . Result
71+ . Content
72+ . ReadAsStringAsync ( )
73+ . Result ;
74+ }
75+ }
76+ catch
77+ {
78+ host = "dev" ;
79+ }
80+ return host ;
81+ }
4682 }
4783}
0 commit comments