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
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

7 Answer(s)
  • User Avatar
    0
    liangshiwei created
    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;
        }
    }
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    IbrahimSarigoz created

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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Hi,

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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • 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?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    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

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.