C#C
C#2y ago
niels

Automate logging

In my ASP.NET application, I need to log that a certain action has taken place. Currently, I manually add a piece of code that does this. It takes information from the DbContext as well as the ServerCallContext. See the code snippet below, the relevant part is the adding of a row to the Auditlog. Is there a way to just automatically perform this action (e.g. by adding an attribute)?

[Authorize(Policy = RightsConstants.TemplateControl)] public override Task<AddResponse> AddSmsTemplate(EyeClinicAdminShared.SmsTemplate request, ServerCallContext context) { var user = _dbContext.GetUser(); var clinic = _dbContext.GetClinic(); var template = _mapper.Map<Models.SmsTemplate>(request); template.Clinic = clinic; var entry = _dbContext.SmsTemplates.Add(template); _dbContext.AuditLogs.Add(new AuditLog { Action = "" + System.Reflection.MethodBase.GetCurrentMethod()?.Name, User = user, Clinic = clinic, IpAddress = context.Peer, RawData = request.ToString() }); _dbContext.SaveChanges(); return Task.FromResult(new AddResponse() {Id = entry.Entity.Id}); }
Was this page helpful?