File size: 2,333 Bytes
f0d69b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using TaskTrackingSystem.Database.AppDbContextModels;

namespace TaskTrackingSystem.WebApi.Infrastructure
{
    public class AuditLogService
    {
        private readonly AppDbContext _db;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public AuditLogService(AppDbContext db, IHttpContextAccessor httpContextAccessor)
        {
            _db = db;
            _httpContextAccessor = httpContextAccessor;
        }

        public async System.Threading.Tasks.Task LogAsync(string action, string module, string description)
        {
            try
            {
                var httpContext = _httpContextAccessor.HttpContext;
                long? userId = null;
                string? ipAddress = null;

                if (httpContext != null)
                {
                    var user = httpContext.User;
                    if (user != null)
                    {
                        var id = user.GetUserId();
                        if (id > 0)
                        {
                            userId = id;
                        }
                    }

                    // Get remote IP address
                    ipAddress = httpContext.Connection?.RemoteIpAddress?.ToString();

                    // Fallback to check X-Forwarded-For if behind reverse proxy
                    if (httpContext.Request.Headers.TryGetValue("X-Forwarded-For", out var forwardedFor))
                    {
                        ipAddress = forwardedFor.ToString().Split(',')[0].Trim();
                    }
                }

                var log = new AuditLog
                {
                    UserId = userId,
                    Action = action,
                    Module = module,
                    Description = description,
                    IpAddress = ipAddress,
                    CreatedAt = DateTime.UtcNow
                };

                _db.AuditLogs.Add(log);
                await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                // Never let audit logging failure block critical operations
                Console.WriteLine($"[AuditLog Error] Failed to write audit log: {ex.Message}");
            }
        }
    }
}