コード例 #1
0
 public void ShouldFailIfAllAttemptsFailed()
 {
     var message = new GelfMessage();
     for(var i = 0; i < retryCount; ++i)
         mainTransport.Expect(x => x.Send(message)).Throw(new Exception());
     retryingTransport.Send(message);
 }
コード例 #2
0
        public byte[] Serialize(GelfMessage message)
        {
            var duration = message.Timestamp.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var result   = new JsonObject
            {
                { "version", message.Version },
                { "host", message.Host },
                { "short_message", message.ShortMessage },
                { "full_message", message.FullMessage },
                { "timestamp", Math.Round(duration.TotalSeconds, 3, MidpointRounding.AwayFromZero) },
                { "level", (int)message.Level }
            };

            foreach (var additionalField in message.AdditionalFields)
            {
                var key         = additionalField.Key;
                var value       = additionalField.Value;
                var prefixedKey = key.StartsWith("_") ? key : "_" + key;
                if (!result.ContainsKey(prefixedKey))
                {
                    result.Add(prefixedKey, value);
                }
                else
                {
                    _logger.Debug(string.Format(
                                      "Duplicate key {0} found.  Key collisions are resolved by favoring the first key-value to be added.",
                                      prefixedKey));
                }
            }
            return(Encoding.UTF8.GetBytes(result.ToString()));
        }
コード例 #3
0
 public void ShouldSendAgainIfFirstAttempFailed()
 {
     var message = new GelfMessage();
     mainTransport.Expect(x => x.Send(message)).Throw(new Exception());
     mainTransport.Expect(x => x.Send(message)).TentativeReturn();
     retryingTransport.Send(message);
 }
コード例 #4
0
 public void ShouldSkipMessageIfSendFailed()
 {
     var countingTransport = new FailCountingTransport();
     var bufferedTransport = new BufferedTransport(new SilentLogger(), countingTransport);
     for (var i = 0; i < MessageCount; ++i)
     {
         var message = new GelfMessage();
         bufferedTransport.Send(message);
     }
     bufferedTransport.Close();
     Assert.AreEqual(MessageCount, countingTransport.Count);
 }
コード例 #5
0
 public void ShouldSerializerSimpleMessage()
 {
     var dateTime = new DateTime(2015, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
     var message = new GelfMessage
         {
             Host = "example.org",
             ShortMessage = "A short message that helps you identify what is going on",
             FullMessage = "Backtrace here\n\nmore stuff",
             Timestamp = dateTime,
             Level = GelfLevel.Alert,
         };
     var serializedMessage = Encoding.UTF8.GetString(serializer.Serialize(message));
     Assert.AreEqual("{\"version\":\"1.1\"," +
                     "\"host\":\"example.org\"," +
                     "\"short_message\":\"A short message that helps you identify what is going on\"," +
                     "\"full_message\":\"Backtrace here\\n\\nmore stuff\"," +
                     "\"timestamp\":1420070400," +
                     "\"level\":1}", serializedMessage);
 }
コード例 #6
0
 public byte[] Serialize(GelfMessage message)
 {
     var duration = message.Timestamp.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
     var result = new JsonObject
         {
             {"message_version", message.Version},
             {"host", message.Host},
             {"short_message", message.ShortMessage},
             {"full_message", message.FullMessage},
             {"timestamp", Math.Round(duration.TotalSeconds, 3, MidpointRounding.AwayFromZero)},
             {"level", (int)message.Level}
         };
     foreach (var additionalField in message.AdditionalFields)
     {
         var key = additionalField.Key;
         var value = additionalField.Value;
         result.Add(key.StartsWith("_") ? key : "_" + key, value);
     }
     return Encoding.UTF8.GetBytes(result.ToString());
 }
コード例 #7
0
        public byte[] Serialize(GelfMessage message)
        {
            var duration = message.Timestamp.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var result   = new JsonObject
            {
                { "version", message.Version },
                { "host", message.Host },
                { "short_message", message.ShortMessage },
                { "full_message", message.FullMessage },
                { "timestamp", Math.Round(duration.TotalSeconds, 3, MidpointRounding.AwayFromZero) },
                { "level", (int)message.Level }
            };

            foreach (var additionalField in message.AdditionalFields)
            {
                var key   = additionalField.Key;
                var value = additionalField.Value;
                result.Add(key.StartsWith("_") ? key : "_" + key, value);
            }
            return(Encoding.UTF8.GetBytes(result.ToString()));
        }
コード例 #8
0
 public void ShouldContinueWhenSendSuceed()
 {
     var message = new GelfMessage();
     mainTransport.Expect(x => x.Send(message)).TentativeReturn();
     retryingTransport.Send(message);
 }
コード例 #9
0
 public void Send(GelfMessage message)
 {
     Interlocked.Increment(ref count);
     throw new Exception();
 }
コード例 #10
0
 public void Send(GelfMessage message)
 {
     Interlocked.Increment(ref count);
 }