diff --git a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs index 95acd9dc8..7df821056 100644 --- a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs +++ b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs @@ -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 @@ -82,13 +79,12 @@ public static void Main(string[] args) } public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .ConfigureLogging(WebHostConfigureLogging) + WebHost.CreateDefaultBuilder(args) .UseStartup() .UseWebRoot(Startup.LocalPath) .UseContentRoot(Startup.LocalPath) .Build(); - + public static IWebHost BuildWebHostPort(string[] args, string port) { return BuildWebHostPort(args, port, DEFAULT_SCHEMA); @@ -96,7 +92,6 @@ public static IWebHost BuildWebHostPort(string[] args, string port) static IWebHost BuildWebHostPort(string[] args, string port, string schema) { return WebHost.CreateDefaultBuilder(args) - .ConfigureLogging(WebHostConfigureLogging) .UseUrls($"{schema}://*:{port}") .UseStartup() .UseWebRoot(Startup.LocalPath) @@ -104,39 +99,6 @@ static IWebHost BuildWebHostPort(string[] args, string port, string schema) .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(); @@ -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(); + 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; @@ -192,18 +156,47 @@ public class Startup public List servicesBase = new List(); 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(); + } + 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) { @@ -220,7 +213,7 @@ public void ConfigureServices(IServiceCollection services) options.AllowSynchronousIO = true; }); services.AddDistributedMemoryCache(); - + services.AddLogging(builder => WebHostConfigureLogging(builder)); services.Configure(options => { if (Config.GetValueOf("MaxFileUploadSize", out string MaxFileUploadSizeStr) && long.TryParse(MaxFileUploadSizeStr, out long MaxFileUploadSize))