示例#1
0
        public HttpLogShipper(
            string bufferBaseFilename,
            int batchPostingLimit,
            TimeSpan period,
            long?eventBodyLimitBytes,
            LoggingLevelSwitch levelControlSwitch,
            long?retainedInvalidPayloadsLimitBytes,
            Encoding encoding)
        {
            _batchPostingLimit   = batchPostingLimit;
            _eventBodyLimitBytes = eventBodyLimitBytes;
            _controlledSwitch    = new ControlledLevelSwitch(levelControlSwitch);
            _connectionSchedule  = new ExponentialBackoffConnectionSchedule(period);
            _retainedInvalidPayloadsLimitBytes = retainedInvalidPayloadsLimitBytes;
            _encoding = encoding;

            _logglyClient = new LogglyClient(); //we'll use the loggly client instead of HTTP directly

            _bookmarkFilename    = Path.GetFullPath(bufferBaseFilename + ".bookmark");
            _logFolder           = Path.GetDirectoryName(_bookmarkFilename);
            _candidateSearchPath = Path.GetFileName(bufferBaseFilename) + "*.json";

            _timer = new PortableTimer(c => OnTick());
            SetTimer();
        }
示例#2
0
        /// <summary>
        /// Construct a sink that saves logs to the specified storage account. Properties are being send as data and the level is used as tag.
        /// </summary>
        /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
        /// <param name="period">The time to wait between checking for event batches.</param>
        ///  <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        public LogglySink(IFormatProvider formatProvider, int batchSizeLimit, TimeSpan period)
            : base(batchSizeLimit, period)
        {
            _formatProvider = formatProvider;

            _client = new LogglyClient();
        }
示例#3
0
        static void Main(string[] args)
        {
            ExcludeFields ef = new ExcludeFields()
            {
                FirstName = "Jack", LastName = "Makan", EmployeeID = 100, Designation = "SSE", Address = "United States"
            };
            List <string> discardedFieldsList = new List <string>();

            discardedFieldsList.Add("FirstName");
            discardedFieldsList.Add("Designation");
            discardedFieldsList.Add("Address");
            var log = new LoggerConfiguration()
                      .WriteTo.Loggly()
                      .CreateLogger();
            ILogglyClient _loggly  = new LogglyClient();
            var           logEvent = new LogglyEvent();

            logEvent.Data.Add("message", "Simple message at {0}");
            logEvent.Data.Add("context", ef.excludeFields(JsonConvert.SerializeObject(ef), discardedFieldsList));
            logEvent.Data.Add("Error", new Exception("your exception message"));
            for (int i = 0; i < 10; i++)
            {
                _loggly.Log(logEvent);
            }
            Console.ReadKey();
        }
        public HttpLogShipper(
            string bufferBaseFilename,
            int batchPostingLimit,
            TimeSpan period, long?
            eventBodyLimitBytes,
            LoggingLevelSwitch levelControlSwitch,
            long?retainedInvalidPayloadsLimitBytes,
            Encoding encoding,
            int?retainedFileCountLimit)
        {
            _batchPostingLimit      = batchPostingLimit;
            _retainedFileCountLimit = retainedFileCountLimit;

            _controlledSwitch   = new ControlledLevelSwitch(levelControlSwitch);
            _connectionSchedule = new ExponentialBackoffConnectionSchedule(period);

            _logglyClient = new LogglyClient(); //we'll use the loggly client instead of HTTP directly

            //create necessary path elements
            var candidateSearchPath = Path.GetFileName(bufferBaseFilename) + "*.json";
            var logFolder           = Path.GetDirectoryName(candidateSearchPath);

            //Filebase is currently the only option available so we will stick with it directly (for now)
            var encodingToUse    = encoding;
            var bookmarkProvider = new FileBasedBookmarkProvider(bufferBaseFilename, _fileSystemAdapter, encoding);

            _bufferDataProvider   = new FileBufferDataProvider(bufferBaseFilename, _fileSystemAdapter, bookmarkProvider, encodingToUse, batchPostingLimit, eventBodyLimitBytes, retainedFileCountLimit);
            _invalidPayloadLogger = new InvalidPayloadLogger(logFolder, encodingToUse, _fileSystemAdapter, retainedInvalidPayloadsLimitBytes);

            _timer = new PortableTimer(c => OnTick());
            SetTimer();
        }
        public void SendsOnlyProperPartOfMessageBuffer()
        {
            var client = new LogglyClient(new Config());

            client.Send(new[] { "message 1", "message 2", "message 3" }, 2);

            var message = Encoding.UTF8.GetString(_messageStream.ToArray());

            message.Should().Be($"message 1{Environment.NewLine}message 2");
        }
        public void SendsCorrectData()
        {
            var client = new LogglyClient(new Config());

            client.Send(new[] { "Test message to be sent" }, 1);

            var message = Encoding.UTF8.GetString(_messageStream.ToArray());

            message.Should().Be("Test message to be sent");
        }
示例#7
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account. Properties are being send as data and the level is used as tag.
 /// </summary>
 /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
 /// <param name="period">The time to wait between checking for event batches.</param>
 ///  <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 /// <param name="logglyConfig">Used to configure underlying LogglyClient programmaticaly. Otherwise use app.Config.</param>
 /// <param name="includes">Decides if the sink should include specific properties in the log message</param>
 public LogglySink(IFormatProvider formatProvider, int batchSizeLimit, TimeSpan period, LogglyConfiguration logglyConfig, LogIncludes includes)
     : base(batchSizeLimit, period)
 {
     if (logglyConfig != null)
     {
         _adapter = new LogglyConfigAdapter();
         _adapter.ConfigureLogglyClient(logglyConfig);
     }
     _client    = new LogglyClient();
     _converter = new LogEventConverter(formatProvider, includes);
 }
        protected override void Write(LogEventInfo logEvent)
        {
            var loggly = new LogglyClient();

            // The unwrapped event has a zero sequenceId, grab it before unwrapping;
            var sequenceId = logEvent.SequenceID;

            logEvent = GetCorrectEvent(logEvent);

            if (logEvent.Properties.ContainsKey("syslog-suppress"))
            {
                /*
                 * logging delimiting messages like "--------------" makes sense for pretty printing to file log targets
                 * but not so much for loggly. Support suppression.
                 */
                return;
            }

            var logMessage = Layout.Render(logEvent);

            var logglyEvent     = new LogglyEvent();
            var isHttpTransport = LogglyConfig.Instance.Transport.LogTransport == LogTransport.Https;

            logglyEvent.Timestamp        = logEvent.TimeStamp;
            logglyEvent.Syslog.MessageId = sequenceId;
            logglyEvent.Syslog.Level     = ToSyslogLevel(logEvent.Level);

            if (logEvent.Exception != null)
            {
                logglyEvent.Data.Add("exception", logEvent.Exception);
            }

            if (isHttpTransport)
            {
                // syslog will capture these via the header
                logglyEvent.Data.Add("sequenceId", sequenceId);
                logglyEvent.Data.Add("level", logEvent.Level.Name);
            }

            logglyEvent.Data.Add("message", logMessage);
            foreach (var tag in Tags.Split(','))
            {
                logglyEvent.Options.Tags.Add(new SimpleTag {
                    Value = tag
                });
            }

            foreach (var key in logEvent.Properties.Keys)
            {
                logglyEvent.Data.AddIfAbsent(key.ToString(), logEvent.Properties[key]);
            }

            loggly.Log(logglyEvent);
        }
        public void SendsToProperUrl()
        {
            var config = new Config
            {
                RootUrl       = "https://logs01.loggly.test",
                CustomerToken = "customer-token",
                Tag           = "tag1,tag2",
                UserAgent     = "user-agent"
            };
            var client = new LogglyClient(config);

            client.Send(new[] { "test message" }, 1);

            _usedUrl.Should().Be($"https://logs01.loggly.test/bulk/customer-token/tag/tag1,tag2,user-agent");
        }
        public void DoesNotRetrySendWhenTokenIsInvalid()
        {
            var forbiddenResponse = new Mock <HttpWebResponse>();

            forbiddenResponse.SetupGet(x => x.StatusCode).Returns(HttpStatusCode.Forbidden);

            _webRequestMock.Setup(x => x.GetResponse())
            .Throws(new WebException("test-error", null, WebExceptionStatus.ProtocolError,
                                     forbiddenResponse.Object));

            var client = new LogglyClient(new Config());

            client.Send(new[] { "test message" }, 1);

            _webRequestMock.Verify(x => x.GetResponse(), Times.Once, "Invalid token should not be retried");
        }
        public void CreatesProperWebRequest()
        {
            var config = new Config
            {
                TimeoutInSeconds = 12,
                UserAgent        = "test-agent"
            };
            var request = (HttpWebRequest)LogglyClient.CreateWebRequest(config, "http://test-url");

            request.Should().BeEquivalentTo(new
            {
                Method           = "POST",
                ReadWriteTimeout = 12000,
                Timeout          = 12000,
                ContentType      = "application/json",
                UserAgent        = "test-agent",
                KeepAlive        = true
            }, x => x.ExcludingMissingMembers());
        }
示例#12
0
        public async Task SelfReferencingLoopShouldNotFailMessageSerialization()
        {
            var transportMock = new Mock <IMessageTransport>();

            transportMock.Setup(x => x.Send(It.IsAny <IEnumerable <LogglyMessage> >()))
            .Callback((IEnumerable <LogglyMessage> x) => x.ToList())    //making sure the messages are evaluated
            .Returns(() => Task.FromResult(new LogResponse {
                Code = ResponseCode.Success
            }));

            var logglyEvent = new LogglyEvent();

            logglyEvent.Data.AddIfAbsent("ReferenceLoopType", new TypeWithReferenceLoopParent());

            var client = new LogglyClient(transportMock.Object);

            var res = await client.Log(logglyEvent);

            Assert.AreEqual(ResponseCode.Success, res.Code);
        }
        public void RetriesSendWhenErrorOccurs()
        {
            var notFoundResponse = new Mock <HttpWebResponse>();

            notFoundResponse.SetupGet(x => x.StatusCode).Returns(HttpStatusCode.NotFound);

            _webRequestMock.Setup(x => x.GetResponse())
            .Throws(new WebException("test-error", null, WebExceptionStatus.ProtocolError,
                                     notFoundResponse.Object));

            var config = new Config
            {
                MaxSendRetries = 3
            };
            var client = new LogglyClient(config);

            client.Send(new[] { "test message" }, 1);

            _webRequestMock.Verify(x => x.GetResponse(), Times.Exactly(config.MaxSendRetries + 1),
                                   "Generic send error should be retried");
        }
示例#14
0
        public void LogglyClientSendException()
        {
            var config = LogglyConfig.Instance;

            config.CustomerToken   = "83fe7674-f87d-473e-a8af-bbbbbbbbbbbb";
            config.ApplicationName = $"test";

            config.Transport.EndpointHostname = "logs-01.loggly.com";
            config.Transport.EndpointPort     = 443;
            config.Transport.LogTransport     = LogTransport.Https;

            var ct = new ApplicationNameTag {
                Formatter = "application-{0}"
            };

            config.TagConfig.Tags.Add(ct);
            var logglyClient = new LogglyClient();


            try
            {
                ThrowException();
            }
            catch (Exception e)
            {
                LogglyEvent logglyEvent = new LogglyEvent
                {
                    Timestamp = DateTimeOffset.UtcNow,
                    Syslog    = { Level = Level.Emergency }
                };
                logglyEvent.Data.AddIfAbsent("Message", "xZx");
                logglyEvent.Data.AddIfAbsent("Level", "Error");
                logglyEvent.Data.AddIfAbsent("Exception", e);

                var res = logglyClient.Log(logglyEvent).Result;
                Assert.Equal(ResponseCode.Success, res.Code);
            }
            Thread.Sleep(5000);
        }
示例#15
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account. Properties are being send as data and the level is used as tag.
 /// </summary>
 /// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
 /// <param name="period">The time to wait between checking for event batches.</param>
 ///  <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
 public LogglySink(IFormatProvider formatProvider, int batchSizeLimit, TimeSpan period)
     : base(batchSizeLimit, period)
 {
     _client    = new LogglyClient();
     _converter = new LogEventConverter(formatProvider);
 }
示例#16
0
 static void printLogStatus(Object o)
 {
     Console.WriteLine(LogglyClient.getLogStatus());
     GC.Collect();
 }
示例#17
0
        /// <summary>
        /// Construct a sink that saves logs to the specified storage account. Properties are being send as data and the level is used as tag.
        /// </summary>
        ///  <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
        public LogglySink(IFormatProvider formatProvider)
        {
            _formatProvider = formatProvider;

            _client = new LogglyClient();
        }