Exemplo n.º 1
0
        /// <summary>
        /// Writes the specified message with a retry.
        /// </summary>
        /// <param name="message">The message.</param>
        private void WriteWithRetry(SyslogMessage message)
        {
            int retryCount = 0;

            while (true)
            {
                bool retry = false;

                try
                {
                    _messageWriter.Write(message);
                }
                catch (Exception ex)
                {
                    retry = (_maxRetries > 0) && (++retryCount <= _maxRetries);

                    if (retry)
                    {
                        Trace.TraceWarning("An error occurred writing syslog message MsgId:{0}. Retry count:{1}. Error:{2}.", message.MsgId, retryCount, ex);
                    }
                    else
                    {
                        Trace.TraceError("An error occurred writing syslog message MsgId:{0}. Error:{1}.", message.MsgId, ex);
                    }
                }

                if (!retry)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Writes the structured data.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageBuilder">The message builder.</param>
        private void WriteStructuredData(SyslogMessage message, StringBuilder messageBuilder)
        {
            if (message.StructuredDataElements.Count <= 0 || message.StructuredDataElements.All(sd => sd.Parameters.All(p => string.IsNullOrWhiteSpace(p.Value))))
            {
                messageBuilder.Append(NilValue);
            }
            else
            {
                // See SD-NAME in http://tools.ietf.org/html/rfc5424#section-6
                var sdIdInvalidBytes = new HashSet <byte>
                {
                    0x5D, // ]
                    0x22, // "
                    0x3D  // =
                };

                foreach (SyslogSdElement sd in message.StructuredDataElements.Where(sd => sd.Parameters.Any(p => !string.IsNullOrWhiteSpace(p.Value))))
                {
                    messageBuilder.Append("[");
                    messageBuilder.Append(SanitizeSyslogValue(sd.SdId, SdIdMaxLength, sdIdInvalidBytes));

                    foreach (SyslogSdParameter parameter in sd.Parameters.Where(p => !string.IsNullOrWhiteSpace(p.Value)))
                    {
                        messageBuilder.AppendFormat(" {0}=\"{1}\"", SanitizeSyslogValue(parameter.Name, ParamNameMaxLength, sdIdInvalidBytes), EscapeStructuredDataParameterValue(parameter.Value));
                    }

                    messageBuilder.Append("]");
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Writes the header.
 ///     See https://tools.ietf.org/html/rfc5424#section-6
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="messageBuilder">The message builder.</param>
 private void WriteHeader(SyslogMessage message, StringBuilder messageBuilder)
 {
     messageBuilder.AppendFormat(@"<{0}>{1} {2} {3} {4} {5} {6} ",
                                 message.Priority,
                                 message.Version,
                                 message.Timestamp?.UtcDateTime.ToString(TimestampFormat) ?? NilValue,
                                 SanitizeSyslogValue(message.HostName, HostNameMaxLength),
                                 SanitizeSyslogValue(message.AppName, AppNameMaxLength),
                                 SanitizeSyslogValue(message.ProcId, ProcIdMaxLength),
                                 SanitizeSyslogValue(message.MsgId, MsgIdMaxLength));
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Serializes the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        private byte[] SerializeMessage(SyslogMessage message)
        {
            byte[] data;

            using (var stream = new MemoryStream())
            {
                _serializer.Serialize(message, stream);

                data = stream.ToArray();
            }

            return(data);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Writes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <exception cref="System.ArgumentNullException">message</exception>
        public void Write(SyslogMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (_disposed || !_canWriteMessages)
            {
                return;
            }

            _messageQueue.TryAdd(message);
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Writes the message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="messageBuilder">The message builder.</param>
        private void WriteMessage(SyslogMessage message, StringBuilder messageBuilder)
        {
            if (string.IsNullOrWhiteSpace(message.Message))
            {
                return;
            }

            messageBuilder.Append(" ");

            // Remove all non visible white space
            var serializedMsg = _nonVisibleWhiteSpaceRegEx.Replace(message.Message, "");

            // Convert new lines to spaces to maintain readability of message
            serializedMsg = _newLineRegex.Replace(serializedMsg, " ");

            messageBuilder.Append(serializedMsg);
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Write the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void Write(SyslogMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                Stream stream = _streamProvider.GetStream();

                byte[] data = SerializeMessage(message);

                stream.Write(data, 0, data.Length);
            }
            catch
            {
                _streamProvider.CloseStream();
                throw;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Serializes the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="stream">The stream.</param>
        /// <exception cref="System.ArgumentNullException">
        ///     message
        ///     or
        ///     stream
        /// </exception>
        public void Serialize(SyslogMessage message, Stream stream)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var messageBuilder = new StringBuilder();

            WriteHeader(message, messageBuilder);
            WriteStructuredData(message, messageBuilder);
            WriteMessage(message, messageBuilder);

            messageBuilder.Append(MessageSeparator);

            WriteToStream(messageBuilder, stream);
        }