-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathAuditController.cs
70 lines (61 loc) · 2.31 KB
/
AuditController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Skoruba.AuditLogging.Constants;
using Skoruba.AuditLogging.Host.Dtos;
using Skoruba.AuditLogging.Host.Events;
using Skoruba.AuditLogging.Services;
using System;
using System.Threading.Tasks;
namespace Skoruba.AuditLogging.Host.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class AuditController(
IAuditEventLogger auditEventLogger,
ILogger<AuditController> logger) : ControllerBase
{
private readonly IAuditEventLogger _auditEventLogger = auditEventLogger;
private readonly ILogger<AuditController> _logger = logger;
[HttpGet]
public async Task<ActionResult> Get()
{
// Create fake product
var productDto = new ProductDto
{
Id = Guid.CreateVersion7().ToString(),
Name = Guid.CreateVersion7().ToString(),
Category = Guid.CreateVersion7().ToString()
};
// Log this action
var productGetUserEvent = new ProductGetEvent
{
Category = nameof(ProductGetEvent),
Product = productDto
};
var productGetMachineEvent = new ProductGetEvent
{
Category = nameof(ProductGetEvent),
Product = productDto,
SubjectType = AuditSubjectTypes.Machine,
SubjectName = Environment.MachineName,
SubjectIdentifier = Environment.MachineName,
Action = new { Method = nameof(Get), Class = nameof(AuditController) }
};
await _auditEventLogger.LogEventAsync(productGetMachineEvent, options =>
{
options.UseDefaultSubject = false;
options.UseDefaultAction = false;
});
await _auditEventLogger.LogEventAsync(productGetUserEvent);
var genericProductEvent = new GenericProductEvent<int, string, ProductDto>
{
Category = nameof(ProductGetEvent),
Product = productDto
};
await _auditEventLogger.LogEventAsync(genericProductEvent);
return Ok(productDto);
}
}
}