|
26 | 26 | using EdFi.Security.DataAccess.Contexts; |
27 | 27 | using FluentValidation; |
28 | 28 | using FluentValidation.AspNetCore; |
| 29 | +using log4net; |
| 30 | +using log4net.Config; |
29 | 31 | using Microsoft.AspNetCore.Http.Json; |
30 | 32 | using Microsoft.AspNetCore.RateLimiting; |
31 | 33 | using Microsoft.EntityFrameworkCore; |
@@ -169,10 +171,6 @@ public static void AddServices(this WebApplicationBuilder webApplicationBuilder) |
169 | 171 | }); |
170 | 172 | }); |
171 | 173 |
|
172 | | - // Logging |
173 | | - var loggingOptions = config.GetSection("Log4NetCore").Get<Log4NetProviderOptions>(); |
174 | | - webApplicationBuilder.Logging.AddLog4Net(loggingOptions); |
175 | | - |
176 | 174 | // Fluent validation |
177 | 175 | webApplicationBuilder |
178 | 176 | .Services.AddValidatorsFromAssembly(assembly) |
@@ -213,6 +211,30 @@ public static void AddServices(this WebApplicationBuilder webApplicationBuilder) |
213 | 211 | webApplicationBuilder.Services.AddTransient<ITenantsService, TenantService>(); |
214 | 212 | } |
215 | 213 |
|
| 214 | + public static void AddLoggingServices(this WebApplicationBuilder webApplicationBuilder) |
| 215 | + { |
| 216 | + ConfigurationManager config = webApplicationBuilder.Configuration; |
| 217 | + |
| 218 | + // Remove all default logging providers (Console, Debug, etc.) |
| 219 | + webApplicationBuilder.Logging.ClearProviders(); |
| 220 | + |
| 221 | + // Initialize log4net early so we can use it in Program.cs |
| 222 | + var log4netConfigFileName = webApplicationBuilder.Configuration.GetValue<string>("Log4NetCore:Log4NetConfigFileName"); |
| 223 | + if (!string.IsNullOrEmpty(log4netConfigFileName)) |
| 224 | + { |
| 225 | + var log4netConfigPath = Path.Combine(AppContext.BaseDirectory, log4netConfigFileName); |
| 226 | + if (File.Exists(log4netConfigPath)) |
| 227 | + { |
| 228 | + var log4netConfig = new FileInfo(log4netConfigPath); |
| 229 | + XmlConfigurator.Configure(LogManager.GetRepository(), log4netConfig); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + // Important to display messages based on the Logging section in appsettings.json |
| 234 | + var loggingOptions = config.GetSection("Log4NetCore").Get<Log4NetProviderOptions>(); |
| 235 | + webApplicationBuilder.Logging.AddLog4Net(loggingOptions); |
| 236 | + } |
| 237 | + |
216 | 238 | private static void EnableMultiTenancySupport(this WebApplicationBuilder webApplicationBuilder) |
217 | 239 | { |
218 | 240 | webApplicationBuilder.Services.AddTransient< |
|
0 commit comments