无法使 EntityFramework 核心的审计工作
Cannot get Auditing of EntityFramework Core to work
首先,我安装了审核模板并使用 Audit.Net 模板创建了一个新项目。使用该项目作为指南,我尝试对我的实际项目实施审计,其中我有一个 Asp.Net 核心 API(项目名称:EcommerceAPI
)。它还使用 Entity Framework 核心(在图书馆项目中:Persistence
)。我只将这些 NuGet 包安装到电子商务API:
- Audit.EntityFramework.Core v18.1.3
- Audit.WebApi.Core v18.1.3
这是我的 AuditConfiguration.cs
文件:
public static class AuditConfiguration
{
private const string CorrelationIdField = "CorrelationId";
/// <summary>
/// Add the global audit filter to the MVC pipeline
/// </summary>
public static MvcOptions AddAudit(this MvcOptions mvcOptions)
{
// Configure the global Action Filter
mvcOptions.AddAuditFilter(a => a
.LogAllActions()
.WithEventType("MVC:{verb}:{controller}:{action}")
.IncludeModelState()
.IncludeRequestBody()
.IncludeResponseBody());
return mvcOptions;
}
/// <summary>
/// Global Audit configuration
/// </summary>
public static IServiceCollection ConfigureAudit(this IServiceCollection serviceCollection)
{
// TODO: Configure the audit data provider and options. For more info see https://github.com/thepirat000/Audit.NET#data-providers.
Audit.Core.Configuration.Setup()
.UseDynamicProvider(_ => _.OnInsert(auditEvent =>
{
if (auditEvent.Environment.Exception != null)
{
Log.Error("Audit Exception in {CallingMethodName}: {Exception}", auditEvent.Environment.CallingMethodName, auditEvent.Environment.Exception);
}
else
{
Log.Information("Audit Event: {EventType} {Duration}", auditEvent.EventType, auditEvent.Duration);
}
}))
.WithCreationPolicy(EventCreationPolicy.InsertOnEnd);
// Entity framework audit output configuration
Audit.EntityFramework.Configuration.Setup()
.ForContext<EntityContext>(_ => _
.AuditEventType("EF:{context}"))
.UseOptOut();
return serviceCollection;
}
public static void UseAuditMiddleware(this IApplicationBuilder app)
{
// Configure the Middleware
app.UseAuditMiddleware(_ => _
.FilterByRequest(r => !r.Path.Value.EndsWith("favicon.ico"))
.IncludeHeaders()
.IncludeRequestBody()
.IncludeResponseBody()
.WithEventType("HTTP:{verb}:{url}"));
}
/// <summary>
/// Add a RequestId so the audit events can be grouped per request
/// </summary>
public static void UseAuditCorrelationId(this IApplicationBuilder app, IHttpContextAccessor ctxAccesor)
{
Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var httpContext = ctxAccesor.HttpContext;
scope.Event.CustomFields[CorrelationIdField] = httpContext.TraceIdentifier;
});
}
}
Asp.Net 核心审核就好了。但是,我从 Entityframework 核心审核中一无所获。我是否需要更改我的 Persistence
图书馆项目中的任何内容才能使审核工作正常进行?我应该将 Audit.EntityFramework.Core
Nuget 包安装到库中吗?如果有人认为他们需要,我可以 post 我的 Startup.cs 文件。任何帮助将不胜感激。
O.K。我没有完全阅读文档。这是我对 RTFM 的失败。
我需要在 Persistence
上安装 Audit.EntityFramework.Core
并将我的 DbContext
更改为 AuditDbContext
。就这么简单。
首先,我安装了审核模板并使用 Audit.Net 模板创建了一个新项目。使用该项目作为指南,我尝试对我的实际项目实施审计,其中我有一个 Asp.Net 核心 API(项目名称:EcommerceAPI
)。它还使用 Entity Framework 核心(在图书馆项目中:Persistence
)。我只将这些 NuGet 包安装到电子商务API:
- Audit.EntityFramework.Core v18.1.3
- Audit.WebApi.Core v18.1.3
这是我的 AuditConfiguration.cs
文件:
public static class AuditConfiguration
{
private const string CorrelationIdField = "CorrelationId";
/// <summary>
/// Add the global audit filter to the MVC pipeline
/// </summary>
public static MvcOptions AddAudit(this MvcOptions mvcOptions)
{
// Configure the global Action Filter
mvcOptions.AddAuditFilter(a => a
.LogAllActions()
.WithEventType("MVC:{verb}:{controller}:{action}")
.IncludeModelState()
.IncludeRequestBody()
.IncludeResponseBody());
return mvcOptions;
}
/// <summary>
/// Global Audit configuration
/// </summary>
public static IServiceCollection ConfigureAudit(this IServiceCollection serviceCollection)
{
// TODO: Configure the audit data provider and options. For more info see https://github.com/thepirat000/Audit.NET#data-providers.
Audit.Core.Configuration.Setup()
.UseDynamicProvider(_ => _.OnInsert(auditEvent =>
{
if (auditEvent.Environment.Exception != null)
{
Log.Error("Audit Exception in {CallingMethodName}: {Exception}", auditEvent.Environment.CallingMethodName, auditEvent.Environment.Exception);
}
else
{
Log.Information("Audit Event: {EventType} {Duration}", auditEvent.EventType, auditEvent.Duration);
}
}))
.WithCreationPolicy(EventCreationPolicy.InsertOnEnd);
// Entity framework audit output configuration
Audit.EntityFramework.Configuration.Setup()
.ForContext<EntityContext>(_ => _
.AuditEventType("EF:{context}"))
.UseOptOut();
return serviceCollection;
}
public static void UseAuditMiddleware(this IApplicationBuilder app)
{
// Configure the Middleware
app.UseAuditMiddleware(_ => _
.FilterByRequest(r => !r.Path.Value.EndsWith("favicon.ico"))
.IncludeHeaders()
.IncludeRequestBody()
.IncludeResponseBody()
.WithEventType("HTTP:{verb}:{url}"));
}
/// <summary>
/// Add a RequestId so the audit events can be grouped per request
/// </summary>
public static void UseAuditCorrelationId(this IApplicationBuilder app, IHttpContextAccessor ctxAccesor)
{
Configuration.AddCustomAction(ActionType.OnScopeCreated, scope =>
{
var httpContext = ctxAccesor.HttpContext;
scope.Event.CustomFields[CorrelationIdField] = httpContext.TraceIdentifier;
});
}
}
Asp.Net 核心审核就好了。但是,我从 Entityframework 核心审核中一无所获。我是否需要更改我的 Persistence
图书馆项目中的任何内容才能使审核工作正常进行?我应该将 Audit.EntityFramework.Core
Nuget 包安装到库中吗?如果有人认为他们需要,我可以 post 我的 Startup.cs 文件。任何帮助将不胜感激。
O.K。我没有完全阅读文档。这是我对 RTFM 的失败。
我需要在 Persistence
上安装 Audit.EntityFramework.Core
并将我的 DbContext
更改为 AuditDbContext
。就这么简单。