Browse Source

Merge pull request #21586 from abpframework/auditlogging

Add options to include/exclude query schema and host
issue/abp-ng-upgrade
liangshiwei 1 year ago
committed by GitHub
parent
commit
c448b1e471
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 18
      docs/en/framework/infrastructure/audit-logging.md
  2. 10
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAspNetCoreAuditingUrlOptions.cs
  3. 35
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs

18
docs/en/framework/infrastructure/audit-logging.md

@ -106,6 +106,24 @@ Configure<AbpAspNetCoreAuditingOptions>(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. `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<AbpAspNetCoreAuditingUrlOptions>(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 ## Enabling/Disabling Audit Logging for Services
### Enable/Disable for Controllers & Actions ### Enable/Disable for Controllers & Actions

10
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; }
}

35
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreAuditLogContributor.cs

@ -1,9 +1,11 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.ExceptionHandling; using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.AspNetCore.WebClientInfo;
using Volo.Abp.Auditing; using Volo.Abp.Auditing;
@ -40,7 +42,7 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe
if (context.AuditInfo.Url == null) if (context.AuditInfo.Url == null)
{ {
context.AuditInfo.Url = BuildUrl(httpContext); context.AuditInfo.Url = GetUrl(context, httpContext);
} }
var clientInfoProvider = context.ServiceProvider.GetRequiredService<IWebClientInfoProvider>(); var clientInfoProvider = context.ServiceProvider.GetRequiredService<IWebClientInfoProvider>();
@ -88,18 +90,29 @@ public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDepe
context.AuditInfo.HttpStatusCode = httpContext.Response.StatusCode; 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<IOptions<AbpAspNetCoreAuditingUrlOptions>>();
var stringBuilder = new StringBuilder();
var uriBuilder = new UriBuilder if (options.Value.IncludeSchema)
{ {
Scheme = httpContext.Request.Scheme, stringBuilder.Append(httpContext.Request.Scheme);
Host = httpContext.Request.Host.Host, stringBuilder.Append("://");
Path = httpContext.Request.Path.ToString(), }
Query = httpContext.Request.QueryString.ToString()
}; if (options.Value.IncludeHost)
{
return uriBuilder.Uri.AbsolutePath; 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();
} }
} }

Loading…
Cancel
Save