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)
-
0
Hi,
You can consider creating an
AuditLogContributor.
https://docs.abp.io/en/abp/latest/Audit-Logging#audit-log-contributorsFor 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; } }
-
0
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
-
0
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 addResponseAuditLogContributor
-
0
What should we write at the top of our WebServiceLogon method to activate the ResponseAuditLogContributor?
-
0
Hi,
It is added to the Audit log system globally, it will be activated for every app service method
-
0
Oh, we don't want to log every method response. Is there any way to activate the ResponseAuditLogContributor only for a specific method?
-
0
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