public void EventWithPayloadKeywrdsNoMsgInXml() { var logger = MockEventSrcForXml.Logger; var formatter = new XmlEventTextFormatter(); string rawOutput = string.Empty; using (var listener = new InMemoryEventListener(formatter)) { listener.EnableEvents(logger, EventLevel.LogAlways, MockEventSrcForXml.Keywords.Errors); try { logger.UsingKeywords(MockEventSrcForXml.LogMessage, long.MaxValue); rawOutput = listener.ToString(); } finally { listener.DisableEvents(logger); } } Assert.AreEqual(-1, rawOutput.IndexOf("\r\n")); var entries = XDocument.Parse("<Events>" + rawOutput + "</Events>").Root.Elements(); XmlFormattedEntry.Fill(entries.Single()); Assert.AreEqual<Guid>(EventSource.GetGuid(typeof(MockEventSrcForXml)), Guid.Parse(XmlFormattedEntry.Provider.Attribute("Guid").Value)); Assert.AreEqual<int>(MockEventSrcForXml.UsingKeywordsEventID, Convert.ToInt32(XmlFormattedEntry.EventId.Value)); Assert.AreEqual<byte>(0, Convert.ToByte(XmlFormattedEntry.Version.Value)); Assert.AreEqual<int>((int)EventLevel.Informational, Int32.Parse(XmlFormattedEntry.Level.Value)); Assert.AreEqual<int>((int)MockEventSrcForXml.Tasks.DBQuery, Int32.Parse(XmlFormattedEntry.Task.Value)); Assert.AreEqual<long>((long)MockEventSrcForXml.Keywords.Errors, Int64.Parse(XmlFormattedEntry.Keywords.Value.Replace("0x", string.Empty))); Assert.AreEqual<int>((int)EventOpcode.Start, Int32.Parse(XmlFormattedEntry.Opcode.Value)); Assert.AreEqual<int>(System.Diagnostics.Process.GetCurrentProcess().Id, Int32.Parse(XmlFormattedEntry.ProcessId.Value)); Assert.AreEqual<int>(ThreadHelper.GetCurrentUnManagedThreadId(), Int32.Parse(XmlFormattedEntry.ThreadId.Value)); DateTime dt; Assert.IsTrue(DateTime.TryParseExact(XmlFormattedEntry.TimeCreated.Attribute("SystemTime").Value, EventEntry.DefaultDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)); Assert.AreEqual(2, XmlFormattedEntry.Payload.Elements().Count()); Assert.AreEqual("message", XmlFormattedEntry.Payload.Elements().First().Attribute("Name").Value); Assert.AreEqual(MockEventSrcForXml.LogMessage, XmlFormattedEntry.Payload.Elements().First().Value); Assert.AreEqual("longArg", XmlFormattedEntry.Payload.Elements().Last().Attribute("Name").Value); Assert.AreEqual(long.MaxValue.ToString(), XmlFormattedEntry.Payload.Elements().Last().Value); }
public void WhenSinkAddedWithErrorDoesNotRecycle() { var fileName = "flatfileListenerOk.log"; File.Delete(fileName); var logger = MockEventSourceOutProc.Logger; var configFile = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Configurations\\Reconfiguration\\temp\\configFile.xml"; UpdateServiceConfigurationFile("Configurations\\Reconfiguration\\NoListener.xml", configFile); TraceEventServiceConfiguration svcConfiguration = TraceEventServiceConfiguration.Load(configFile, true); TestScenario.WithConfiguration( svcConfiguration, () => { using (var collectErrorsListener = new InMemoryEventListener()) { collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.LogAlways, Keywords.All); try { TraceSessionHelper.WaitAndAssertCountOfSessions("ServiceReconfig", 1); UpdateServiceConfigurationFile("Configurations\\Reconfiguration\\FlatFileListenerError.xml", configFile); TraceSessionHelper.WaitAndAssertCountOfSessions("ServiceReconfig-flatFileListener", 0); TraceSessionHelper.WaitAndAssertCountOfSessions("ServiceReconfig-2flatFileListener", 1); MockEventSourceOutProc.Logger.LogSomeMessage("Some informational from a new listener."); var entries2 = FlatFileHelper.PollUntilTextEventsAreWritten(fileName, 1, "======"); Assert.AreEqual(1, entries2.Count()); StringAssert.Contains(entries2.First(), "Some informational from a new listener."); collectErrorsListener.WaitEvents.Wait(TimeSpan.FromSeconds(3)); StringAssert.Contains(collectErrorsListener.ToString(), "One or more errors occurred when loading the TraceEventService configuration file."); StringAssert.Contains(collectErrorsListener.ToString(), "The given path's format is not supported."); StringAssert.Contains(collectErrorsListener.ToString(), "The configuration was partially successfully loaded. Check logs for further error details."); } finally { File.Delete(configFile); collectErrorsListener.DisableEvents(SemanticLoggingEventSource.Log); } } }); }
public void WhenCustomFormatterThrowsAnExceptionAndUsedConfig() { string fileName = "FlatFileOutProcCustomFormatterHandleExceptionViaConfig.log"; File.Delete(fileName); var logger = MockEventSourceOutProc.Logger; MockFormatter formatter = new MockFormatter(true); //this formatter throws TraceEventServiceConfiguration svcConfiguration = TraceEventServiceConfiguration.Load("Configurations\\CustomSink\\FlatFileCustomFormatter.xml"); TestScenario.WithConfiguration( svcConfiguration, () => { using (InMemoryEventListener collectErrorsListener = new InMemoryEventListener()) { try { collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.Error, Keywords.All); logger.LogSomeMessage("some message using formatter that throws"); collectErrorsListener.WaitEvents.Wait(5000); StringAssert.Contains(collectErrorsListener.ToString(), "Payload : [message : System.InvalidOperationException: Operation is not valid due to the current state of the object."); } finally { collectErrorsListener.DisableEvents(SemanticLoggingEventSource.Log); } } }); }
public void WhenCustomFormatterThrowsAnExceptionAndUsedProgramatically() { string fileName = "FlatFileOutProcCustomFormatterHandleException.log"; File.Delete(fileName); var logger = MockEventSourceOutProc.Logger; MockFormatter formatter = new MockFormatter(true); //this formatter throws EventSourceSettings settings = new EventSourceSettings("MockEventSourceOutProc", null, EventLevel.Informational); var subject = new EventEntrySubject(); subject.LogToFlatFile(fileName, formatter); SinkSettings sinkSettings = new SinkSettings("flatFileSink", subject, new List<EventSourceSettings>() { { settings } }); List<SinkSettings> sinks = new List<SinkSettings>() { { sinkSettings } }; TraceEventServiceConfiguration svcConfiguration = new TraceEventServiceConfiguration(sinks); TestScenario.WithConfiguration( svcConfiguration, () => { using (var collectErrorsListener = new InMemoryEventListener()) { try { collectErrorsListener.EnableEvents(SemanticLoggingEventSource.Log, EventLevel.LogAlways, Keywords.All); logger.LogSomeMessage("some message using formatter that throws"); collectErrorsListener.WaitEvents.Wait(5000); StringAssert.Contains(collectErrorsListener.ToString(), "Payload : [message : System.InvalidOperationException: Operation is not valid due to the current state of the object."); } finally { collectErrorsListener.DisableEvents(SemanticLoggingEventSource.Log); } } }); }