Starts in:
1 DAY
13 HRS
7 MIN
14 SEC
Starts in:
1 D
13 H
7 M
14 S
Open Closed

Pass log information from UI to Audit Logging #6029


User avatar
0
hanntd created

Hi Abp Team,

I'm using Blazor Web Assembly for UI, I would like to add some information from UI such as razor page name to know which page has called the api and some other information for tracking. How I can custom the Audit Logging module to add more information like that.

Thanks, Dharma (Han Nguyen)


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

    Hi,

    This is just my simple idea:

    1. Add the current route name to the HttpRequest headers. for example:
    public class AuditMessageHandler : DelegatingHandler, ITransientDependency
    {
        private readonly NavigationManager _navigationManager;
    
        public AuditMessageHandler(NavigationManager navigationManager)
        {
            _navigationManager = navigationManager;
        }
    
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            request.Headers.Add("blazor-audit-url", _navigationManager.Uri);
            return base.SendAsync(request, cancellationToken);
        }
    }
    
    public override void PreConfigureServices(ServiceConfigurationContext context)
    {
        PreConfigure<AbpHttpClientBuilderOptions>(options =>
        {
            options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) =>
            {
                clientBuilder.AddHttpMessageHandler<AuditMessageHandler>();
            });
        });
    }
    

    2 You can add your AuditLogContributor to save the information to the database.

    https://docs.abp.io/en/abp/latest/Audit-Logging#audit-log-contributors

  • User Avatar
    0
    hanntd created

    Hi, I'd like to add some additional information into the standard ABP Audit Logging, for example with the standard audit logging of ABP will add one extra property such as PageName and when Audit Logging insert to database for Http Api call will add PageName into extra property of the logged record. When we check the log we will know exactly which page, which screen has called this api or related to this log record :

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I think I have explained it here.

    https://support.abp.io/QA/Questions/6029#answer-3a0e6dda-91f6-3ffa-978d-a04fb88877c8

  • User Avatar
    0
    hanntd created

    Thanks, but I still confuse how can we can combine #1 and #2 above so that we have Blaze-Audit-Url value in the database. Do you have any similar sample code? Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    First, you need to define new property:

    https://docs.abp.io/en/abp/latest/Module-Entity-Extensions#quick-example

    Then add a new AuditLogContributor to get blazor-audit-url from the httpcontext request headers and set to database.

  • User Avatar
    0
    hanntd created

    Thanks, let me try.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    ok

  • User Avatar
    0
    hanntd created

    I have done successful as you what you guide. Thank you so much

  • User Avatar
    0
    hanntd created

    Hello Liangshiwei,

    I'd like to reopen this ticket. I can add an extra property (blazor-url) into AuditLogging to keep blazor page url when calling Api. Now I'd like to get data from Auditlogging and can be filtered by blazor-url, for normal fields I can use AuditLogsAppService.GetListAsync(new GetAuditLogListDto { }) and filter but how can I apply filter for extra property such as blazor-url mentioned above?

    Thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can override the AuditLogs repository to filter the blazor-url.

    You can refer to those:

    • https://docs.abp.io/en/abp/latest/Entity-Framework-Core#mapefcoreproperty
    • https://learn.microsoft.com/en-us/ef/core/modeling/shadow-properties#accessing-shadow-properties
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06