Browse Source

Resolved #784: Add ClientId & CorrelationId to audit logs.

pull/786/head^2
Halil ibrahim Kalkan 7 years ago
parent
commit
bcb1aefb19
  1. 43
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreCorrelationIdProvider.cs
  2. 4
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs
  3. 11
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs
  4. 7
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICorrelationIdProvider.cs
  5. 12
      framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/NullCorrelationIdProvider.cs
  6. 3
      framework/src/Volo.Abp.Http/Volo/Abp/Http/AbpHttpModule.cs
  7. 4
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/AuditLogConsts.cs
  8. 6
      modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs
  9. 2
      modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/AbpAuditLoggingtDbContextModelBuilderExtensions.cs
  10. 1150
      samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/20190208210234_Added_ClientId_And_CorrelationId_To_AuditLogs.Designer.cs
  11. 72
      samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/20190208210234_Added_ClientId_And_CorrelationId_To_AuditLogs.cs
  12. 10
      samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/AuthServerDbContextModelSnapshot.cs

43
framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AspNetCoreCorrelationIdProvider.cs

@ -0,0 +1,43 @@
using System;
using Microsoft.AspNetCore.Http;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Auditing
{
[Dependency(ReplaceServices = true)]
public class AspNetCoreCorrelationIdProvider : ICorrelationIdProvider, ITransientDependency
{
public const string CorrelationIdKey = "_CorrelationId";
protected IHttpContextAccessor HttpContextAccessor { get; }
public AspNetCoreCorrelationIdProvider(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
public virtual string Get()
{
if (HttpContextAccessor.HttpContext?.Request?.Headers == null)
{
return CreateNewCorrelationId();
}
string correlationId = HttpContextAccessor.HttpContext.Request.Headers[CorrelationIdKey];
if (correlationId.IsNullOrEmpty())
{
correlationId = CreateNewCorrelationId();
HttpContextAccessor.HttpContext.Request.Headers[CorrelationIdKey] = correlationId;
}
return correlationId;
}
protected virtual string CreateNewCorrelationId()
{
return Guid.NewGuid().ToString("N");
}
}
}

4
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditLogInfo.cs

@ -24,6 +24,10 @@ namespace Volo.Abp.Auditing
public int ExecutionDuration { get; set; }
public string ClientId { get; set; }
public string CorrelationId { get; set; }
public string ClientIpAddress { get; set; }
public string ClientName { get; set; }

11
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditingHelper.cs

@ -5,6 +5,7 @@ using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.Clients;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Timing;
@ -18,30 +19,36 @@ namespace Volo.Abp.Auditing
protected IAuditingStore AuditingStore { get; }
protected ICurrentUser CurrentUser { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ICurrentClient CurrentClient { get; }
protected IClock Clock { get; }
protected AbpAuditingOptions Options;
protected IAuditSerializer AuditSerializer;
protected IServiceProvider ServiceProvider;
protected ICorrelationIdProvider CorrelationIdProvider { get; }
public AuditingHelper(
IAuditSerializer auditSerializer,
IOptions<AbpAuditingOptions> options,
ICurrentUser currentUser,
ICurrentTenant currentTenant,
ICurrentClient currentClient,
IClock clock,
IAuditingStore auditingStore,
ILogger<AuditingHelper> logger,
IServiceProvider serviceProvider)
IServiceProvider serviceProvider,
ICorrelationIdProvider correlationIdProvider)
{
Options = options.Value;
AuditSerializer = auditSerializer;
CurrentUser = currentUser;
CurrentTenant = currentTenant;
CurrentClient = currentClient;
Clock = clock;
AuditingStore = auditingStore;
Logger = logger;
ServiceProvider = serviceProvider;
CorrelationIdProvider = correlationIdProvider;
}
public virtual bool ShouldSaveAudit(MethodInfo methodInfo, bool defaultValue = false)
@ -85,6 +92,8 @@ namespace Volo.Abp.Auditing
TenantId = CurrentTenant.Id,
UserId = CurrentUser.Id,
UserName = CurrentUser.UserName,
ClientId = CurrentClient.Id,
CorrelationId = CorrelationIdProvider.Get(),
//ImpersonatorUserId = AbpSession.ImpersonatorUserId, //TODO: Impersonation system is not available yet!
//ImpersonatorTenantId = AbpSession.ImpersonatorTenantId,
ExecutionTime = Clock.Now

7
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICorrelationIdProvider.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Auditing
{
public interface ICorrelationIdProvider
{
string Get();
}
}

12
framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/NullCorrelationIdProvider.cs

@ -0,0 +1,12 @@
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Auditing
{
public class NullCorrelationIdProvider : ICorrelationIdProvider, ISingletonDependency
{
public string Get()
{
return null;
}
}
}

3
framework/src/Volo.Abp.Http/Volo/Abp/Http/AbpHttpModule.cs

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Http.ProxyScripting.Configuration;
using Volo.Abp.Http.ProxyScripting.Configuration;
using Volo.Abp.Http.ProxyScripting.Generators.JQuery;
using Volo.Abp.Json;
using Volo.Abp.Modularity;

4
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain.Shared/Volo/Abp/AuditLogging/AuditLogConsts.cs

@ -6,6 +6,10 @@
public const int MaxClientNameLength = 128;
public const int MaxClientIdLength = 64;
public const int MaxCorrelationIdLength = 64;
public const int MaxBrowserInfoLength = 512;
public const int MaxExceptionsLength = 4000;

6
modules/audit-logging/src/Volo.Abp.AuditLogging.Domain/Volo/Abp/AuditLogging/AuditLog.cs

@ -30,6 +30,10 @@ namespace Volo.Abp.AuditLogging
public virtual string ClientName { get; protected set; }
public string ClientId { get; set; }
public string CorrelationId { get; set; }
public virtual string BrowserInfo { get; protected set; }
public virtual string HttpMethod { get; protected set; }
@ -61,6 +65,8 @@ namespace Volo.Abp.AuditLogging
ExecutionDuration = auditInfo.ExecutionDuration;
ClientIpAddress = auditInfo.ClientIpAddress.Truncate(AuditLogConsts.MaxClientIpAddressLength);
ClientName = auditInfo.ClientName.Truncate(AuditLogConsts.MaxClientNameLength);
ClientId = auditInfo.ClientId.Truncate(AuditLogConsts.MaxClientIdLength);
CorrelationId = auditInfo.CorrelationId.Truncate(AuditLogConsts.MaxCorrelationIdLength);
BrowserInfo = auditInfo.BrowserInfo.Truncate(AuditLogConsts.MaxBrowserInfoLength);
HttpMethod = auditInfo.HttpMethod.Truncate(AuditLogConsts.MaxHttpMethodLength);
Url = auditInfo.Url.Truncate(AuditLogConsts.MaxUrlLength);

2
modules/audit-logging/src/Volo.Abp.AuditLogging.EntityFrameworkCore/Volo/Abp/AuditLogging/EntityFrameworkCore/AbpAuditLoggingtDbContextModelBuilderExtensions.cs

@ -26,6 +26,8 @@ namespace Volo.Abp.AuditLogging.EntityFrameworkCore
b.Property(x => x.ClientIpAddress).HasMaxLength(AuditLogConsts.MaxClientIpAddressLength).HasColumnName(nameof(AuditLog.ClientIpAddress));
b.Property(x => x.ClientName).HasMaxLength(AuditLogConsts.MaxClientNameLength).HasColumnName(nameof(AuditLog.ClientName));
b.Property(x => x.ClientId).HasMaxLength(AuditLogConsts.MaxClientIdLength).HasColumnName(nameof(AuditLog.ClientId));
b.Property(x => x.CorrelationId).HasMaxLength(AuditLogConsts.MaxCorrelationIdLength).HasColumnName(nameof(AuditLog.CorrelationId));
b.Property(x => x.BrowserInfo).HasMaxLength(AuditLogConsts.MaxBrowserInfoLength).HasColumnName(nameof(AuditLog.BrowserInfo));
b.Property(x => x.HttpMethod).HasMaxLength(AuditLogConsts.MaxHttpMethodLength).HasColumnName(nameof(AuditLog.HttpMethod));
b.Property(x => x.Url).HasMaxLength(AuditLogConsts.MaxUrlLength).HasColumnName(nameof(AuditLog.Url));

1150
samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/20190208210234_Added_ClientId_And_CorrelationId_To_AuditLogs.Designer.cs

File diff suppressed because it is too large

72
samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/20190208210234_Added_ClientId_And_CorrelationId_To_AuditLogs.cs

@ -0,0 +1,72 @@
using System.Reflection.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
namespace AuthServer.Host.Migrations
{
public partial class Added_ClientId_And_CorrelationId_To_AuditLogs : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
"PK_IdentityServerClientPostLogoutRedirectUris",
"IdentityServerClientPostLogoutRedirectUris"
);
migrationBuilder.AlterColumn<string>(
name: "PostLogoutRedirectUri",
table: "IdentityServerClientPostLogoutRedirectUris",
maxLength: 200,
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 2000);
migrationBuilder.AddPrimaryKey(
"PK_IdentityServerClientPostLogoutRedirectUris",
"IdentityServerClientPostLogoutRedirectUris",
new[] {"ClientId", "PostLogoutRedirectUri"}
);
migrationBuilder.AddColumn<string>(
name: "ClientId",
table: "AbpAuditLogs",
maxLength: 64,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "CorrelationId",
table: "AbpAuditLogs",
maxLength: 64,
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ClientId",
table: "AbpAuditLogs");
migrationBuilder.DropColumn(
name: "CorrelationId",
table: "AbpAuditLogs");
migrationBuilder.DropPrimaryKey(
"PK_IdentityServerClientPostLogoutRedirectUris",
"IdentityServerClientPostLogoutRedirectUris"
);
migrationBuilder.AlterColumn<string>(
name: "PostLogoutRedirectUri",
table: "IdentityServerClientPostLogoutRedirectUris",
maxLength: 2000,
nullable: false,
oldClrType: typeof(string),
oldMaxLength: 200);
migrationBuilder.AddPrimaryKey(
"PK_IdentityServerClientPostLogoutRedirectUris",
"IdentityServerClientPostLogoutRedirectUris",
new[] { "ClientId", "PostLogoutRedirectUri" }
);
}
}
}

10
samples/MicroserviceDemo/applications/AuthServer.Host/Migrations/AuthServerDbContextModelSnapshot.cs

@ -28,6 +28,10 @@ namespace AuthServer.Host.Migrations
.HasColumnName("BrowserInfo")
.HasMaxLength(512);
b.Property<string>("ClientId")
.HasColumnName("ClientId")
.HasMaxLength(64);
b.Property<string>("ClientIpAddress")
.HasColumnName("ClientIpAddress")
.HasMaxLength(64);
@ -42,6 +46,10 @@ namespace AuthServer.Host.Migrations
b.Property<string>("ConcurrencyStamp");
b.Property<string>("CorrelationId")
.HasColumnName("CorrelationId")
.HasMaxLength(64);
b.Property<string>("Exceptions")
.HasColumnName("Exceptions")
.HasMaxLength(4000);
@ -753,7 +761,7 @@ namespace AuthServer.Host.Migrations
b.Property<Guid>("ClientId");
b.Property<string>("PostLogoutRedirectUri")
.HasMaxLength(2000);
.HasMaxLength(200);
b.HasKey("ClientId", "PostLogoutRedirectUri");

Loading…
Cancel
Save