Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:LoggingAPIImpl_Update' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuarezgx authored and genexusbot committed Sep 26, 2023
1 parent 0aa9f6d commit b90a4b9
Showing 1 changed file with 43 additions and 50 deletions.
93 changes: 43 additions & 50 deletions dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ public class Program
const string DEFAULT_PORT = "80";
static string DEFAULT_SCHEMA = Uri.UriSchemeHttp;

private static string OPENTELEMETRY_SERVICE = "Observability";
private static string OPENTELEMETRY_AZURE_DISTRO = "GeneXus.OpenTelemetry.Azure.AzureAppInsights";
private static string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";
public static void Main(string[] args)
{
try
Expand Down Expand Up @@ -82,61 +79,26 @@ public static void Main(string[] args)
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(WebHostConfigureLogging)
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseWebRoot(Startup.LocalPath)
.UseContentRoot(Startup.LocalPath)
.Build();

public static IWebHost BuildWebHostPort(string[] args, string port)
{
return BuildWebHostPort(args, port, DEFAULT_SCHEMA);
}
static IWebHost BuildWebHostPort(string[] args, string port, string schema)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureLogging(WebHostConfigureLogging)
.UseUrls($"{schema}://*:{port}")
.UseStartup<Startup>()
.UseWebRoot(Startup.LocalPath)
.UseContentRoot(Startup.LocalPath)
.Build();
}

private static void WebHostConfigureLogging(WebHostBuilderContext hostingContext, ILoggingBuilder loggingBuilder)
{
loggingBuilder.AddConsole();
GXService providerService = GXServices.Instance?.Get(OPENTELEMETRY_SERVICE);
if (providerService != null && providerService.ClassName.StartsWith(OPENTELEMETRY_AZURE_DISTRO))
{
ConfigureAzureOpentelemetry(loggingBuilder);
}
}
private static void ConfigureAzureOpentelemetry(ILoggingBuilder loggingBuilder)
{
string endpoint = Environment.GetEnvironmentVariable(APPLICATIONINSIGHTS_CONNECTION_STRING);
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddTelemetrySdk();

loggingBuilder.AddOpenTelemetry(loggerOptions =>
{
loggerOptions
.SetResourceBuilder(resourceBuilder)
.AddAzureMonitorLogExporter(options =>
{
if (!string.IsNullOrEmpty(endpoint))
options.ConnectionString = endpoint;
else
options.Credential = new DefaultAzureCredential();
})
.AddConsoleExporter();

loggerOptions.IncludeFormattedMessage = true;
loggerOptions.IncludeScopes = true;
loggerOptions.ParseStateValues = true;
});
}
private static void LocatePhysicalLocalPath()
{
string startup = FileUtil.GetStartupDirectory();
Expand Down Expand Up @@ -167,8 +129,10 @@ public static IApplicationBuilder MapWebSocketManager(this IApplicationBuilder a

public class Startup
{
public static bool IsHttpContext = InitializeHttpContext();
static readonly IGXLogger log = GXLoggerFactory.GetLogger<Startup>();
static IGXLogger log;
private static string OPENTELEMETRY_SERVICE = "Observability";
private static string OPENTELEMETRY_AZURE_DISTRO = "GeneXus.OpenTelemetry.Azure.AzureAppInsights";
private static string APPLICATIONINSIGHTS_CONNECTION_STRING = "APPLICATIONINSIGHTS_CONNECTION_STRING";

const long DEFAULT_MAX_FILE_UPLOAD_SIZE_BYTES = 528000000;
public static string VirtualPath = string.Empty;
Expand All @@ -192,18 +156,47 @@ public class Startup
public List<string> servicesBase = new List<string>();

private GXRouting gxRouting;
static bool InitializeHttpContext()
{
GxContext.IsHttpContext = true;
return true;
}
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
{
Config.ConfigRoot = configuration;
GxContext.IsHttpContext = true;
GXRouting.ContentRootPath = env.ContentRootPath;
GXRouting.UrlTemplateControllerWithParms = "controllerWithParms";
GxContext.IsHttpContext = true;
gxRouting = new GXRouting(REST_BASE_URL);
log = GXLoggerFactory.GetLogger<Startup>();
}
private static void WebHostConfigureLogging(ILoggingBuilder loggingBuilder)
{
loggingBuilder.AddConsole();
GXService providerService = GXServices.Instance?.Get(OPENTELEMETRY_SERVICE);
if (providerService != null && providerService.ClassName.StartsWith(OPENTELEMETRY_AZURE_DISTRO))
{
ConfigureAzureOpentelemetry(loggingBuilder);
}
}
private static void ConfigureAzureOpentelemetry(ILoggingBuilder loggingBuilder)
{
string endpoint = Environment.GetEnvironmentVariable(APPLICATIONINSIGHTS_CONNECTION_STRING);
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddTelemetrySdk();

loggingBuilder.AddOpenTelemetry(loggerOptions =>
{
loggerOptions
.SetResourceBuilder(resourceBuilder)
.AddAzureMonitorLogExporter(options =>
{
if (!string.IsNullOrEmpty(endpoint))
options.ConnectionString = endpoint;
else
options.Credential = new DefaultAzureCredential();
})
.AddConsoleExporter();

loggerOptions.IncludeFormattedMessage = true;
loggerOptions.IncludeScopes = true;
loggerOptions.ParseStateValues = true;
});
}
public void ConfigureServices(IServiceCollection services)
{
Expand All @@ -220,7 +213,7 @@ public void ConfigureServices(IServiceCollection services)
options.AllowSynchronousIO = true;
});
services.AddDistributedMemoryCache();

services.AddLogging(builder => WebHostConfigureLogging(builder));
services.Configure<FormOptions>(options =>
{
if (Config.GetValueOf("MaxFileUploadSize", out string MaxFileUploadSizeStr) && long.TryParse(MaxFileUploadSizeStr, out long MaxFileUploadSize))
Expand Down

0 comments on commit b90a4b9

Please sign in to comment.