예제 #1
0
        /// <summary>
        ///     Initialises the specified audit log entry.
        /// </summary>
        /// <param name="success">if set to <c>true</c> the entry is successfull, false otherwise.</param>
        /// <param name="logEntryMetadataId">The log entry metadata identifier.</param>
        /// <param name="parameters">The parameters.</param>
        private void Initialise(bool success, string logEntryMetadataId, IDictionary <string, object> parameters)
        {
            using (new SecurityBypassContext())
            {
                _createdDate = DateTime.UtcNow;

                // Get the metadata
                _auditLogEntryMetadata = Entity.Get <AuditLogEntryMetadata>(logEntryMetadataId);
                if (_auditLogEntryMetadata == null)
                {
                    throw new ArgumentException("logEntryMetadataId");
                }

                _auditLogEntryType = _auditLogEntryMetadata.AuditLogEntryType;

                // Get the severity based on success or failure
                AuditLogSeverityEnum severity = success ? _auditLogEntryMetadata.SeveritySuccess : _auditLogEntryMetadata.SeverityFailure;

                // Assign properties common to all log entrues
                _success      = success;
                _severity     = severity;
                _severityEnum = AuditLogSeverityEnum.ConvertAliasToEnum(_severity.Alias) ?? AuditLogSeverityEnum_Enumeration.AuditLogInformation;
                _userName     = GetCurrentUserName();
                _tenantName   = RequestContext.GetContext().Tenant.Name;

                // Assign type specific parameters
                if (parameters != null)
                {
                    foreach (var kvp in parameters)
                    {
                        _parameters[kvp.Key] = kvp.Value;
                    }
                }

                string messageFormatString = _auditLogEntryMetadata.MessageFormatString;

                // Generate message from format string
                if (!string.IsNullOrEmpty(messageFormatString))
                {
                    IDictionary <string, object> fields = new Dictionary <string, object>(_parameters);

                    // Add base fields keyed off aliases as this is what is expected in the format string
                    fields["auditLogEntrySuccess"]     = _success;
                    fields["auditLogEntryUser"]        = _userName;
                    fields["auditLogEntryCreatedDate"] = _createdDate;
                    fields["auditLogEntrySeverity"]    = _severityEnum;

                    _message = string.Format(new DictionaryFormatter(), messageFormatString, fields);
                }
            }
        }
        public void TestWriteAuditLogEntryToEventLog(AuditLogSeverityEnum_Enumeration severity)
        {
            var  eventLogSettings = ConfigurationSettings.GetAuditLogConfigurationSection().EventLogSettings;
            bool isEnabled        = eventLogSettings.IsEnabled;

            try
            {
                var mockEventLog = new Mock <IEventLog>(MockBehavior.Strict);

                switch (severity)
                {
                case AuditLogSeverityEnum_Enumeration.AuditLogError:
                    mockEventLog.Setup(l => l.WriteError(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;

                case AuditLogSeverityEnum_Enumeration.AuditLogInformation:
                    mockEventLog.Setup(l => l.WriteInformation(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;

                case AuditLogSeverityEnum_Enumeration.AuditLogWarning:
                    mockEventLog.Setup(l => l.WriteWarning(It.Is <string>(s => s.StartsWith("Audit log entry.") && s.Contains("p1Value") && s.Contains("p2Value"))));
                    break;
                }

                // Ensure event log is enabled
                eventLogSettings.IsEnabled = true;
                var eventLogWriter = new AuditLogEventLogWriter(mockEventLog.Object);

                // Override severity
                AuditLogEntryMetadata metaData = Entity.Get <AuditLogEntryMetadata>("logonAuditLogEntryMetadata", true);
                metaData.SeverityFailure_Enum = severity;
                metaData.Save();

                IAuditLogEntryData auditLogEventData = new AuditLogEntryData(false, "logonAuditLogEntryMetadata", new Dictionary <string, object>
                {
                    { "p1", "p1Value" },
                    { "p2", "p2Value" }
                });

                eventLogWriter.Write(auditLogEventData);

                mockEventLog.VerifyAll();
            }
            finally
            {
                eventLogSettings.IsEnabled = isEnabled;
            }
        }