Starts in:
1 DAY
21 HRS
41 MIN
42 SEC
Starts in:
1 D
21 H
41 M
42 S
Open Closed

Logging Return Objects #6867


User avatar
0
IbrahimSarigoz created

I have a method in my application service where audit logging logs the input object. I want to log the return object from this method as well. Is it easily possible?

  • ABP Framework version: v8.0.4
  • UI Type: MVC
  • Database System: EF Core Oracle

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

    Hi,

    You can consider creating an AuditLogContributor. https://docs.abp.io/en/abp/latest/Audit-Logging#audit-log-contributors

    For example:

    public class ResponseAuditLogContributor : AuditLogContributor, ITransientDependency
    {
        public ResponseAuditLogContributor()
        {
        }
        
        public override void PostContribute(AuditLogContributionContext context)
        {
            var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
            if (httpContext == null)
            {
                return;
            }
    
            // read and log response body to audit log
            //httpContext.Response.Body;
        }
    }
    
  • User Avatar
    0
    IbrahimSarigoz created
    public virtual async Task<WebserviceLogonResponse> WebServiceLogon(string username,string password)
    {
        WebserviceLogonResponse webserviceLogonResponse = await LogonAsync(username,password);
        return webserviceLogonResponse;
    }
    

    How can I use ResponseAuditLogContributor in this method? Also, could you provide an example of writing this log to a text file

    thanks

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    The httpContext.Response.Body is a stream; you can read it as a string and write it into a text file.

    You can give it a try.

    PS, don't forget to configure the AbpAuditingOptions to add ResponseAuditLogContributor

  • User Avatar
    0
    IbrahimSarigoz created

    What should we write at the top of our WebServiceLogon method to activate the ResponseAuditLogContributor?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    It is added to the Audit log system globally, it will be activated for every app service method

  • User Avatar
    0
    IbrahimSarigoz created

    Oh, we don't want to log every method response. Is there any way to activate the ResponseAuditLogContributor only for a specific method?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Unfortunately, it can't be done.

    But you can use Interceptor; You can use the interceptor method and get the return value. Here is a video about how to write and use it. https://abp.io/video-courses/essentials/interception

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06