Пример #1
0
        public void EventWithPayloadAndMessageAndDateTimeFormatInJson()
        {
            var logger    = MockEventSrcForJson.Logger;
            var formatter = new JsonEventTextFormatter();

            formatter.DateTimeFormat = "dd/MM/yyyy";

            string rawOutput = string.Empty;

            using (var listener = new InMemoryEventListener()
            {
                Formatter = formatter
            })
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForJson.Keywords.Errors);
                try
                {
                    logger.LogUsingMessage(MockEventSrcForJson.LogMessage);
                    rawOutput = Encoding.Default.GetString(listener.Stream.ToArray());
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            string today    = System.DateTime.Today.ToString(formatter.DateTimeFormat);
            string tomorrow = System.DateTime.Today.AddDays(1).ToString(formatter.DateTimeFormat);

            Assert.IsTrue(rawOutput.Contains(today) || rawOutput.Contains(tomorrow));
        }
        public void when_creating_formatter_with_default_values()
        {
            var formatter = new JsonEventTextFormatter();

            Assert.IsNull(formatter.DateTimeFormat);
            Assert.AreEqual(EventTextFormatting.None, formatter.Formatting);
        }
 protected override void Given()
 {
     formatter = new JsonEventTextFormatter();
     listener  = new InMemoryEventListener()
     {
         Formatter = formatter
     };
     listener.EnableEvents(logger, EventLevel.LogAlways);
 }
        public void when_creating_formatter_with_null_dateTimeFormat()
        {
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented)
            {
                DateTimeFormat = null
            };

            Assert.IsNull(formatter.DateTimeFormat);
        }
Пример #5
0
        /// <summary>
        /// Creates the <see cref="IEventTextFormatter" /> instance.
        /// </summary>
        /// <returns>
        /// The event text formatter instance.
        /// </returns>
        public override IEventTextFormatter CreateEventTextFormatter()
        {
            var formatter = new JsonEventTextFormatter(this.Formatting)
            {
                DateTimeFormat = this.DateTimeFormat,
            };

            return(formatter);
        }
 protected override void Given()
 {
     formatter = new JsonEventTextFormatter()
     {
         IncludeEntrySeparator = false
     };
     collectionListener = new StringCollectionEventListener(formatter);
     collectionListener.EnableEvents(logger, EventLevel.LogAlways);
 }
        public void when_creating_formatter_with_specific_values()
        {
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented)
            {
                DateTimeFormat = "R"
            };

            Assert.AreEqual("R", formatter.DateTimeFormat);
            Assert.AreEqual(EventTextFormatting.Indented, formatter.Formatting);
        }
Пример #8
0
        private void ConfigureLogging()
        {
            eventListener = new ObservableEventListener();
            eventListener.EnableEvents(SparkEngineEventSource.Log, EventLevel.LogAlways,
                                       Keywords.All);
            eventListener.EnableEvents(SparkMongoEventSource.Log, EventLevel.LogAlways,
                                       Keywords.All);
            eventListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.LogAlways, Keywords.All);
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented);

            eventListener.LogToFlatFile(@"C:\projects\fhir\log\spark.log", formatter);
        }
Пример #9
0
        public override async Task Start()
        {
            await base.Start();

            // Calculate paths
            if (!Directory.Exists(_tempDirectory))
            {
                Directory.CreateDirectory(_tempDirectory);
            }

            // Generate an entirely unique file name
            string fileName = Invocation.Id.ToString("N") + "_" + Guid.NewGuid().ToString("N") + ".json";

            _tempFile = Path.Combine(_tempDirectory, fileName);
            if (File.Exists(_tempFile))
            {
                File.Delete(_tempFile);
            }

            // Locate the log blob
            _targetBlob = LogContainer.GetBlockBlobReference("invocations/" + _blobName);

            // Fetch the current logs if this is a continuation, we'll append to them during the invocation
            if (Invocation.IsContinuation && await _targetBlob.ExistsAsync())
            {
                await _targetBlob.DownloadToFileAsync(_tempFile, FileMode.Create);
            }

            // Capture the events into a JSON file and a plain text file
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented, dateTimeFormat: "O");

            _eventSubscription = this.Buffer(() =>
                                             Observable.Amb(
                                                 _flushBuffer,
                                                 Observable.Timer(TimeSpan.FromSeconds(5)).Select(x => Unit.Instance))
                                             .Take(1))
                                 .Subscribe(
                onNext: evts =>
            {
                // Dump to the temp file
                using (var writer = new StreamWriter(new FileStream(_tempFile, FileMode.Append, FileAccess.Write)))
                {
                    foreach (var evt in evts)
                    {
                        formatter.WriteEvent(evt, writer);
                    }
                }

                // Upload the temp file
                UploadLog().Wait();
            });
        }
        /// <summary>
        /// Creates the <see cref="IEventTextFormatter" /> instance.
        /// </summary>
        /// <param name="element">The configuration element.</param>
        /// <returns>
        /// The formatter instance.
        /// </returns>
        public IEventTextFormatter CreateFormatter(XElement element)
        {
            var formatter = this.GetFormatterElement(element);

            EventTextFormatting formatting = (EventTextFormatting)Enum.Parse(typeof(EventTextFormatting), (string)formatter.Attribute("formatting") ?? JsonEventTextFormatter.DefaultEventTextFormatting.ToString());

            var includeSeparator = formatter.Attribute("includeEntrySeparator");

            var jsonFormatter = new JsonEventTextFormatter(formatting, (string)formatter.Attribute("dateTimeFormat"));

            if (includeSeparator != null)
            {
                jsonFormatter.IncludeEntrySeparator = (bool)includeSeparator;
            }

            return(jsonFormatter);
        }
Пример #11
0
        static void Main()
        {
            Application.ThreadException +=
                new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //create formatter
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented);

            //TODO: Set up and enable the event listener
            using (var listener = new ObservableEventListener())
            {
                listener.LogToConsole(formatter);
                listener.EnableEvents(CalculatorEventSource.Log, EventLevel.LogAlways, Keywords.All);

                Form entryForm = new MainForm();
                Application.EnableVisualStyles();
                Application.Run(entryForm);
            }
        }
Пример #12
0
        static void Main()
        {
            Application.ThreadException +=
                new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //create formatter
            var formatter = new JsonEventTextFormatter(EventTextFormatting.Indented);

            using (var listener = new ObservableEventListener())
            {
                listener.LogToConsole(formatter);
                listener.LogToWindowsAzureTable("Calculator", "UseDevelopmentStorage=true;");
                listener.EnableEvents(CalculatorEventSource.Log, EventLevel.LogAlways, Keywords.All);

                Form entryForm = new MainForm();
                Application.EnableVisualStyles();
                Application.Run(entryForm);
            }
        }
Пример #13
0
        public void WhenEventWithMessageInAttributeUsingJson()
        {
            var consoleOutputInterceptor = new MockConsoleOutputInterceptor();
            var formatter     = new EventTextFormatter(EventTextFormatter.DashSeparator);
            var jsonFormatter = new JsonEventTextFormatter();

            jsonFormatter.DateTimeFormat = "dd/MM/yyyy";
            var logger = TestEventSourceNoAttributes.Logger;

            using (var eventListener = new ObservableEventListener())
            {
                eventListener.LogToConsole(jsonFormatter);
                eventListener.EnableEvents(logger, EventLevel.LogAlways);
                logger.ObjectArrayEvent4(1000, "stringstringarg10", 2000, "stringstringarg20", 3000);
            }

            var entry = Regex.Split(consoleOutputInterceptor.Ouput, formatter.Header).Where(c => !string.IsNullOrWhiteSpace(c)).SingleOrDefault();

            Assert.IsNotNull(entry);
            StringAssert.Contains(entry, "{\"arg0\":1000,\"arg1\":\"stringstringarg10\",\"arg2\":2000,\"arg3\":\"stringstringarg20\",\"arg4\":3000}");
            StringAssert.Contains(entry, "Check if it is logged");
        }
Пример #14
0
 protected override void When()
 {
     sut =
         FormatterElementFactory.Get(XElement.Parse(string.Format(@"<consoleSink xmlns='http://schemas.microsoft.com/practices/2013/entlib/semanticlogging/etw' name='ConsoleEventListener1'><sources><eventSource name='Foo' level='Error'/></sources>{0}</consoleSink>", GetElementText()))) as JsonEventTextFormatter;
 }