Esempio n. 1
0
        public async Task <Response> AddAuditEntry(EventType eventType, EventId eventId, string message)
        {
            try
            {
                if (!IsAuditLogEnabled())
                {
                    return(new FailedResponse("Audit Log is disabled."));
                }

                var ui = GetUsernameAndIp();

                // Truncate data so that SQL won't blow up
                if (message.Length > 256)
                {
                    message = message.Substring(0, 256);
                }

                string machineName = Environment.MachineName;
                if (machineName.Length > 32)
                {
                    machineName = machineName.Substring(0, 32);
                }

                var auditEntry = new AuditEntry(eventType, eventId, ui.Username, ui.Ipv4, machineName, message);

                var connStr = _configuration.GetConnectionString(Constants.DbConnectionName);
                //await using var conn = new SqlConnection(connStr);
                await using var conn = new MySqlConnection(connStr);

                //var sql = @"INSERT INTO AuditLog([EventId],[EventType],[EventTimeUtc],[WebUsername],[IpAddressV4],[MachineName],[Message])
                //            VALUES(@EventId, @EventType, @EventTimeUtc, @Username, @IpAddressV4, @MachineName, @Message)";
                var sql = @"INSERT INTO AuditLog(EventId,EventType,EventTimeUtc,WebUsername,IpAddressV4,MachineName,Message)
                            VALUES(@EventId, @EventType, @EventTimeUtc, @Username, @IpAddressV4, @MachineName, @Message)";

                int rows = await conn.ExecuteAsync(sql, auditEntry);

                return(new Response(rows > 0));
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                return(new FailedResponse((int)ResponseFailureCode.GeneralException, e.Message, e));
            }
        }
Esempio n. 2
0
        public async Task AddAuditEntry(EventType eventType, AuditEventId auditEventId, string message)
        {
            try
            {
                if (!await IsAuditLogEnabled())
                {
                    return;
                }

                (string username, string ipv4) = GetUsernameAndIp();

                // Truncate data so that SQL won't blow up
                if (message.Length > 256)
                {
                    message = message.Substring(0, 256);
                }

                string machineName = Environment.MachineName;
                if (machineName.Length > 32)
                {
                    machineName = machineName.Substring(0, 32);
                }

                var auditEntry = new AuditEntry(eventType, auditEventId, username, ipv4, machineName, message);

                var connStr = _configuration.GetConnectionString(Constants.DbConnectionName);
                await using var conn = new SqlConnection(connStr);

                var sql = @"INSERT INTO AuditLog([EventId],[EventType],[EventTimeUtc],[WebUsername],[IpAddressV4],[MachineName],[Message])
                            VALUES(@EventId, @EventType, @EventTimeUtc, @Username, @IpAddressV4, @MachineName, @Message)";

                await conn.ExecuteAsync(sql, auditEntry);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
            }
        }