Exemple #1
0
        /// <summary>
        /// Creates an audit scope with the given extra fields, and saves it right away
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="extraFields">An anonymous object that can contain additional fields to be merged into the audit event.</param>
        public static async Task LogAsync(string eventType, object extraFields)
        {
            var options = new AuditScopeOptions()
            {
                EventType       = eventType,
                ExtraFields     = extraFields,
                IsCreateAndSave = true
            };

            await new AuditScope(options).StartAsync();
        }
Exemple #2
0
        /// <summary>
        /// Creates an audit scope with the given extra fields, and saves it right away
        /// </summary>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="extraFields">An anonymous object that can contain additional fields to be merged into the audit event.</param>
        public static void Log(string eventType, object extraFields)
        {
            var options = new AuditScopeOptions()
            {
                EventType       = eventType,
                ExtraFields     = extraFields,
                IsCreateAndSave = true
            };

            new AuditScope(options).Start();
        }
Exemple #3
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);
        }
Exemple #4
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);
        }
Exemple #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();
            }
        }
Exemple #6
0
        /// <summary>
        /// Shortcut to create an audit scope with the given Event type and Target.
        /// </summary>
        /// <param name="eventType">A string representing the type of the event.</param>
        /// <param name="target">The target object getter.</param>
        /// <param name="extraFields">An anonymous object that contains additional fields to be merged into the audit event.</param>
        public static async Task <AuditScope> CreateAsync(string eventType, Func <object> target, object extraFields = null)
        {
            var options = new AuditScopeOptions(eventType: eventType, targetGetter: target, extraFields: extraFields);

            return(await new AuditScope(options).StartAsync());
        }
Exemple #7
0
        /// <summary>
        /// Shortcut to create an audit scope with the given Event type and Target.
        /// </summary>
        /// <param name="eventType">A string representing the type of the event.</param>
        /// <param name="target">The target object getter.</param>
        /// <param name="extraFields">An anonymous object that contains additional fields to be merged into the audit event.</param>
        public static AuditScope Create(string eventType, Func <object> target, object extraFields = null)
        {
            var options = new AuditScopeOptions(eventType: eventType, targetGetter: target, extraFields: extraFields);

            return(new AuditScope(options).Start());
        }
Exemple #8
0
        /// <summary>
        /// Creates an audit scope with the given creation options as a Fluent API.
        /// </summary>
        public static async Task <IAuditScope> CreateAsync(Action <IAuditScopeOptionsConfigurator> config)
        {
            var options = new AuditScopeOptions(config);

            return(await new AuditScope(options).StartAsync());
        }
Exemple #9
0
        /// <summary>
        /// Creates an audit scope with the given creation options as a Fluent API.
        /// </summary>
        public static IAuditScope Create(Action <IAuditScopeOptionsConfigurator> config)
        {
            var options = new AuditScopeOptions(config);

            return(new AuditScope(options).Start());
        }
Exemple #10
0
 /// <summary>
 /// Shortcut to create an audit scope
 /// </summary>
 public static async Task <AuditScope> CreateAsync(AuditScopeOptions options)
 {
     return(await new AuditScope(options).StartAsync());
 }
Exemple #11
0
 /// <summary>
 /// Shortcut to create an audit scope
 /// </summary>
 public static AuditScope Create(AuditScopeOptions options)
 {
     return(new AuditScope(options).Start());
 }