Exemplo n.º 1
0
        public void ShouldSerializeHeaderWell()
        {
            //Arrange
            var options = new SyslogLoggerOptions
            {
                Hostname = "host",
                AppName  = "app",
                ProcId   = "proc"
            };

            var logTime = new DateTime(2001, 1, 1, 1, 1, 1, 1, DateTimeKind.Local)
                          .AddTicks(10);

            var serializer = new SyslogMessageSerializer(options)
            {
                Level   = LogLevel.Debug,
                EventId = new EventId(0),
                LogTime = logTime
            };

            //Act
            var strMsg = serializer.Serialize("foo");

            //Assert
            Assert.Equal("<191>1 2001-01-01T01:01:01.001001+03:00 host app proc foo", strMsg);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes the <see cref="AuditLogInstance" /> class.
        /// </summary>
        static AuditLogInstance()
        {
            IList <IAuditLogWriter> auditLogWriters = new List <IAuditLogWriter>();

            try
            {
                AuditLogConfiguration auditLogConfiguration = ConfigurationSettings.GetAuditLogConfigurationSection();

                if (auditLogConfiguration?.EventLogSettings != null && auditLogConfiguration.EventLogSettings.IsEnabled)
                {
                    auditLogWriters.Add(new AuditLogEventLogWriter(EventLog.Application));
                }

                if (auditLogConfiguration?.EntityModelSettings != null && auditLogConfiguration.EntityModelSettings.IsEnabled)
                {
                    auditLogWriters.Add(new AuditLogEntityModelWriter(new AuditLogEntityModelDeleter()));
                }

                if (auditLogConfiguration?.SyslogSettings != null && auditLogConfiguration.SyslogSettings.IsEnabled && !string.IsNullOrEmpty(auditLogConfiguration.SyslogSettings.HostName) && auditLogConfiguration.SyslogSettings.Port > 0)
                {
                    IStreamProvider          tcpStreamProvider     = new TcpStreamProvider(auditLogConfiguration.SyslogSettings.HostName, auditLogConfiguration.SyslogSettings.Port, true, auditLogConfiguration.SyslogSettings.IsSecure, auditLogConfiguration.SyslogSettings.IgnoreSslErrors);
                    ISyslogMessageSerializer syslogMsgSerializer   = new SyslogMessageSerializer();
                    ISyslogMessageWriter     streamWriter          = new SyslogStreamWriter(tcpStreamProvider, syslogMsgSerializer);
                    ISyslogMessageWriter     queueingMessageWriter = new SyslogQueueingMessageWriter(streamWriter);

                    auditLogWriters.Add(new AuditLogSyslogWriter(queueingMessageWriter));
                }
            }
            catch (Exception ex)
            {
                EventLog.Application.WriteError("AuditLogInstance failed to initialize. Error: {0}.", ex.ToString());
            }

            AuditLogInstanceInternal = new AuditLog(auditLogWriters);
        }
        public void TestSerializerCtrNullStream()
        {
            var syslogSerializer = new SyslogMessageSerializer();

            Assert.Throws <ArgumentNullException>(() => syslogSerializer.Serialize(new SyslogMessage
            {
                Facility  = SyslogFacility.ClockDaemon1,
                Severity  = SyslogSeverity.Alert,
                Timestamp = DateTimeOffset.UtcNow
            }, null));
        }
        public void TestSerializerData(SyslogFacility facility, SyslogSeverity severity, string dateTime, string hostName, string appName, string procId, string msgId, string structuredData, string message, string result)
        {
            var syslogSerializer = new SyslogMessageSerializer();

            var syslogMsg = new SyslogMessage
            {
                Facility  = facility,
                Severity  = severity,
                Timestamp = string.IsNullOrEmpty(dateTime) ? (DateTimeOffset?)null : DateTimeOffset.ParseExact(dateTime, @"MM/dd/yyyy H:mm zzz", CultureInfo.InvariantCulture),
                HostName  = hostName,
                AppName   = appName,
                ProcId    = procId,
                MsgId     = msgId,
                Message   = message
            };

            IEnumerable <SyslogSdElement> structuredDataList = DecodeStructuredData(structuredData);

            if (structuredDataList != null)
            {
                foreach (SyslogSdElement sd in structuredDataList)
                {
                    syslogMsg.StructuredDataElements.Add(sd);
                }
            }


            using (var stream = new MemoryStream())
            {
                // Serialize a message to a memory stream
                syslogSerializer.Serialize(syslogMsg, stream);

                stream.Position = 0;

                // Get the message as a string and compare
                using (var reader = new StreamReader(stream))
                {
                    Assert.AreEqual(result, reader.ReadToEnd(), "The serialized messages is invalid.");
                }
            }
        }
        public void TestSerializerCtrNullMessage()
        {
            var syslogSerializer = new SyslogMessageSerializer();

            Assert.Throws <ArgumentNullException>(() => syslogSerializer.Serialize(null, new MemoryStream()));
        }
Exemplo n.º 6
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
            });
        }