diff --git a/docs/en/framework/infrastructure/audit-logging.md b/docs/en/framework/infrastructure/audit-logging.md index f04bfafbe5..3392f5ac35 100644 --- a/docs/en/framework/infrastructure/audit-logging.md +++ b/docs/en/framework/infrastructure/audit-logging.md @@ -106,6 +106,24 @@ Configure(options => `IgnoredUrls` is the only option. It is a list of ignored URLs prefixes. In the preceding example, all URLs starting with `/products` will be ignored for audit logging. +## AbpAspNetCoreAuditingUrlOptions + +`AbpAspNetCoreAuditingUrlOptions` is the [options object](../fundamentals/options.md) to configure audit logging in the ASP.NET Core layer. You can configure it in the `ConfigureServices` method of your [module](../architecture/modularity/basics.md): + +````csharp +Configure(options => +{ + options.IncludeQuery = true; +}); +```` + +Here, a list of the options you can configure: + +* `IncludeSchema` (default: `false`): If you set to true, it will include the schema in the URL. +* `IncludeHost` (default: `false`): If you set to true, it will include the host in the URL. +* `IncludeQuery` (default: `false`): If you set to true, it will include the query string in the URL. + + ## Enabling/Disabling Audit Logging for Services ### Enable/Disable for Controllers & Actions diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingUrlOptions.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingUrlOptions.cs new file mode 100644 index 0000000000..7d371aa331 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingUrlOptions.cs @@ -0,0 +1,10 @@ +namespace Volo.Abp.AspNetCore.Auditing; + +public class AbpAspNetCoreAuditingUrlOptions +{ + public bool IncludeSchema { get; set; } + + public bool IncludeHost { get; set; } + + public bool IncludeQuery { get; set; } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs index fc010f9846..becdee806d 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs @@ -1,9 +1,11 @@ using System; using System.Linq; +using System.Text; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.Auditing; @@ -40,7 +42,7 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe if (context.AuditInfo.Url == null) { - context.AuditInfo.Url = BuildUrl(httpContext); + context.AuditInfo.Url = GetUrl(context, httpContext); } var clientInfoProvider = context.ServiceProvider.GetRequiredService(); @@ -88,18 +90,29 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe context.AuditInfo.HttpStatusCode = httpContext.Response.StatusCode; } - protected virtual string BuildUrl(HttpContext httpContext) + protected virtual string GetUrl(AuditLogContributionContext context, HttpContext httpContext) { - //TODO: Add options to include/exclude query, schema and host + var options = context.ServiceProvider.GetRequiredService>(); + var stringBuilder = new StringBuilder(); - var uriBuilder = new UriBuilder + if (options.Value.IncludeSchema) { - Scheme = httpContext.Request.Scheme, - Host = httpContext.Request.Host.Host, - Path = httpContext.Request.Path.ToString(), - Query = httpContext.Request.QueryString.ToString() - }; - - return uriBuilder.Uri.AbsolutePath; + stringBuilder.Append(httpContext.Request.Scheme); + stringBuilder.Append("://"); + } + + if (options.Value.IncludeHost) + { + stringBuilder.Append(httpContext.Request.Host.Host); + } + + stringBuilder.Append(httpContext.Request.Path.ToString()); + + if (options.Value.IncludeQuery) + { + stringBuilder.Append(httpContext.Request.QueryString.ToString()); + } + + return stringBuilder.ToString(); } }