Audit.NET icon indicating copy to clipboard operation
Audit.NET copied to clipboard

user cannot be converted to type system.string

Open gbdrm opened this issue 4 years ago • 3 comments

Time to time I have a runtime exception about converting to system.string: image

where ApplicationUser is the next class: public class ApplicationUser : IdentityUser { }

I can not find any particular steps to reproduce, it just occurs time to time.

I use: Audit.NET 19.0.1 Audit.NET.MySql 19.0.1 Audit.EntityFramework.Core 19.0.1

with .NET 6

and the next confugiration: `Audit.Core.Configuration.AddCustomAction(ActionType.OnScopeCreated, scope => { scope.SetCustomField("AuditUser", ctxAccessor.HttpContext?.User?.Identity?.Name ?? "guest"); });

        Audit.Core.Configuration.Setup()
            .UseEntityFramework(x => x
                .AuditTypeMapper(typeName => typeof(AuditLog))
                .AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
                {
                    auditEntity.User = ev.CustomFields["AuditUser"]?.ToString() ?? "guest";
                    auditEntity.Table = ent.Table;
                    auditEntity.AuditDate = DateTime.UtcNow;
                    auditEntity.Action = ent.Action;
                    auditEntity.Changes = JsonSerializer.Serialize(ent.Changes);
                    auditEntity.Entries = JsonSerializer.Serialize(ev.GetEntityFrameworkEvent().Entries);
                    auditEntity.Success = ev.GetEntityFrameworkEvent().Success;
                    auditEntity.ColumnValues = JsonSerializer.Serialize(ent.ColumnValues);
                    auditEntity.PrimaryKey = JsonSerializer.Serialize(ent.PrimaryKey);
                }));`

Any ideas how can I fix this issue?

gbdrm avatar Dec 28 '21 20:12 gbdrm

I think you're missing the IgnoreMatchedProperties(true) configuration

Audit.Core.Configuration.Setup()
            .UseEntityFramework(x => x
                .AuditTypeMapper(typeName => typeof(AuditLog))
                .AuditEntityAction<AuditLog>((ev, ent, auditEntity) =>
                {
                    auditEntity.User = ev.CustomFields["AuditUser"]?.ToString() ?? "guest";
                    auditEntity.Table = ent.Table;
                    auditEntity.AuditDate = DateTime.UtcNow;
                    auditEntity.Action = ent.Action;
                    auditEntity.Changes = JsonSerializer.Serialize(ent.Changes);
                    auditEntity.Entries = JsonSerializer.Serialize(ev.GetEntityFrameworkEvent().Entries);
                    auditEntity.Success = ev.GetEntityFrameworkEvent().Success;
                    auditEntity.ColumnValues = JsonSerializer.Serialize(ent.ColumnValues);
                    auditEntity.PrimaryKey = JsonSerializer.Serialize(ent.PrimaryKey);
                }).IgnoreMatchedProperties());

Otherwise, the framework will try to match the Entity properties to the AuditLog properties automatically

thepirat000 avatar Dec 29 '21 23:12 thepirat000

didn't get any complaints recently, so it seems that this config helped. Thank you.

gbdrm avatar Jan 05 '22 20:01 gbdrm

I guess some of your entity classes have a property called User of type ApplicationUser, and the property matching mechanism was trying to set the ApplicationUser object to the AuditLog.User string property, and failed

thepirat000 avatar Jan 05 '22 20:01 thepirat000