/// <summary>
        ///     Validates the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="auditLogEventData">The audit log event data.</param>
        /// <returns></returns>
        private bool ValidateMessage(SyslogMessage message, IAuditLogEntryData auditLogEventData)
        {
            SyslogApplicationSettings syslogApplicationSettings = ConfigurationSettings.GetSyslogConfigurationSection().SyslogApplicationSettings;
            SyslogSeverity            severity;

            string databaseName          = string.Empty;
            string databaseServer        = string.Empty;
            var    databaseConfiguration = ConfigurationSettings.GetDatabaseConfigurationSection();

            if (databaseConfiguration?.ConnectionSettings != null)
            {
                databaseName   = databaseConfiguration.ConnectionSettings.Database;
                databaseServer = databaseConfiguration.ConnectionSettings.Server;
            }

            switch (auditLogEventData.SeverityEnum)
            {
            case AuditLogSeverityEnum_Enumeration.AuditLogInformation:
                severity = SyslogSeverity.Informational;
                break;

            case AuditLogSeverityEnum_Enumeration.AuditLogWarning:
                severity = SyslogSeverity.Warning;
                break;

            case AuditLogSeverityEnum_Enumeration.AuditLogError:
                severity = SyslogSeverity.Error;
                break;

            default:
                throw new ArgumentException("auditLogEventData");
            }

            Assert.AreEqual(1, message.Version, "The version is incorrect.");
            Assert.AreEqual(SyslogFacility.LogAudit, message.Facility, "The facility is incorrect");
            Assert.AreEqual(severity, message.Severity, "The severity is incorrect");
            Assert.AreEqual(syslogApplicationSettings.ApplicationName, message.AppName, "The app name is incorrect");
            Assert.AreEqual(auditLogEventData.AuditLogEntryMetadata.MessageId, message.MsgId, "The message id is invalid");
            Assert.AreEqual(4, message.StructuredDataElements.Count, "The number of structured data elements is invalid");
            Assert.AreEqual("audit@" + syslogApplicationSettings.EnterpriseId, message.StructuredDataElements[0].SdId);
            Assert.AreEqual(auditLogEventData.AuditLogEntryMetadata.MessageId + "@" + syslogApplicationSettings.EnterpriseId, message.StructuredDataElements[1].SdId);
            Assert.AreEqual("systemInfo@" + syslogApplicationSettings.EnterpriseId, message.StructuredDataElements[2].SdId);
            Assert.AreEqual("installDirectory", message.StructuredDataElements[2].Parameters[0].Name);
            Assert.AreEqual(SystemInfo.InstallFolder.Replace("\\", "/"), message.StructuredDataElements[2].Parameters[0].Value);

            Assert.AreEqual("databaseName", message.StructuredDataElements[2].Parameters[1].Name);
            Assert.AreEqual(databaseName, message.StructuredDataElements[2].Parameters[1].Value);

            Assert.AreEqual("databaseServer", message.StructuredDataElements[2].Parameters[2].Name);
            Assert.AreEqual(databaseServer, message.StructuredDataElements[2].Parameters[2].Value);

            Assert.AreEqual(SyslogOriginConstants.Origin, message.StructuredDataElements[3].SdId);

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="AuditLogSyslogWriter" /> class.
        /// </summary>
        /// <param name="syslogMessageWriter">The syslog message writer.</param>
        /// <exception cref="System.ArgumentNullException">syslogMessageWriter</exception>
        public AuditLogSyslogWriter(ISyslogMessageWriter syslogMessageWriter)
        {
            if (syslogMessageWriter == null)
            {
                throw new ArgumentNullException(nameof(syslogMessageWriter));
            }

            _syslogMessageWriter = syslogMessageWriter;

            SyslogConfiguration syslogConfiguration = ConfigurationSettings.GetSyslogConfigurationSection();

            if (syslogConfiguration?.SyslogApplicationSettings != null)
            {
                _enterpriseId    = syslogConfiguration.SyslogApplicationSettings.EnterpriseId;
                _applicationName = syslogConfiguration.SyslogApplicationSettings.ApplicationName;
            }

            // Fallback
            if (_enterpriseId == 0)
            {
                _enterpriseId = SyslogReadiNowConstants.EnterpriseId;
            }

            if (string.IsNullOrEmpty(_applicationName))
            {
                _applicationName = SyslogReadiNowConstants.ApplicationName;
            }

            _databaseName   = string.Empty;
            _databaseServer = string.Empty;
            var databaseConfiguration = ConfigurationSettings.GetDatabaseConfigurationSection();

            if (databaseConfiguration?.ConnectionSettings != null)
            {
                _databaseName   = databaseConfiguration.ConnectionSettings.Database;
                _databaseServer = databaseConfiguration.ConnectionSettings.Server;
            }

            _processName = Process.GetCurrentProcess().MainModule.ModuleName;
            _hostName    = GetHostName();

            try
            {
                _ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
            }
            catch
            {
                // ignored
            }

            if (!string.IsNullOrEmpty(SystemInfo.InstallFolder))
            {
                _installFolder = SystemInfo.InstallFolder.Replace("\\", "/");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the syslog writer.
        /// </summary>
        /// <returns>The syslog writer.</returns>
        public static IEventLogWriter GetSyslogEventLogWriter()
        {
            DiagnosticsConfiguration diagnosticsConfiguration = ConfigurationSettings.GetDiagnosticsConfigurationSection();

            EventLogSyslogSettings syslogSettings = diagnosticsConfiguration?.SyslogSettings;

            if (syslogSettings == null || !syslogSettings.IsEnabled || string.IsNullOrEmpty(syslogSettings.HostName) || syslogSettings.Port <= 0)
            {
                return(null);
            }

            int    enterpriseId    = 0;
            string applicationName = string.Empty;

            SyslogConfiguration syslogConfiguration = ConfigurationSettings.GetSyslogConfigurationSection();

            if (syslogConfiguration?.SyslogApplicationSettings != null)
            {
                enterpriseId    = syslogConfiguration.SyslogApplicationSettings.EnterpriseId;
                applicationName = syslogConfiguration.SyslogApplicationSettings.ApplicationName;
            }

            // Fallback
            if (enterpriseId == 0)
            {
                enterpriseId = SyslogReadiNowConstants.EnterpriseId;
            }

            if (string.IsNullOrEmpty(applicationName))
            {
                applicationName = SyslogReadiNowConstants.ApplicationName;
            }

            string databaseName          = string.Empty;
            string databaseServer        = string.Empty;
            var    databaseConfiguration = ConfigurationSettings.GetDatabaseConfigurationSection();

            if (databaseConfiguration?.ConnectionSettings != null)
            {
                databaseName   = databaseConfiguration.ConnectionSettings.Database;
                databaseServer = databaseConfiguration.ConnectionSettings.Server;
            }

            IStreamProvider          tcpStreamProvider     = new TcpStreamProvider(syslogSettings.HostName, syslogSettings.Port, true, syslogSettings.IsSecure, syslogSettings.IgnoreSslErrors);
            ISyslogMessageSerializer syslogMsgSerializer   = new SyslogMessageSerializer();
            ISyslogMessageWriter     streamWriter          = new SyslogStreamWriter(tcpStreamProvider, syslogMsgSerializer);
            ISyslogMessageWriter     queueingMessageWriter = new SyslogQueueingMessageWriter(streamWriter, 0);

            return(new EventLogSyslogWriter(queueingMessageWriter)
            {
                EnterpriseId = enterpriseId,
                ApplicationName = applicationName,
                SoftwareVersion = SystemInfo.PlatformVersion,
                ErrorEnabled = syslogSettings.ErrorEnabled,
                WarningEnabled = syslogSettings.WarningEnabled,
                InformationEnabled = syslogSettings.InformationEnabled,
                TraceEnabled = syslogSettings.TraceEnabled,
                InstallFolder = SystemInfo.InstallFolder,
                DatabaseName = databaseName,
                DatabaseServer = databaseServer
            });
        }