コード例 #1
0
 public static void CreateAndSave(string eventType, object extraFields, AuditDataProvider dataProvider = null)
 {
     using (var scope = new AuditScope(new AuditScopeOptions(eventType, null, extraFields, dataProvider, null, true)))
     {
         scope.Start();
     }
 }
コード例 #2
0
ファイル: AuditScope.cs プロジェクト: popotans/Audit.NET
        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();
            }
        }
コード例 #3
0
ファイル: AuditScope.cs プロジェクト: antunesl/Audit.NET
        /// <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);
            }
        }
コード例 #4
0
ファイル: AuditScope.cs プロジェクト: CedarLogic/Audit.NET
        /// <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)
        {
            int callingMethodStackIndex = 2;

            _creationPolicy = creationPolicy ?? AuditConfiguration.CreationPolicy;
            _dataProvider   = dataProvider ?? AuditConfiguration.DataProvider;
            _targetGetter   = target;
            var callingMethod = new StackFrame(callingMethodStackIndex).GetMethod();

            _event = new AuditEvent()
            {
                Environment = new AuditEventEnvironment()
                {
                    UserName          = Environment.UserName,
                    MachineName       = Environment.MachineName,
                    DomainName        = Environment.UserDomainName,
                    CallingMethodName = (callingMethod.DeclaringType != null ? callingMethod.DeclaringType.FullName + "." : "") + callingMethod.Name + "()",
                    AssemblyName      = callingMethod.DeclaringType?.Assembly.FullName,
                    Culture           = System.Globalization.CultureInfo.CurrentCulture.ToString()
                },
                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)
            {
                _event.EventId = _dataProvider.InsertEvent(_event);
            }
        }
コード例 #5
0
 /// <summary>
 /// Creates an instance of options for an audit scope creation.
 /// </summary>
 /// <param name="eventType">A string representing the type of the event.</param>
 /// <param name="targetGetter">The target object getter.</param>
 /// <param name="extraFields">An anonymous object that contains additional fields to be merged into the audit event.</param>
 /// <param name="creationPolicy">The event creation policy to use. NULL to use the configured default creation policy.</param>
 /// <param name="dataProvider">The data provider to use. NULL to use the configured default data provider.</param>
 /// <param name="isCreateAndSave">To indicate if the scope should be immediately saved after creation.</param>
 /// <param name="auditEvent">The initialized audit event to use, or NULL to create a new instance of AuditEvent.</param>
 /// <param name="skipExtraFrames">Used to indicate how many frames in the stack should be skipped to determine the calling method.</param>
 public AuditScopeOptions(
     string eventType                   = null,
     Func <object> targetGetter         = null,
     object extraFields                 = null,
     AuditDataProvider dataProvider     = null,
     EventCreationPolicy?creationPolicy = null,
     bool isCreateAndSave               = false,
     AuditEvent auditEvent              = null,
     int skipExtraFrames                = 0)
 {
     EventType           = eventType;
     TargetGetter        = targetGetter;
     ExtraFields         = extraFields;
     CreationPolicy      = creationPolicy ?? Configuration.CreationPolicy;
     DataProviderFactory = dataProvider != null ? () => dataProvider : Configuration.DataProviderFactory;
     IsCreateAndSave     = isCreateAndSave;
     AuditEvent          = auditEvent;
     SkipExtraFrames     = skipExtraFrames;
     CallingMethod       = null;
 }
コード例 #6
0
 public IAuditScopeOptionsConfigurator DataProvider(AuditDataProvider dataProvider)
 {
     _options.DataProvider = dataProvider;
     return(this);
 }
コード例 #7
0
 public static async Task CreateAndSaveAsync(string eventType, object extraFields, AuditDataProvider dataProvider = null)
 {
     await new AuditScope(new AuditScopeOptions(eventType, null, extraFields, dataProvider, null, true)).StartAsync();
 }
コード例 #8
0
 public static AuditScope Create(string eventType, Func <object> target, object extraFields, EventCreationPolicy creationPolicy, AuditDataProvider dataProvider = null, AuditEvent auditEvent = null, int skipExtraFrames = 0)
 {
     return(new AuditScope(new AuditScopeOptions(eventType, target, extraFields, dataProvider, creationPolicy, false, auditEvent, skipExtraFrames)).Start());
 }
コード例 #9
0
 public static AuditScope Create(string eventType, Func <object> target, EventCreationPolicy creationPolicy, AuditDataProvider dataProvider = null)
 {
     return(new AuditScope(new AuditScopeOptions(eventType, target, null, dataProvider, creationPolicy)).Start());
 }
コード例 #10
0
 public async Task <IAuditScope> CreateAsync(string eventType, Func <object> target, object extraFields, EventCreationPolicy?creationPolicy, AuditDataProvider dataProvider)
 {
     return(await new AuditScope(new AuditScopeOptions(eventType, target, extraFields, dataProvider, creationPolicy)).StartAsync());
 }
コード例 #11
0
 /// <summary>
 /// Creates an audit scope from a reference value, and an event type.
 /// </summary>
 /// <param name="eventType">Type of the event.</param>
 /// <param name="target">The reference 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>
 public static AuditScope Create(string eventType, Func <object> target, object extraFields, EventCreationPolicy creationPolicy, AuditDataProvider dataProvider = null)
 {
     return(new AuditScope(eventType, target, extraFields, dataProvider, creationPolicy));
 }
コード例 #12
0
 public static async Task LogAsync(string eventType, object extraFields, AuditDataProvider dataProvider = null)
 {
     await CreateAndSaveAsync(eventType, extraFields, null);
 }
コード例 #13
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>
 /// <param name="dataProvider">The data provider to use. NULL to use the configured default data provider.</param>
 public static void CreateAndSave(string eventType, object extraFields, AuditDataProvider dataProvider = null)
 {
     new AuditScope(eventType, null, extraFields, dataProvider, null, true);
 }
コード例 #14
0
 static AuditConfiguration()
 {
     DataProvider   = new FileDataProvider();
     CreationPolicy = EventCreationPolicy.InsertOnEnd;
 }
コード例 #15
0
 public static async Task <AuditScope> CreateAsync(string eventType, Func <object> target, EventCreationPolicy creationPolicy, AuditDataProvider dataProvider = null)
 {
     return(await new AuditScope(new AuditScopeOptions(eventType, target, null, dataProvider, creationPolicy)).StartAsync());
 }
コード例 #16
0
 /// <summary>
 /// Sets the default data provider to use.
 /// </summary>
 /// <param name="dataProvider">The data provider.</param>
 public static void SetDataProvider(AuditDataProvider dataProvider)
 {
     DataProvider = dataProvider;
 }
コード例 #17
0
 public IAuditScope Create(string eventType, Func <object> target, object extraFields, EventCreationPolicy?creationPolicy, AuditDataProvider dataProvider)
 {
     return(new AuditScope(new AuditScopeOptions(eventType, target, extraFields, dataProvider, creationPolicy)).Start());
 }