Ends in:
4 DAYS
7 HRS
28 MIN
50 SEC
Ends in:
4 D
7 H
28 M
50 S
Open Closed

Auditlog: How to log paramters > 2000 characters? #1613


User avatar
0
BassaSolutions created

Audit logging:

When the paramter is larger than 2000 characters, no parameters are logged.

Is it possible to cut off the parameters at 2000 chars and still log them? The same way 'comments' in audit logs corrently works.

  • ABP Framework version: v4.3.2
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can custom the AuditLog entity.

    Try:

    
    public class MigrationDbContext
    {
        ......
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            .......
    
            builder.Entity<AuditLog>(b =>
            {
                b.Property(x => x.Comments).HasMaxLength(int.MaxValue);
            });
        }
    }
    

    And add & apply migration.

  • User Avatar
    0
    BassaSolutions created

    Ok, I formulated it badly.

    I mean the property AbpAuditLogActions.Parameters, where the HTTP POST Body is logged.

    It has a max length of 2000 characters. When the JSON is longer than 2000, it will log nothing at all. I would like to have it cut off instead, same as AbpAuditLogs.Comments.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Got it, I think you can create an audit Log Contributors : https://docs.abp.io/en/abp/latest/Audit-Logging#audit-log-contributors

    public class MyAuditLogContributor : AuditLogContributor
    {
        public override void PostContribute(AuditLogContributionContext context)
        {
            foreach (var action in context.AuditInfo.Actions)
            {
                if (action.Parameters.Length > 2000)
                {
                    action.Parameters = action.Parameters.Substring(0, 1999);
                }
            }
        }
    }
    
  • User Avatar
    0
    BassaSolutions created

    This works half way, the data is inserted into the DB. But now the Frontend crashes (Blazor), because the Page (AuditLogs -> Action -> Details) seems to expect a valid JSON:

    
     ---> System.Text.Json.JsonReaderException: Expected end of string, but instead reached end of data. LineNumber: 0 | BytePositionInLine: 1999.
    

    I guess this was the reason why it was just not inserted into the DB. For auditing it would be really useful to still get the information, wo can this be fixed?

  • User Avatar
    0
    BassaSolutions created

    Ok, I tried fixing it by writing the data to antoher field (e.g. increasing comments size and writing it there, or in ExtraProperties).

    None of it works, because the UI does not display it. (e.g. comments only shows first 200 characters, Extraproperties seems to not be able to hande json).

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    But now the Frontend crashes (Blazor), because the Page (AuditLogs -> Action -> Details) seems to expect a valid JSON:

    Can you provide steps to reproduce? thanks.

  • User Avatar
    0
    BassaSolutions created

    Steps to reproduce:

    • Use Blazor Server
    • Write an invalid JSON into AuditLogActions.Details. (e.g.Cut off valid json at 2000 characters). Can also be directy edited in the database.
    • Open Audilogs->Actions->Details
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I have checked the source code, and yes, this is by design. you can not truncate the Parameters.

    You can use the change Parameters max length for the solution:

    public class MigrationDbContext
    {
        ......
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            .......
    
            builder.Entity<AuditLogAction>(b =>
            {
                b.Property(x => x.Parameters).HasMaxLength(int.MaxValue);
            });
        }
    }
    
  • User Avatar
    0
    BassaSolutions created

    The parameters also often contains large base64 images, so a maxlength parameter field would seriously impact our database performance. It would be great when at least 1 field of the audit/auditaction table works for this. Eg. comments ui gets scrollable when length > 200.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I find a way to show the UI, it can work with https://support.abp.io/QA/Questions/1613#answer-4bbf3269-2b57-ae99-3c1d-39fde434f0fb

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(AuditLogsAppService), typeof(IAuditLogsAppService))]
    public class MyAuditLogsAppService : AuditLogsAppService
    {
        public MyAuditLogsAppService(IAuditLogRepository auditLogRepository, IJsonSerializer jsonSerializer, IPermissionChecker permissionChecker, IPermissionDefinitionManager permissionDefinitionManager) : base(auditLogRepository, jsonSerializer, permissionChecker, permissionDefinitionManager)
        {
        }
    
        public override async Task<AuditLogDto> GetAsync(Guid id)
        {
            var log = await AuditLogRepository.GetAsync(id);
            return ObjectMapper.Map<AuditLog, AuditLogDto>(log);
        }
    }
    
  • User Avatar
    0
    BassaSolutions created

    Great, that works!

    I updated your solution to include the original part where it formats the JSON, only incase of an invalid json I just display the string.

    Thank you!

Made with ❤️ on ABP v9.1.0-preview. Updated on December 02, 2024, 12:35