Skip to content

Commit

Permalink
Merge pull request #18 from episerver/bugfix/CMS-23514-Search-doesnt-…
Browse files Browse the repository at this point in the history
…work-on-net6.0

Fix logger is not working
  • Loading branch information
hungoptimizely authored May 10, 2022
2 parents 032bde3 + 391c549 commit df719a3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/EPiServer.Search.IndexingService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace EPiServer.Search.IndexingService
{
Expand All @@ -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<Startup>());
}
}
17 changes: 9 additions & 8 deletions src/EPiServer.Search.IndexingService/Security/SecurityHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,37 @@ public class SecurityHandler : ISecurityHandler
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ClientElementHandler _clientElementHandler;

private readonly ILogger<SecurityHandler> _logger;
public SecurityHandler(IHttpContextAccessor httpContextAccessor,
ClientElementHandler clientElementHandler)
ClientElementHandler clientElementHandler, ILogger<SecurityHandler> 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;
}

Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/EPiServer.Search.IndexingService/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
Expand Down

0 comments on commit df719a3

Please sign in to comment.