public void LogOperation(IEntity entity, LogOperationType operationType, int?userId = null) { if (entity != null && entity.GetType().GetCustomAttribute <AuditLogAttribute>() == null) { return; } var entityJson = operationType is (LogOperationType.Modify or LogOperationType.Create) && entity != null ? JsonConvert.SerializeObject(entity, Formatting.None, new JsonSerializerSettings { ContractResolver = new CustomJsonContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore }) : null; var logUserId = userId ?? _appContext.UserId; var auditLog = new AuditLogEntity { User = logUserId.HasValue ? _session.Load <UserEntity>(logUserId.Value) : null, OperationType = operationType, RelatedEntityId = entity?.Id, RelatedEntityJson = entityJson, CreationDateUtc = DateTime.UtcNow, ModificationDateUtc = DateTime.UtcNow }; _session.Save(auditLog); }
internal static void Log(LogOperationType type, string msg) { DateTime time = DateTime.Now; string format = "yyyy-MM-dd HH:mm:ss"; string log = String.Format(@"[{0}] {1} {2}", time.ToString(format), type, msg); FileInfo logInfo = new FileInfo(logFile); if (File.Exists(logFile)) { if (GetSizeInMb(logInfo.Length) >= 100) { try { File.Delete(logFile); } catch { throw new Exception("Failed to delete the log file."); } } } try { using (StreamWriter s = File.AppendText(logFile)) { s.WriteLine(log); } } catch { throw new Exception("Failed to write to the log file."); } }
public void PutLogMessage(TargetEntitySqlModel entity, LogOperationType operation, string message, string durationName) { Put("&nset @rows = cast(@@ROWCOUNT as nvarchar);&n"); if (message == null) { Put("set @msg = cast(ERROR_MESSAGE() as nvarchar(max))" + " + ' Severity:' + cast(ERROR_SEVERITY() as nvarchar)" + " + ' State:' + cast(ERROR_STATE() as nvarchar)" + " + ' Line:' + cast(ERROR_LINE() as nvarchar)" + " + ' Num:' + cast(ERROR_NUMBER() as nvarchar);&n"); } else { if (entity != null) message = $"{entity.TargetTable}: {message}"; if (message.Contains("@rows")) { Put($"set @msg = REPLACE('{message}','@rows',@rows);&n"); } else { Put($"set @msg = '{message}';&n"); } for (int i = 0; i < 9; i++) { string argname = "@arg" + i; if (message.Contains(argname)) { Put($"set @msg = REPLACE(@msg,'{argname}',{argname}); &n"); } } } if (durationName != null) { if (!_startedDurations.Contains(durationName)) throw new Exception("DBSH-00218 Duration name not defined: " + durationName); Put($"set @lastLogDiff = DATEDIFF(second, @lastLogTime_{durationName}, GETDATE());&n"); } else { Put("set @lastLogDiff = NULL;&n"); } Put($"INSERT INTO @messages (Message, Duration, Operation, TargetEntity, Rows) VALUES (@msg, @lastLogDiff, '{operation}', %v, @rows);&n", entity?.LogName); Put("RAISERROR(@msg, 0, 1) WITH NOWAIT; &n"); //Put("PRINT @msg; &n"); _datasync.Dbsh.LogHandlers.ForEach(x => x.PutLogMessage( Dumper, $"'{operation}'", entity == null ? "NULL" : $"'{entity.LogName}'", "@msg", "@lastLogDiff", _procName == null ? "''" : $"'{_procName}'", "@rows", _context)); //sb.AppendFormat( // "INSERT INTO ImportLog ([Procedure], SourceRowSet, TargetEntity, [RowCount], [Operation], ImportDate, Message, DurationSeconds) " // + "VALUES ('{0}', '{1}', '{2}', @rows, '{3}', @nowDateTime, @msg, @lastLogDiff);\n", // Procedure.Name, // entity == null || entity.Source == null ? "" : entity.Source.Name, // entity == null ? "" : entity.Name, // operation.ToString().ToUpper()); }