From dddc01d24c8a9c6991285b69091d423fcf07dbad Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 2 Sep 2024 09:36:59 +0200 Subject: [PATCH] Revert "chore(config): NLog-Konfiguration aus WebApiConfig entfernt" This reverts commit a55b4c5f63b9cc68a0279867fa744aa998f8826d. --- 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, 130 insertions(+), 3 deletions(-) diff --git a/HRD.WebApi/Configs/WebApiConfig.cs b/HRD.WebApi/Configs/WebApiConfig.cs index 8aaed19..cc4ddf9 100644 --- a/HRD.WebApi/Configs/WebApiConfig.cs +++ b/HRD.WebApi/Configs/WebApiConfig.cs @@ -1,6 +1,8 @@ -using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Linq; namespace HRD.WebApi { @@ -16,6 +18,17 @@ 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; @@ -31,6 +44,32 @@ 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) @@ -79,7 +118,43 @@ 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 d5343c9..8622517 100644 --- a/HRD.WebApi/DAL/WebApiBaseContext.cs +++ b/HRD.WebApi/DAL/WebApiBaseContext.cs @@ -4,6 +4,10 @@ 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 1bc9d62..39fb058 100644 --- a/HRD.WebApi/Middleware/WebApiMiddleware.cs +++ b/HRD.WebApi/Middleware/WebApiMiddleware.cs @@ -21,5 +21,35 @@ 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 efb463d..e8b747d 100644 --- a/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs +++ b/HRD.WebApi/Middleware/WebApiMiddlewareExtensions.cs @@ -27,6 +27,10 @@ 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 96b4226..77b5a20 100644 --- a/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs +++ b/HRD.WebApi/Middleware/WebApiMiddlewareOptions.cs @@ -10,5 +10,13 @@ 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 acf12e7..23d90c0 100644 --- a/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs +++ b/StaffDBServer/_Shared/SharedExtensions/WebApiMiddlewareOptionsHelper.cs @@ -12,7 +12,13 @@ namespace StaffDBServer.SharedExtensions AssemblyVersion = WebApiConfig.AssemblyVersion, AssemblyName = WebApiConfig.AssemblyName, ClientVersion = WebApiConfig.ClientVersion, - Connectionstring = WebApiConfig.Connectionstring + + Connectionstring = WebApiConfig.Connectionstring, + + NlogConnectionstring = WebApiConfig.NlogConnectionstring, + NlogDBLogLevel = WebApiConfig.NlogDBLogLevel, + NlogFileLogLevel = WebApiConfig.NlogFileLogLevel, + NlogLogDirectory = WebApiConfig.NlogLogDirectory }; return options; }