From 9354f0fd50c3881e326013226d4d8380175a8e07 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 10 Dec 2024 15:17:28 +0800 Subject: [PATCH 1/3] Add options to include/exclude query schema and host --- .../AbpAspNetCoreAuditingUrlOptions.cs | 10 ++++++ .../Auditing/AspNetCoreAuditLogContributor.cs | 32 +++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingUrlOptions.cs 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..f649bb70d8 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 = BuildUrl(context, httpContext); } var clientInfoProvider = context.ServiceProvider.GetRequiredService(); @@ -88,10 +90,10 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe context.AuditInfo.HttpStatusCode = httpContext.Response.StatusCode; } - protected virtual string BuildUrl(HttpContext httpContext) + protected virtual string BuildUrl(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 { Scheme = httpContext.Request.Scheme, @@ -99,7 +101,25 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe Path = httpContext.Request.Path.ToString(), Query = httpContext.Request.QueryString.ToString() }; - - return uriBuilder.Uri.AbsolutePath; + + if (options.Value.IncludeSchema) + { + stringBuilder.Append(uriBuilder.Scheme); + stringBuilder.Append("://"); + } + + if (options.Value.IncludeHost) + { + stringBuilder.Append(uriBuilder.Host); + } + + stringBuilder.Append(uriBuilder.Path); + + if (options.Value.IncludeQuery) + { + stringBuilder.Append(uriBuilder.Query); + } + + return stringBuilder.ToString(); } } From 1946e5e1539a1f5b4483b6eaad8d6ff01ef068fd Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 10 Dec 2024 16:46:47 +0800 Subject: [PATCH 2/3] Update AspNetCoreAuditLogContributor.cs --- .../Auditing/AspNetCoreAuditLogContributor.cs | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) 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 f649bb70d8..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 @@ -42,7 +42,7 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe if (context.AuditInfo.Url == null) { - context.AuditInfo.Url = BuildUrl(context, httpContext); + context.AuditInfo.Url = GetUrl(context, httpContext); } var clientInfoProvider = context.ServiceProvider.GetRequiredService(); @@ -90,34 +90,27 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe context.AuditInfo.HttpStatusCode = httpContext.Response.StatusCode; } - protected virtual string BuildUrl(AuditLogContributionContext context, HttpContext httpContext) + protected virtual string GetUrl(AuditLogContributionContext context, HttpContext httpContext) { var options = context.ServiceProvider.GetRequiredService>(); var stringBuilder = new StringBuilder(); - var uriBuilder = new UriBuilder - { - Scheme = httpContext.Request.Scheme, - Host = httpContext.Request.Host.Host, - Path = httpContext.Request.Path.ToString(), - Query = httpContext.Request.QueryString.ToString() - }; - + if (options.Value.IncludeSchema) { - stringBuilder.Append(uriBuilder.Scheme); + stringBuilder.Append(httpContext.Request.Scheme); stringBuilder.Append("://"); } if (options.Value.IncludeHost) { - stringBuilder.Append(uriBuilder.Host); + stringBuilder.Append(httpContext.Request.Host.Host); } - stringBuilder.Append(uriBuilder.Path); + stringBuilder.Append(httpContext.Request.Path.ToString()); if (options.Value.IncludeQuery) { - stringBuilder.Append(uriBuilder.Query); + stringBuilder.Append(httpContext.Request.QueryString.ToString()); } return stringBuilder.ToString(); From eb2a8ad42e9a0e87882d8369cd9654059a0551b7 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 10 Dec 2024 17:10:43 +0800 Subject: [PATCH 3/3] update audit logging document --- .../framework/infrastructure/audit-logging.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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