Ejemplo n.º 1
0
        public static void With1Listener(IEnumerable<EventSource> loggers, Action<ObservableEventListener, InMemoryEventListener> scenario)
        {
            using (var errorsListener = new InMemoryEventListener())
            using (var listener = new ObservableEventListener())
            {
                try
                {
                    errorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Verbose, Keywords.All);

                    scenario(listener, errorsListener);
                }
                finally
                {
                    foreach (var logger in loggers)
                    {
                        try
                        { listener.DisableEvents(logger); }
                        catch
                        { }
                    }

                    errorsListener.DisableEvents(SemanticLoggingEventSource.Log);
                }
            }
        }
Ejemplo n.º 2
0
        public void TestExporter() {
            var listener = new ObservableEventListener();
            listener.EnableEvents(TflEventSource.Log, EventLevel.Verbose);
            var subscription = listener.LogToConsole(new LegacyLogFormatter());

            var file = Path.GetTempFileName();
            File.WriteAllText(file, @"t1,t2,t3,t4
Monday,10,1.1,1/1/2014
Tuesday,11,2.2,2/1/2014
Wednesday,12,3.3,3/1/2014
Wednesday,12,3.3,3/1/2014
Thursday,13,4.4,4/1/2014
Friday,14,5.5,5/1/2014
Saturday,15,6.6,6/1/2014");

            File.Delete(OUTPUT);

            var profile = new Profiler().Profile(file);
            new ProfileExporter().Export(profile, OUTPUT);

            subscription.Dispose();
            listener.DisableEvents(TflEventSource.Log);
            listener.Dispose();

            Assert.IsTrue(File.Exists(OUTPUT));
        }
Ejemplo n.º 3
0
        static void Main(string[] args) {

            var listener = new ObservableEventListener();
            listener.EnableEvents(TflEventSource.Log, EventLevel.Informational);
            var subscription = listener.LogToConsole(new LegacyLogFormatter());

            ValidateArguments(args);

            new ProfileExporter().Export(new Profiler().Profile(_input, _sample), _output);

            subscription.Dispose();
            listener.DisableEvents(TflEventSource.Log);
            listener.Dispose();
        }
Ejemplo n.º 4
0
    static void Main(string[] args)
    {
      ObservableEventListener listener = new ObservableEventListener();
      listener.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);

      listener.LogToConsole();

      // Modify these settings to match your SMTP service requirements.
      listener.LogToEmail("smtp.live.com", 587, "*****@*****.**", "In Proc Sample", "etw");

      MyCompanyEventSource.Log.Failure("No response from servers, general network failure!!");

      listener.DisableEvents(MyCompanyEventSource.Log);
      listener.Dispose();
    }
Ejemplo n.º 5
0
    static void SimpleEventSource()
    {
      // Set up and enable the event listener - typically done when the application starts
      var listener = new ObservableEventListener();
      listener.LogToConsole();
      listener.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);

      // Log some messages
      MyCompanyEventSource.Log.Startup();
      MyCompanyEventSource.Log.Failure("Couldn't connect to server.");
      Console.WriteLine("Written two log messages.\nUsing a basic console listener to capture them.");
      Console.WriteLine("The color is determined by the severity level.\n");
      
      // Disable the event listener - typically done when the application terminates
      listener.DisableEvents(MyCompanyEventSource.Log);
      listener.Dispose();
     }
Ejemplo n.º 6
0
        public static void With2Listeners(EventSource logger, Action<ObservableEventListener, ObservableEventListener> scenario)
        {
            using (var listener1 = new ObservableEventListener())
            using (var listener2 = new ObservableEventListener())
            {
                try
                {
                    scenario(listener1, listener2);
                }
                finally
                {
                    try
                    { listener1.DisableEvents(logger); }
                    catch
                    { }

                    try
                    { listener2.DisableEvents(logger); }
                    catch
                    { }
                }
            }
        }
        public void CanDisableEventsOnNonEnabledNonCreatedEventSource()
        {
            var eventSourceName = "SensingEventSource";

            using (EventListener listener = new ObservableEventListener())
            {
                listener.DisableEvents(eventSourceName);

                Assert.IsFalse(SensingEventSource.Log.IsEnabled());
            }
        }
        public void CanDisableEventsOnEnabledEventSourceBeforeItIsCreated()
        {
            var eventSourceName = "SensingEventSource";

            using (EventListener listener = new ObservableEventListener())
            {
                listener.EnableEvents(eventSourceName, EventLevel.Error, EventKeywords.AuditSuccess | EventKeywords.AuditFailure);
                listener.EnableEvents(eventSourceName, EventLevel.Informational, EventKeywords.Sqm, new Dictionary<string, string> { { "key1", "value1" } });
                listener.EnableEvents(eventSourceName, EventLevel.Warning, EventKeywords.EventLogClassic, new Dictionary<string, string> { { "key2", "value2" } });
                listener.DisableEvents(eventSourceName);

                Assert.IsFalse(SensingEventSource.Log.IsEnabled());
                Assert.AreEqual(0, SensingEventSource.Log.Commands.Count);
            }
        }
        public void CanDeferRequestsForMultipleSources()
        {
            using (EventListener listener = new ObservableEventListener())
            {
                listener.EnableEvents("DeferredEventSource1", EventLevel.Error, EventKeywords.AuditSuccess | EventKeywords.AuditFailure);
                listener.EnableEvents("DeferredEventSource2", EventLevel.Informational, EventKeywords.Sqm, new Dictionary<string, string> { { "key1", "value1" } });
                listener.EnableEvents("DeferredEventSource3", EventLevel.Warning, EventKeywords.EventLogClassic, new Dictionary<string, string> { { "key2", "value2" } });
                listener.EnableEvents("DeferredEventSource2", EventLevel.Warning, EventKeywords.EventLogClassic, new Dictionary<string, string> { { "key2", "value2" } });
                listener.EnableEvents("DeferredEventSource1", EventLevel.Warning, EventKeywords.EventLogClassic, new Dictionary<string, string> { { "key2", "value2" } });
                listener.EnableEvents("DeferredEventSource1", EventLevel.Informational, EventKeywords.Sqm, new Dictionary<string, string> { { "key1", "value1" } });
                listener.DisableEvents("DeferredEventSource2");
                listener.EnableEvents("DeferredEventSource2", EventLevel.Error, EventKeywords.AuditSuccess | EventKeywords.AuditFailure);
                listener.DisableEvents("DeferredEventSource3");

                Assert.IsTrue(DeferredEventSource1.Log.IsEnabled(EventLevel.Informational, EventKeywords.Sqm));
                Assert.IsFalse(DeferredEventSource1.Log.IsEnabled(EventLevel.Verbose, EventKeywords.Sqm));
                Assert.IsTrue(DeferredEventSource2.Log.IsEnabled(EventLevel.Error, EventKeywords.AuditSuccess));
                Assert.IsFalse(DeferredEventSource2.Log.IsEnabled(EventLevel.Warning, EventKeywords.AuditSuccess));
                Assert.IsFalse(DeferredEventSource3.Log.IsEnabled());
            }
        }
Ejemplo n.º 10
0
        public void WhenEventsInThreeConsecutiveIntervals()
        {
            this.tableName = "WhenEventsInThreeConsecutiveIntervals";
            var connectionString = System.Configuration.ConfigurationManager.AppSettings["StorageConnectionString"];

            AzureTableHelper.DeleteTable(connectionString, this.tableName);
            var logger = TestEventSource.Logger;

            var bufferingInterval = TimeSpan.FromSeconds(5);
            var insertionInterval = TimeSpan.FromSeconds(1);

            using (var listener = new ObservableEventListener())
                using (var errorsListener = new InMemoryEventListener())
                {
                    try
                    {
                        errorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Verbose);
                        listener.LogToWindowsAzureTable("mytestinstance", connectionString, this.tableName, bufferingInterval: bufferingInterval);
                        listener.EnableEvents(logger, EventLevel.Informational);

                        // 1st interval: Log 10 events
                        for (int i = 0; i < 10; i++)
                        {
                            logger.Informational("Message1");
                        }

                        // 1st interval: Wait for the buffer to flush at end of interval
                        Task.Delay(bufferingInterval).Wait();
                        // 2nd interval: start

                        // 1st interval: Wait for the events to be written and assert
                        Task.Delay(insertionInterval).Wait();
                        Assert.AreEqual(10, AzureTableHelper.GetEventsCount(connectionString, this.tableName));

                        // 2nd interval: Log 10 events
                        for (int i = 0; i < 10; i++)
                        {
                            logger.Informational("Message1");
                        }

                        // 2nd interval: Wait for the buffer to flush at end of interval
                        Task.Delay(bufferingInterval).Wait();
                        // 3rd interval: start

                        // 2nd interval: Wait for the events to be written and assert
                        Task.Delay(insertionInterval).Wait();
                        Assert.AreEqual(20, AzureTableHelper.GetEventsCount(connectionString, this.tableName));

                        // 3rd interval: Log 10 events
                        for (int i = 0; i < 10; i++)
                        {
                            logger.Informational("Message1");
                        }

                        // 3rd interval: Wait for the buffer to flush at end of interval
                        Task.Delay(bufferingInterval).Wait();
                        // 4th interval: start

                        // 3rd interval: Wait for the events to be written and assert
                        Task.Delay(insertionInterval).Wait();
                        Assert.AreEqual(30, AzureTableHelper.GetEventsCount(connectionString, this.tableName));

                        // No errors should have been reported
                        Assert.AreEqual(string.Empty, errorsListener.ToString());
                    }
                    finally
                    {
                        listener.DisableEvents(logger);
                        errorsListener.DisableEvents(SemanticLoggingEventSource.Log);
                    }
                }

            // No more events should have been written during the last flush in the Dispose
            Assert.AreEqual(30, AzureTableHelper.GetEventsCount(connectionString, this.tableName));
        }
Ejemplo n.º 11
0
    static void MultipleEventListenersLevel()
    {
      // Set up and enable the event listeners - typically done when the application starts
      var listener1 = new ObservableEventListener();
      var listener2 = new ObservableEventListener();
      listener1.EnableEvents(MyCompanyEventSource.Log, EventLevel.Error, Keywords.All);
      listener2.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);
      listener1.LogToConsole();

      var subscription = listener2.LogToFlatFile(@"C:\Temp\DemoSemanticLogging.log", new EventTextFormatter("===================", "==================="));

      // Log some messages
      MyCompanyEventSource.Log.Startup();
      MyCompanyEventSource.Log.Failure("Couldn't connect to server.");
      Console.WriteLine("Written two log messages.\nUsing a basic console listener and a Flat File listener to capture them.");
      Console.WriteLine("Only the critical message appears in the console, both appear in: \nC:\\Temp\\DemoSemanticLogging.log.\n");
      
      // Flush the sink
      subscription.Sink.FlushAsync().Wait();

      // Disable the event listener - typically done when the application terminates
      listener1.DisableEvents(MyCompanyEventSource.Log);
      listener2.DisableEvents(MyCompanyEventSource.Log);

      listener1.Dispose();
      listener2.Dispose();
    }
Ejemplo n.º 12
0
    static void SimpleEventSourceWithColorMapper()
    {
      // Set up and enable the event listener - typically done when the application starts
      var listener = new ObservableEventListener();
      listener.LogToConsole(new JsonEventTextFormatter(EventTextFormatting.Indented), new MyCustomColorMapper());
      listener.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);

      // Log some messages
      MyCompanyEventSource.Log.Startup();
      MyCompanyEventSource.Log.PageStop(99);
      Console.WriteLine("Written two log messages.\nUsing a console listener with a JSON formatter to capture them.");

      // Disable the event listener - typically done when the application terminates
      listener.DisableEvents(MyCompanyEventSource.Log);
      listener.Dispose();
    }
Ejemplo n.º 13
0
    static void SimpleEventSourceWithXMLFormatter()
    {
      // Set up and enable the event listener - typically done when the application starts
      var listener = new ObservableEventListener();
      listener.LogToConsole(new XmlEventTextFormatter(EventTextFormatting.Indented));
      listener.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);

      // Log some messages
      MyCompanyEventSource.Log.Startup();
      MyCompanyEventSource.Log.Failure("Couldn't connect to server.");
      Console.WriteLine("Written two log messages.\nUsing a console listener with an XML formatter to capture them.");

      // Disable the event listener - typically done when the application terminates
      listener.DisableEvents(MyCompanyEventSource.Log);
      listener.Dispose();
    }
Ejemplo n.º 14
0
 /// <summary>
 /// Disable events for this event source.
 /// </summary>
 public static void DisableEvens()
 {
     listener.DisableEvents(Instance);
 }
        public void EventWithPayloadAndEnumsInXml()
        {
            string fileName = "LogUsingPayloadWithEnumsInProc";
            File.Delete(fileName);
            var formatter = new XmlEventTextFormatter(EventTextFormatting.Indented, "d");
            var logger = MockEventSourceInProcEnum.Logger;

            using (var listener = new ObservableEventListener())
            {
                listener.EnableEvents(logger, EventLevel.LogAlways, Keywords.All);
                listener.LogToFlatFile(fileName, formatter);
                try
                {
                    logger.SendEnumsEvent16(MockEventSourceInProcEnum.MyColor.Green, MockEventSourceInProcEnum.MyFlags.Flag1);
                }
                finally
                {
                    listener.DisableEvents(logger);
                }
            }

            var rawOutput = FlatFileHelper.GetAllText(fileName);
            var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements();
            Assert.AreEqual(1, entries.Count());
            XmlFormattedEntry.Fill(entries.First());
            Assert.AreEqual("a", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value);
            Assert.AreEqual("2", XmlFormattedEntry.Payload.Elements().First().Value);
            Assert.AreEqual("b", XmlFormattedEntry.Payload.Elements().Last().Attribute("Name").Value);
            Assert.AreEqual("1", XmlFormattedEntry.Payload.Elements().Last().Value);
        }
Ejemplo n.º 16
0
        //[Ignore("Depends on NorthWind database on local SQL Server.")]
        public void TestProfileAndExportDatabaseTable() {

            var listener = new ObservableEventListener();
            listener.EnableEvents(TflEventSource.Log, EventLevel.Verbose);
            var subscription = listener.LogToConsole(new LegacyLogFormatter());

            var result = new Profiler().Profile("localhost.NorthWind.dbo.Customers");
            new ProfileExporter().Export(result);

            subscription.Dispose();
            listener.DisableEvents(TflEventSource.Log);
            listener.Dispose();

            Assert.NotNull(result);
        }
Ejemplo n.º 17
0
    static void MultipleEventListenersKeywords()
    {
      var listener1 = new ObservableEventListener();
      var listener2 = new ObservableEventListener();
      listener1.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, MyCompanyEventSource.Keywords.Perf | MyCompanyEventSource.Keywords.Diagnostic);
      listener2.EnableEvents(MyCompanyEventSource.Log, EventLevel.LogAlways, Keywords.All);
      // Set up and enable the event listeners -  typically done when the application starts
      listener1.LogToConsole();
      // The SinkSubscription is used later to flush the buffer
      var subscription = listener2.LogToSqlDatabase("Demo Semantic Logging Instance", connectionString);

      // Log some messages
      MyCompanyEventSource.Log.PageStart(23, "http://mysite/demopage");
      MyCompanyEventSource.Log.Startup();
      MyCompanyEventSource.Log.Failure("Couldn't connect to server.");
      Console.WriteLine("Written three log messages.\nUsing a basic console listener and a SQL listener to capture them.");
      Console.WriteLine("Only the messages with the Perf or Diagnostic keywords appears in the console, \nall three appear in the SQL Database:\n");

      // Disable the event listener - typically done when the application terminates
      listener1.DisableEvents(MyCompanyEventSource.Log);
      listener2.DisableEvents(MyCompanyEventSource.Log);

      // Manually flush the buffer so you can see what's in the database
      subscription.Sink.FlushAsync().Wait();
      ShowContentsOfSqlDatabaseTable();

      listener1.Dispose();
      listener2.Dispose();
    }
Ejemplo n.º 18
0
 private void UnregisterEvents()
 {
     eventListener.DisableEvents(SemanticLogging.EventSource);
 }