diff --git a/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsService.cs b/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsService.cs
index 6af9dafc7d..87343b2f9f 100644
--- a/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsService.cs
+++ b/src/Microsoft.AspNetCore.Cors/Infrastructure/CorsService.cs
@@ -5,9 +5,11 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
+using Microsoft.AspNetCore.Cors.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
+using Microsoft.Extensions.Logging;
namespace Microsoft.AspNetCore.Cors.Infrastructure
{
@@ -17,12 +19,23 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
public class CorsService : ICorsService
{
private readonly CorsOptions _options;
+ private readonly ILogger _logger;
///
/// Creates a new instance of the .
///
/// The option model representing .
public CorsService(IOptions options)
+ :this(options, loggerFactory: null)
+ {
+ }
+
+ ///
+ /// Creates a new instance of the .
+ ///
+ /// The option model representing .
+ /// The .
+ public CorsService(IOptions options, ILoggerFactory loggerFactory)
{
if (options == null)
{
@@ -30,6 +43,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
}
_options = options.Value;
+ _logger = loggerFactory?.CreateLogger();
}
///
@@ -70,6 +84,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
!StringValues.IsNullOrEmpty(accessControlRequestMethod))
{
EvaluatePreflightRequest(context, policy, corsResult);
+ _logger?.IsPreflightRequest();
}
else
{
@@ -87,6 +102,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
return;
}
+ _logger?.RequestHasOriginHeader();
AddOriginToResult(origin, policy, result);
result.SupportsCredentials = policy.SupportsCredentials;
AddHeaderValues(result.AllowedExposedHeaders, policy.ExposedHeaders);
@@ -100,6 +116,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
return;
}
+ _logger?.RequestHasOriginHeader();
var accessControlRequestMethod = context.Request.Headers[CorsConstants.AccessControlRequestMethod];
if (StringValues.IsNullOrEmpty(accessControlRequestMethod))
{
diff --git a/src/Microsoft.AspNetCore.Cors/Internal/CORSLoggerExtensions.cs b/src/Microsoft.AspNetCore.Cors/Internal/CORSLoggerExtensions.cs
new file mode 100644
index 0000000000..7672797458
--- /dev/null
+++ b/src/Microsoft.AspNetCore.Cors/Internal/CORSLoggerExtensions.cs
@@ -0,0 +1,58 @@
+// Copyright (c) .NET Foundation. All rights reserved.
+// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
+
+using System;
+using Microsoft.Extensions.Logging;
+
+namespace Microsoft.AspNetCore.Cors.Internal
+{
+ internal static class CORSLoggerExtensions
+ {
+ private static readonly Action _isPreflightRequest;
+ private static readonly Action _requestHasOriginHeader;
+ private static readonly Action _policySuccess;
+ private static readonly Action _policyFailure;
+
+ static CORSLoggerExtensions()
+ {
+ _isPreflightRequest = LoggerMessage.Define(
+ LogLevel.Debug,
+ 1,
+ "The request is preflight.");
+
+ _requestHasOriginHeader = LoggerMessage.Define(
+ LogLevel.Debug,
+ 2,
+ "The request has an origin header.");
+
+ _policySuccess = LoggerMessage.Define(
+ LogLevel.Information,
+ 3,
+ "Policy execution successful.");
+
+ _policyFailure = LoggerMessage.Define(
+ LogLevel.Information,
+ 3,
+ "Policy execution failed. '{FailureReason}'");
+ }
+
+ public static void IsPreflightRequest(this ILogger logger)
+ {
+ _isPreflightRequest(logger, null);
+ }
+
+ public static void RequestHasOriginHeader(this ILogger logger)
+ {
+ _requestHasOriginHeader(logger, null);
+ }
+
+ public static void PolicySuccess(this ILogger logger, string failureReason)
+ {
+ _policySuccess(logger, null);
+ }
+ public static void PolicyFailure(this ILogger logger, string failureReason)
+ {
+ _policyFailure(logger, failureReason, null);
+ }
+ }
+}
diff --git a/test/Microsoft.AspNetCore.Cors.Test/CorsServiceTests.cs b/test/Microsoft.AspNetCore.Cors.Test/CorsServiceTests.cs
index c88eccd6fa..b3a92a8d68 100644
--- a/test/Microsoft.AspNetCore.Cors.Test/CorsServiceTests.cs
+++ b/test/Microsoft.AspNetCore.Cors.Test/CorsServiceTests.cs
@@ -446,7 +446,7 @@ namespace Microsoft.AspNetCore.Cors.Infrastructure
}
[Fact]
- public void EaluatePolicy_DoesCaseSensitiveComparison()
+ public void EvaluatePolicy_DoesCaseSensitiveComparison()
{
// Arrange
var corsService = new CorsService(new TestCorsOptions());