From a55b4c5f63b9cc68a0279867fa744aa998f8826d Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 2 Sep 2024 09:12:32 +0200 Subject: [PATCH] chore(config): NLog-Konfiguration aus WebApiConfig entfernt --- HRD.WebApi/Configs/WebApiConfig.cs | 79 +------------------ HRD.WebApi/DAL/WebApiBaseContext.cs | 4 - HRD.WebApi/Middleware/WebApiMiddleware.cs | 30 ------- .../Middleware/WebApiMiddlewareExtensions.cs | 4 - .../Middleware/WebApiMiddlewareOptions.cs | 8 -- .../WebApiMiddlewareOptionsHelper.cs | 8 +- 6 files changed, 3 insertions(+), 130 deletions(-) diff --git a/HRD.WebApi/Configs/WebApiConfig.cs b/HRD.WebApi/Configs/WebApiConfig.cs index cc4ddf9..8aaed19 100644 --- a/HRD.WebApi/Configs/WebApiConfig.cs +++ b/HRD.WebApi/Configs/WebApiConfig.cs @@ -1,8 +1,6 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.Linq; namespace HRD.WebApi { @@ -18,17 +16,6 @@ namespace HRD.WebApi { private static string _dalConnectionstring; - internal static LogLevel GetMonitoringWebRequestLevel(string method, string path) - { - var entity = NlogMonitoringWebRequest.FirstOrDefault( - x => x.WebRequestMethod.Equals(method, StringComparison.OrdinalIgnoreCase) && - (x.WebRequestPath == "*" || path.StartsWith(x.WebRequestPath, StringComparison.OrdinalIgnoreCase)) - ); - if (entity == default) - return LogLevel.None; - return entity.LoggingLevel; - } - private static Dictionary _customConfig = new Dictionary(); public static int ConnectionstringTimeoutInMin { get; set; } = 5; @@ -44,32 +31,6 @@ namespace HRD.WebApi OfficeFileServerUrl = appSettings["AppConfig:OfficeFileServerUrl"]; IsLive = bool.Parse(appSettings["AppConfig:LDAP_WebAppGroup_Is_Live"]); - //LDAP - //LDAPService.LdapGlobals.LDAP_WebAppGroup_Is_Live = IsLive; - - //Nlog - NlogConnectionstring = appSettings["Nlog:NlogConnectionstring"]; - if (NlogConnectionstring.Equals("%sqlConnection%", StringComparison.OrdinalIgnoreCase)) - { - NlogConnectionstring = appSettings["ConnectionStrings:sqlConnection"]; - } - - NlogDBLogLevel = ToEnumLoggingLevel(appSettings["Nlog:NlogDBLogLevel"]); - NlogFileLogLevel = ToEnumLoggingLevel(appSettings["Nlog:NlogFileLogLevel"]); - NlogLogDirectory = appSettings["Nlog:NlogLogDirectory"]; - NlogSentryDSN = appSettings["Nlog:NlogSentryDSN"]; - NlogSentryIsEnable = bool.Parse(appSettings["Nlog:NlogSentryIsEnable"]); - - for (int i = 1; i < 100; i++) - { - string configString = appSettings[$"Nlog:MonitoringWebRequest{i}"]; - if (!string.IsNullOrEmpty(configString)) - { - string[] configArr = configString.Split("|").Select(x => x.Trim()).ToArray(); - NlogMonitoringWebRequest.Add(new MonitoringWebRequest { WebRequestMethod = configArr[0], WebRequestPath = configArr[1], LoggingLevel = ToEnumLoggingLevel(configArr[2]) }); - } - } - //custom var list = appSettings.GetSection("CustomConfig").GetChildren(); foreach (var item in list) @@ -118,43 +79,7 @@ namespace HRD.WebApi public static string ClientVersion { get; set; } public static bool IsLive { get; set; } - - //Logger - public static List NlogMonitoringWebRequest { get; set; } = new List(); - - public static string NlogLogDirectory { get; set; } = string.Empty; - - public static string NlogSentryDSN { get; set; } = string.Empty; - public static bool NlogSentryIsEnable { get; set; } = true; - - public static string NlogConnectionstring { get; set; } - public static LogLevel NlogFileLogLevel { get; set; } = LogLevel.Error; - public static LogLevel NlogDBLogLevel { get; set; } = LogLevel.Error; - - public static DbContextOptions SQLOptions() - { - if (string.IsNullOrEmpty(Connectionstring)) - { - throw new NullReferenceException("Connectnion string is empty!"); - } - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder - .EnableDetailedErrors() - .UseSqlServer(Connectionstring, - opts => - { - opts.CommandTimeout((int)TimeSpan.FromMinutes(ConnectionstringTimeoutInMin).TotalSeconds); - opts.EnableRetryOnFailure( - maxRetryCount: 5, - maxRetryDelay: TimeSpan.FromSeconds(30), - errorNumbersToAdd: null - ); - } - ); - - return optionsBuilder.Options; - } - + public static string ConnectionString(EN_ConnectionType connectionType = EN_ConnectionType.SQLServer) { switch (connectionType) diff --git a/HRD.WebApi/DAL/WebApiBaseContext.cs b/HRD.WebApi/DAL/WebApiBaseContext.cs index 8622517..d5343c9 100644 --- a/HRD.WebApi/DAL/WebApiBaseContext.cs +++ b/HRD.WebApi/DAL/WebApiBaseContext.cs @@ -4,10 +4,6 @@ namespace HRD.WebApi.DAL { public abstract class WebApiBaseContext : DbContext { - public WebApiBaseContext() : base(WebApiConfig.SQLOptions()) - { - } - public WebApiBaseContext(DbContextOptions options) : base(options) { diff --git a/HRD.WebApi/Middleware/WebApiMiddleware.cs b/HRD.WebApi/Middleware/WebApiMiddleware.cs index 39fb058..1bc9d62 100644 --- a/HRD.WebApi/Middleware/WebApiMiddleware.cs +++ b/HRD.WebApi/Middleware/WebApiMiddleware.cs @@ -21,35 +21,5 @@ namespace HRD.WebApi.DAL.Middleware _next = next; _logger = logger; } - - public async Task InvokeAsync(HttpContext httpContext) - { - try - { - var loglevel = WebApiConfig.GetMonitoringWebRequestLevel(httpContext.Request.Method, httpContext.Request.Path.Value); - if (loglevel == LogLevel.Information - || loglevel == LogLevel.Warning - || loglevel == LogLevel.Error - ) - { - var requestReader = new StreamReader(httpContext.Request.Body); - var requestContent = await requestReader.ReadToEndAsync(); - if (string.IsNullOrEmpty(requestContent)) - { - requestContent = $"{httpContext.Request.Method} {httpContext.Request.Path.Value}"; - } - - if (loglevel == LogLevel.Information) { _logger.LogInformation(requestContent); } - if (loglevel == LogLevel.Warning) { _logger.LogWarning(requestContent); } - if (loglevel == LogLevel.Error) { _logger.LogError(requestContent); } - } - } - catch (System.Exception) - { - //throw; - } - - await _next(httpContext).ConfigureAwait(false); - } } } \ No newline at end of file diff --git a/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs b/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs index e8b747d..efb463d 100644 --- a/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs +++ b/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs @@ -27,10 +27,6 @@ namespace HRD.WebApi.DAL.Middleware WebApiConfig.AssemblyName = options.AssemblyName; WebApiConfig.ClientVersion = options.ClientVersion; WebApiConfig.Connectionstring = options.Connectionstring; - WebApiConfig.NlogConnectionstring = options.NlogConnectionstring; - WebApiConfig.NlogDBLogLevel = options.NlogDBLogLevel; - WebApiConfig.NlogFileLogLevel = options.NlogFileLogLevel; - WebApiConfig.NlogLogDirectory = options.NlogLogDirectory; } } } \ No newline at end of file diff --git a/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs b/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs index 77b5a20..96b4226 100644 --- a/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs +++ b/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs @@ -10,13 +10,5 @@ namespace HRD.WebApi.DAL.Middleware public string ClientVersion { get; set; } public string Connectionstring { get; set; } = String.Empty; - - //Logger - public string NlogLogDirectory { get; set; } = String.Empty; - - public string NlogConnectionstring { get; set; } = String.Empty; - - public LogLevel NlogFileLogLevel { get; set; } = LogLevel.Error; - public LogLevel NlogDBLogLevel { get; set; } = LogLevel.Error; } } \ No newline at end of file diff --git a/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs b/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs index 23d90c0..acf12e7 100644 --- a/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs +++ b/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs @@ -12,13 +12,7 @@ namespace StaffDBServer.SharedExtensions AssemblyVersion = WebApiConfig.AssemblyVersion, AssemblyName = WebApiConfig.AssemblyName, ClientVersion = WebApiConfig.ClientVersion, - - Connectionstring = WebApiConfig.Connectionstring, - - NlogConnectionstring = WebApiConfig.NlogConnectionstring, - NlogDBLogLevel = WebApiConfig.NlogDBLogLevel, - NlogFileLogLevel = WebApiConfig.NlogFileLogLevel, - NlogLogDirectory = WebApiConfig.NlogLogDirectory + Connectionstring = WebApiConfig.Connectionstring }; return options; }