Beispiel #1
0
        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();
            }
        }
Beispiel #2
0
        internal AuditScope(AuditScopeOptions options)
        {
            _options        = 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 || NETSTANDARD2_0 || NETSTANDARD2_1 || NET461
            environment.UserName    = Environment.UserName;
            environment.MachineName = Environment.MachineName;
            environment.DomainName  = Environment.UserDomainName;
            if (callingMethod == null)
            {
                callingMethod = new StackFrame(2 + options.SkipExtraFrames).GetMethod();
            }
#else
            environment.MachineName = Environment.GetEnvironmentVariable("COMPUTERNAME");
            environment.UserName    = Environment.GetEnvironmentVariable("USERNAME");
#endif
            if (callingMethod != null)
            {
                environment.CallingMethodName = (callingMethod.DeclaringType != null ? callingMethod.DeclaringType.FullName + "." : "")
                                                + callingMethod.Name + "()";
                environment.AssemblyName = callingMethod.DeclaringType?.GetTypeInfo().Assembly.FullName;
            }
            _event             = options.AuditEvent ?? new AuditEvent();
            _event.Environment = environment;
            _event.StartDate   = Configuration.SystemClock.UtcNow;
            if (options.EventType != null)
            {
                _event.EventType = options.EventType;
            }
            if (_event.CustomFields == null)
            {
                _event.CustomFields = new Dictionary <string, object>();
            }
            if (options.TargetGetter != null)
            {
                var targetValue = options.TargetGetter.Invoke();
                _event.Target = new AuditTarget
                {
                    Old  = _dataProvider.Serialize(targetValue),
                    Type = targetValue?.GetType().GetFullTypeName() ?? "Object"
                };
            }
            ProcessExtraFields(options.ExtraFields);
        }
Beispiel #3
0
        private AuditScope(AuditScopeOptions options)
        {
            _options        = 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
            //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 + "()";
                environment.AssemblyName = callingMethod.DeclaringType?.GetTypeInfo().Assembly.FullName;
            }
            _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);
        }
Beispiel #4
0
        /// <summary>
        /// Creates an audit scope from a reference value, an event type and a reference Id.
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="target">The target object getter.</param>
        /// <param name="extraFields">An anonymous object that can contain additional fields will be merged into the audit event.</param>
        /// <param name="creationPolicy">The event creation policy to use.</param>
        /// <param name="dataProvider">The data provider to use. NULL to use the configured default data provider.</param>
        protected internal AuditScope(string eventType, Func <object> target, object extraFields = null,
                                      AuditDataProvider dataProvider     = null,
                                      EventCreationPolicy?creationPolicy = null)
        {
            _creationPolicy = creationPolicy ?? AuditConfiguration.CreationPolicy;
            _dataProvider   = dataProvider ?? AuditConfiguration.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;
#endif
            _event = new AuditEvent()
            {
                Environment  = environment,
                StartDate    = DateTime.Now,
                EventType    = eventType,
                Comments     = new List <string>(),
                CustomFields = new Dictionary <string, object>()
            };
            if (target != null)
            {
                var targetValue = target.Invoke();
                _event.Target = new AuditTarget
                {
                    SerializedOld = _dataProvider.Serialize(targetValue),
                    Type          = targetValue?.GetType().Name ?? "Object"
                };
            }
            ProcessExtraFields(extraFields);
            // Process the event insertion (if applies)
            if (_creationPolicy == EventCreationPolicy.InsertOnStartReplaceOnEnd || _creationPolicy == EventCreationPolicy.InsertOnStartInsertOnEnd)
            {
                _eventId = _dataProvider.InsertEvent(_event);
            }
        }
Beispiel #5
0
        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();
            }
        }