1- using Azure . Monitor . OpenTelemetry . AspNetCore ;
2- using EssentialCSharp . Web . Areas . Identity . Data ;
3- using EssentialCSharp . Web . Areas . Identity . Services . PasswordValidators ;
4- using EssentialCSharp . Web . Data ;
5- using EssentialCSharp . Web . Extensions ;
6- using EssentialCSharp . Web . Middleware ;
7- using EssentialCSharp . Web . Services ;
8- using EssentialCSharp . Web . Services . Referrals ;
1+ using Azure . Monitor . OpenTelemetry . AspNetCore ;
2+ using EssentialCSharp . Web . Areas . Identity . Data ;
3+ using EssentialCSharp . Web . Areas . Identity . Services . PasswordValidators ;
4+ using EssentialCSharp . Web . Data ;
5+ using EssentialCSharp . Web . Extensions ;
6+ using EssentialCSharp . Web . Middleware ;
7+ using EssentialCSharp . Web . Services ;
8+ using EssentialCSharp . Web . Services . Referrals ;
99using Mailjet . Client ;
1010using Microsoft . AspNetCore . HttpOverrides ;
1111using Microsoft . AspNetCore . Identity ;
12- using Microsoft . AspNetCore . Identity . UI . Services ;
13- using Microsoft . EntityFrameworkCore ;
14-
15- namespace EssentialCSharp . Web ;
16-
17- public partial class Program
18- {
19- private static void Main ( string [ ] args )
20- {
12+ using Microsoft . AspNetCore . Identity . UI . Services ;
13+ using Microsoft . EntityFrameworkCore ;
14+
15+ namespace EssentialCSharp . Web ;
16+
17+ public partial class Program
18+ {
19+ private static void Main ( string [ ] args )
20+ {
2121 WebApplicationBuilder builder = WebApplication . CreateBuilder ( args ) ;
2222
2323 builder . Services . Configure < ForwardedHeadersOptions > ( options =>
@@ -31,20 +31,35 @@ private static void Main(string[] args)
3131 options . KnownNetworks . Clear ( ) ;
3232 options . KnownProxies . Clear ( ) ;
3333 } ) ;
34-
35- ConfigurationManager configuration = builder . Configuration ;
34+
35+ ConfigurationManager configuration = builder . Configuration ;
3636 string connectionString = builder . Configuration . GetConnectionString ( "EssentialCSharpWebContextConnection" ) ?? throw new InvalidOperationException ( "Connection string 'EssentialCSharpWebContextConnection' not found." ) ;
3737
38- builder . Logging . AddConsole ( ) ;
39- builder . Services . AddHealthChecks ( ) ;
38+ builder . Logging . AddConsole ( ) ;
39+ builder . Services . AddHealthChecks ( ) ;
40+
41+ // Create a temporary logger for startup logging
42+ using var loggerFactory = LoggerFactory . Create ( loggingBuilder =>
43+ loggingBuilder . AddConsole ( ) . SetMinimumLevel ( LogLevel . Information ) ) ;
44+ var logger = loggerFactory . CreateLogger < Program > ( ) ;
4045
4146 if ( ! builder . Environment . IsDevelopment ( ) )
4247 {
43- // Configure Azure Application Insights with OpenTelemetry
44- builder . Services . AddOpenTelemetry ( ) . UseAzureMonitor ( ) ;
45- builder . Services . AddApplicationInsightsTelemetry ( ) ;
46- builder . Services . AddServiceProfiler ( ) ;
47- }
48+ // Configure Azure Application Insights with OpenTelemetry only if connection string is available
49+ var appInsightsConnectionString = builder . Configuration . GetConnectionString ( "ApplicationInsights" )
50+ ?? builder . Configuration [ "ApplicationInsights:ConnectionString" ] ;
51+
52+ if ( ! string . IsNullOrEmpty ( appInsightsConnectionString ) )
53+ {
54+ builder . Services . AddOpenTelemetry ( ) . UseAzureMonitor ( ) ;
55+ builder . Services . AddApplicationInsightsTelemetry ( ) ;
56+ builder . Services . AddServiceProfiler ( ) ;
57+ }
58+ else
59+ {
60+ logger . LogWarning ( "Application Insights connection string not found. Telemetry collection will be disabled." ) ;
61+ }
62+ }
4863
4964 builder . Services . AddDbContext < EssentialCSharpWebContext > ( options => options . UseSqlServer ( connectionString ) ) ;
5065 builder . Services . AddDefaultIdentity < EssentialCSharpWebUser > ( options =>
@@ -70,9 +85,9 @@ private static void Main(string[] args)
7085 . AddEntityFrameworkStores < EssentialCSharpWebContext > ( )
7186 . AddPasswordValidator < UsernameOrEmailAsPasswordValidator < EssentialCSharpWebUser > > ( )
7287 . AddPasswordValidator < Top100000PasswordValidator < EssentialCSharpWebUser > > ( ) ;
73-
74- builder . Configuration
75- . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
88+
89+ builder . Configuration
90+ . AddJsonFile ( "appsettings.json" , optional : false , reloadOnChange : true )
7691 . AddEnvironmentVariables ( ) ;
7792
7893 builder . Services . ConfigureApplicationCookie ( options =>
@@ -113,22 +128,22 @@ private static void Main(string[] args)
113128 return userPasswordStore ;
114129 }
115130 throw new NotSupportedException ( "The default UI requires a user store with password support." ) ;
116- } ) ;
131+ } ) ;
117132
118- //TODO: Implement the anti-forgery token with every POST/PUT request: https://learn.microsoft.com/en-us/aspnet/core/security/anti-request-forgery
133+ //TODO: Implement the anti-forgery token with every POST/PUT request: https://learn.microsoft.com/en-us/aspnet/core/security/anti-request-forgery
119134
120135 if ( ! builder . Environment . IsDevelopment ( ) )
121136 {
122137 builder . Services . AddTransient < IEmailSender , EmailSender > ( ) ;
123138 }
124139 builder . Services . Configure < AuthMessageSenderOptions > ( builder . Configuration . GetSection ( AuthMessageSenderOptions . AuthMessageSender ) ) ;
125-
126- // Add services to the container.
140+
141+ // Add services to the container.
127142 builder . Services . AddRazorPages ( ) ;
128143 builder . Services . AddCaptchaService ( builder . Configuration . GetSection ( CaptchaOptions . CaptchaSender ) ) ;
129144 builder . Services . AddSingleton < ISiteMappingService , SiteMappingService > ( ) ;
130145 builder . Services . AddHostedService < DatabaseMigrationService > ( ) ;
131- builder . Services . AddScoped < IReferralService , ReferralService > ( ) ;
146+ builder . Services . AddScoped < IReferralService , ReferralService > ( ) ;
132147
133148 if ( ! builder . Environment . IsDevelopment ( ) )
134149 {
@@ -161,11 +176,12 @@ private static void Main(string[] args)
161176 }
162177
163178
164-
179+
165180 WebApplication app = builder . Build ( ) ;
166- // Configure the HTTP request pipeline.
167- if ( ! app . Environment . IsDevelopment ( ) )
168- {
181+
182+ // Configure the HTTP request pipeline.
183+ if ( ! app . Environment . IsDevelopment ( ) )
184+ {
169185 app . UseExceptionHandler ( "/Error" ) ;
170186 app . UseForwardedHeaders ( ) ;
171187 app . UseHsts ( ) ;
@@ -176,25 +192,25 @@ private static void Main(string[] args)
176192 {
177193 app . UseDeveloperExceptionPage ( ) ;
178194 app . UseForwardedHeaders ( ) ;
179- }
195+ }
180196
181197 app . MapHealthChecks ( "/healthz" ) ;
182-
183- app . UseHttpsRedirection ( ) ;
184- app . UseStaticFiles ( ) ;
185-
198+
199+ app . UseHttpsRedirection ( ) ;
200+ app . UseStaticFiles ( ) ;
201+
186202 app . UseRouting ( ) ;
187203
188- app . UseAuthentication ( ) ;
204+ app . UseAuthentication ( ) ;
189205 app . UseAuthorization ( ) ;
190206 app . UseMiddleware < ReferralMiddleware > ( ) ;
191-
192-
193- app . MapRazorPages ( ) ;
207+
208+
209+ app . MapRazorPages ( ) ;
194210 app . MapDefaultControllerRoute ( ) ;
195211
196- app . MapFallbackToController ( "Index" , "Home" ) ;
197-
198- app . Run ( ) ;
212+ app . MapFallbackToController ( "Index" , "Home" ) ;
213+
214+ app . Run ( ) ;
199215 }
200- }
216+ }
0 commit comments