protected internal AuditScope Start() { _saveMode = SaveMode.InsertOnStart; // Execute custom on scope created actions Configuration.InvokeScopeCustomActions(ActionType.OnScopeCreated, this); // Process the event insertion (if applies) if (_options.IsCreateAndSave) { EndEvent(); SaveEvent(); _ended = true; } else if (_creationPolicy == EventCreationPolicy.InsertOnStartReplaceOnEnd || _creationPolicy == EventCreationPolicy.InsertOnStartInsertOnEnd) { SaveEvent(); _saveMode = _creationPolicy == EventCreationPolicy.InsertOnStartReplaceOnEnd ? SaveMode.ReplaceOnEnd : SaveMode.InsertOnEnd; } else if (_creationPolicy == EventCreationPolicy.InsertOnEnd) { _saveMode = SaveMode.InsertOnEnd; } else if (_creationPolicy == EventCreationPolicy.Manual) { _saveMode = SaveMode.Manual; } return(this); }
protected internal AuditScope(string eventType, Func <object> target, object extraFields = null, AuditDataProvider dataProvider = null, EventCreationPolicy?creationPolicy = null, bool isCreateAndSave = false) { _creationPolicy = creationPolicy ?? Configuration.CreationPolicy; _dataProvider = dataProvider ?? Configuration.DataProvider; _targetGetter = target; var environment = new AuditEventEnvironment() { Culture = System.Globalization.CultureInfo.CurrentCulture.ToString(), }; #if NET45 //This will be possible in future NETStandard: //See: https://github.com/dotnet/corefx/issues/1797, https://github.com/dotnet/corefx/issues/1784 var callingMethod = new StackFrame(2).GetMethod(); environment.UserName = Environment.UserName; environment.MachineName = Environment.MachineName; environment.DomainName = Environment.UserDomainName; environment.CallingMethodName = (callingMethod.DeclaringType != null ? callingMethod.DeclaringType.FullName + "." : "") + callingMethod.Name + "()"; environment.AssemblyName = callingMethod.DeclaringType?.Assembly.FullName; #elif NETSTANDARD1_3 environment.MachineName = Environment.GetEnvironmentVariable("COMPUTERNAME"); environment.UserName = Environment.GetEnvironmentVariable("USERNAME"); #endif _event = new AuditEvent() { Environment = environment, StartDate = DateTime.Now, EventType = eventType, CustomFields = new Dictionary <string, object>() }; if (target != null) { var targetValue = target.Invoke(); _event.Target = new AuditTarget { SerializedOld = _dataProvider.Serialize(targetValue), Type = targetValue?.GetType().GetFullTypeName() ?? "Object" }; } ProcessExtraFields(extraFields); // Execute custom on scope created actions Configuration.InvokeScopeCustomActions(ActionType.OnScopeCreated, this); // Process the event insertion (if applies) if (isCreateAndSave) { EndEvent(); SaveEvent(); _ended = true; } else if (_creationPolicy == EventCreationPolicy.InsertOnStartReplaceOnEnd || _creationPolicy == EventCreationPolicy.InsertOnStartInsertOnEnd) { SaveEvent(); } }
private void SaveEvent(bool forceInsert = false) { if (_ended) { return; } Configuration.InvokeScopeCustomActions(ActionType.OnEventSaving, this); if (_eventId != null && !forceInsert) { _dataProvider.ReplaceEvent(_eventId, _event); } else { _eventId = _dataProvider.InsertEvent(_event); } }
private async Task SaveEventAsync(bool forceInsert = false) { if (IsEndedOrDisabled()) { return; } // Execute custom on event saving actions Configuration.InvokeScopeCustomActions(ActionType.OnEventSaving, this); if (IsEndedOrDisabled()) { return; } if (_eventId != null && !forceInsert) { await _dataProvider.ReplaceEventAsync(_eventId, _event); } else { _eventId = await _dataProvider.InsertEventAsync(_event); } // Execute custom after saving actions Configuration.InvokeScopeCustomActions(ActionType.OnEventSaved, this); }
protected internal AuditScope(AuditScopeOptions options) { _creationPolicy = options.CreationPolicy ?? Configuration.CreationPolicy; _dataProvider = options.DataProvider ?? Configuration.DataProvider; _targetGetter = options.TargetGetter; var environment = new AuditEventEnvironment() { Culture = System.Globalization.CultureInfo.CurrentCulture.ToString(), }; MethodBase callingMethod = options.CallingMethod; #if NET45 || NET40 //This will be possible in future NETStandard: //See: https://github.com/dotnet/corefx/issues/1797, https://github.com/dotnet/corefx/issues/1784 environment.UserName = Environment.UserName; environment.MachineName = Environment.MachineName; environment.DomainName = Environment.UserDomainName; if (callingMethod == null) { callingMethod = new StackFrame(2 + options.SkipExtraFrames).GetMethod(); } #elif NETSTANDARD1_3 environment.MachineName = Environment.GetEnvironmentVariable("COMPUTERNAME"); environment.UserName = Environment.GetEnvironmentVariable("USERNAME"); #endif if (callingMethod != null) { environment.CallingMethodName = (callingMethod.DeclaringType != null ? callingMethod.DeclaringType.FullName + "." : "") + callingMethod.Name + "()"; #if NET40 environment.AssemblyName = callingMethod.DeclaringType?.Assembly.FullName; #else environment.AssemblyName = callingMethod.DeclaringType?.GetTypeInfo().Assembly.FullName; #endif } _event = options.AuditEvent ?? new AuditEvent(); _event.Environment = environment; _event.StartDate = DateTime.Now; _event.EventType = options.EventType; _event.CustomFields = new Dictionary <string, object>(); if (options.TargetGetter != null) { var targetValue = options.TargetGetter.Invoke(); _event.Target = new AuditTarget { SerializedOld = _dataProvider.Serialize(targetValue), Type = targetValue?.GetType().GetFullTypeName() ?? "Object" }; } ProcessExtraFields(options.ExtraFields); // Execute custom on scope created actions Configuration.InvokeScopeCustomActions(ActionType.OnScopeCreated, this); // Process the event insertion (if applies) if (options.IsCreateAndSave) { EndEvent(); SaveEvent(); _ended = true; } else if (_creationPolicy == EventCreationPolicy.InsertOnStartReplaceOnEnd || _creationPolicy == EventCreationPolicy.InsertOnStartInsertOnEnd) { SaveEvent(); } }