Skip to content

Commit e8d829e

Browse files
committed
Define VTEXSplunkLoggerFormatter as default ILoggerFormatter to be used and also implemented method to check host identification
#9
1 parent 306906e commit e8d829e

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/SampleWebAPI/Startup.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using System;
2+
using System.Net.Http;
3+
using System.Threading;
4+
using Microsoft.AspNetCore.Builder;
25
using Microsoft.AspNetCore.Hosting;
36
using Microsoft.Extensions.Configuration;
47
using Microsoft.Extensions.DependencyInjection;
58
using Microsoft.Extensions.Logging;
9+
using Splunk;
610
using Splunk.Configurations;
11+
using VTEX.SampleWebAPI.Logging;
712

813
namespace 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

Comments
 (0)