Esempio n. 1
0
        /// <summary>
        /// Publishes a Gelf message to the specified Graylog server.
        /// </summary>
        public void Publish(GelfMessage message)
        {
            var serializedGelfMessage = JsonConvert.SerializeObject(message, Formatting.Indented);

            byte[] bytes = serializedGelfMessage.GzipMessage(Constants.Encoding);

            if (Constants.MaxChunkSize < bytes.Length)
            {
                var chunkCount = (bytes.Length / Constants.MaxChunkSize) + 1;

                if (chunkCount > Constants.MaxChunkCount)
                {
                    throw new GelfMessageTooBigException($"Message of size {bytes.Length} will break up into {chunkCount} chunks, which is greater than maximum of GELF standard {Constants.MaxChunkCount}");
                }

                var messageId = ChunkedMessageHelper.GenerateMessageId();
                var state     = new UdpState {
                    SendClient = client, Bytes = bytes, ChunkCount = chunkCount, MessageId = messageId, SendIndex = 0
                };
                var messageChunkFull = ChunkedMessageHelper.GetMessageChunkFull(state.Bytes, state.MessageId, state.SendIndex, state.ChunkCount);
                client.BeginSend(messageChunkFull, messageChunkFull.Length, SendCallback, state);
            }
            else
            {
                var state = new UdpState {
                    SendClient = client, Bytes = bytes, ChunkCount = 0, MessageId = string.Empty, SendIndex = 0
                };
                client.BeginSend(bytes, bytes.Length, SendCallback, state);
            }
        }
Esempio n. 2
0
        private void SendCallback(IAsyncResult ar)
        {
            var state = (UdpState)ar.AsyncState;
            var u     = state.SendClient;

            var bytesSent = u.EndSend(ar);

            Debug.WriteLine($"Async bytes sent {bytesSent} chunk {state.SendIndex} of {state.ChunkCount}");

            state.SendIndex++;
            if (state.SendIndex < state.ChunkCount)
            {
                var messageChunkFull = ChunkedMessageHelper.GetMessageChunkFull(state.Bytes, state.MessageId, state.SendIndex, state.ChunkCount);
                state.SendClient.BeginSend(messageChunkFull, messageChunkFull.Length, SendCallback, state);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Publishes a Gelf message to the specified Graylog server.
        /// </summary>
        public void Publish(GelfMessage message)
        {
            var serializedGelfMessage = JsonConvert.SerializeObject(message, Formatting.Indented);

            byte[] bytes = serializedGelfMessage.GzipMessage(Constants.Encoding);

            if (Constants.MaxChunkSize < bytes.Length)
            {
                var chunkCount = (bytes.Length / Constants.MaxChunkSize) + 1;
                var messageId  = ChunkedMessageHelper.GenerateMessageId();
                var state      = new UdpState {
                    SendClient = client, Bytes = bytes, ChunkCount = chunkCount, MessageId = messageId, SendIndex = 0
                };
                var messageChunkFull = ChunkedMessageHelper.GetMessageChunkFull(state.Bytes, state.MessageId, state.SendIndex, state.ChunkCount);
                client.BeginSend(messageChunkFull, messageChunkFull.Length, SendCallback, state);
            }
            else
            {
                var state = new UdpState {
                    SendClient = client, Bytes = bytes, ChunkCount = 0, MessageId = string.Empty, SendIndex = 0
                };
                client.BeginSend(bytes, bytes.Length, SendCallback, state);
            }
        }