From 391c549b3e4e6bd99c33b2804ffd8a52d290ca8f Mon Sep 17 00:00:00 2001 From: "EP\\hunng" Date: Tue, 10 May 2022 10:50:22 +0700 Subject: [PATCH] Fix logger is not working Fixes: CMS-23514 --- src/EPiServer.Search.IndexingService/Program.cs | 6 ++++++ .../Security/SecurityHandler.cs | 17 +++++++++-------- .../appsettings.json | 3 +-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/EPiServer.Search.IndexingService/Program.cs b/src/EPiServer.Search.IndexingService/Program.cs index 99dd502..42e4bd7 100644 --- a/src/EPiServer.Search.IndexingService/Program.cs +++ b/src/EPiServer.Search.IndexingService/Program.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; namespace EPiServer.Search.IndexingService { @@ -9,6 +10,11 @@ public class Program public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) + .ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.AddConsole(); + }) .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup()); } } diff --git a/src/EPiServer.Search.IndexingService/Security/SecurityHandler.cs b/src/EPiServer.Search.IndexingService/Security/SecurityHandler.cs index 566aca3..92ef03b 100644 --- a/src/EPiServer.Search.IndexingService/Security/SecurityHandler.cs +++ b/src/EPiServer.Search.IndexingService/Security/SecurityHandler.cs @@ -8,36 +8,37 @@ public class SecurityHandler : ISecurityHandler { private readonly IHttpContextAccessor _httpContextAccessor; private readonly ClientElementHandler _clientElementHandler; - + private readonly ILogger _logger; public SecurityHandler(IHttpContextAccessor httpContextAccessor, - ClientElementHandler clientElementHandler) + ClientElementHandler clientElementHandler, ILogger logger) { _httpContextAccessor = httpContextAccessor; _clientElementHandler = clientElementHandler; + _logger = logger; } public bool IsAuthenticated(string accessKey, AccessLevel accessLevel) { - IndexingServiceSettings.IndexingServiceServiceLog.LogDebug(string.Format("Request for authorization for access key '{0}'", accessKey)); + _logger.LogDebug("Request for authorization for access key '{AccessKey}'", accessKey); //Always fail if no client access key is found in the request if (string.IsNullOrEmpty(accessKey)) { - IndexingServiceSettings.IndexingServiceServiceLog.LogError("No access key found. Access denied."); + _logger.LogError("No access key found. Access denied."); return false; } //Check if the access key exists and get the client element if (!IndexingServiceSettings.ClientElements.TryGetValue(accessKey, out var elem)) { - IndexingServiceSettings.IndexingServiceServiceLog.LogError(string.Format("The access key: '{0}' was not found for configured clients. Access denied.", accessKey)); + _logger.LogError(string.Format("The access key: '{0}' was not found for configured clients. Access denied.", accessKey)); return false; } //Check level. if (elem.ReadOnly && accessLevel == AccessLevel.Modify) { - IndexingServiceSettings.IndexingServiceServiceLog.LogError(string.Format("Modify request for access key '{0}' failed. Only read access", accessKey)); + _logger.LogError(string.Format("Modify request for access key '{0}' failed. Only read access", accessKey)); return false; } @@ -50,11 +51,11 @@ public bool IsAuthenticated(string accessKey, AccessLevel accessLevel) if (!_clientElementHandler.IsIPAddressAllowed(elem, remoteIpAddress)) { - IndexingServiceSettings.IndexingServiceServiceLog.LogError(string.Format("No match for client IP {0}. Access denied for access key {1}.", remoteIpAddress, accessKey)); + _logger.LogError(string.Format("No match for client IP {0}. Access denied for access key {1}.", remoteIpAddress, accessKey)); return false; } - IndexingServiceSettings.IndexingServiceServiceLog.LogDebug(string.Format("Request for authorization for access key '{0}' succeded", accessKey)); + _logger.LogDebug(string.Format("Request for authorization for access key '{0}' succeded", accessKey)); return true; } diff --git a/src/EPiServer.Search.IndexingService/appsettings.json b/src/EPiServer.Search.IndexingService/appsettings.json index 0bf1c76..b173d6e 100644 --- a/src/EPiServer.Search.IndexingService/appsettings.json +++ b/src/EPiServer.Search.IndexingService/appsettings.json @@ -2,8 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*",