public void Writing_Message_To_Etw()
        {
            var resetEvent = new ManualResetEvent(false);
            var fpath      = Path.Combine(Path.GetTempPath(), "_etwnlogtest.etl");

            using (var session = new TraceEventSession("SimpleMonitorSession", fpath))
            {
                Thread.Sleep(1000);

                try
                {
                    session.EnableProvider(providerId);
                    Thread.Sleep(1000);
                }
                catch
                {
                    Thread.Sleep(1000);
                    session.EnableProvider(providerId);
                }

                Thread.Sleep(1000);

                // send events to session
                var logger = LogManager.GetLogger("A");
                logger.Debug("test-debug");
                logger.Info("test-info");
                logger.Warn("test-warn");
                logger.Error("test-error");
                logger.Fatal("test-fatal");

                try
                {
                    Thread.Sleep(1000);
                    session.DisableProvider(providerId);
                }
                catch
                {
                    Thread.Sleep(1000);
                    session.DisableProvider(providerId);
                }

                Thread.Sleep(1000);

                logger.Fatal("don't log this one");
            }

            var collectedEvents = new List <NLogEtwEventData>(5);

            using (var source = new ETWTraceEventSource(fpath))
            {
                source.UnhandledEvents += delegate(TraceEvent data)
                {
                    if (data.Level == TraceEventLevel.Always)
                    {
                        return;   // Not ours
                    }
                    collectedEvents.Add(new NLogEtwEventData(data)
                    {
                        Message = data.FormattedMessage,
                    });

                    if (collectedEvents.Count == 5)
                    {
                        resetEvent.Set();
                    }
                };
                source.Process();
            }
            File.Delete(fpath);

            var providerName = $"Provider({providerId.ToString()})";

            // assert collected events
            var expectedEvents = new NLogEtwEventData[] {
                new NLogEtwEventData {
                    ProviderName = providerName, TaskName = "EventWriteString", Level = TraceEventLevel.Verbose, Message = "DEBUG|A|test-debug"
                },
                new NLogEtwEventData {
                    ProviderName = providerName, TaskName = "EventWriteString", Level = TraceEventLevel.Informational, Message = "INFO|A|test-info"
                },
                new NLogEtwEventData {
                    ProviderName = providerName, TaskName = "EventWriteString", Level = TraceEventLevel.Warning, Message = "WARN|A|test-warn"
                },
                new NLogEtwEventData {
                    ProviderName = providerName, TaskName = "EventWriteString", Level = TraceEventLevel.Error, Message = "ERROR|A|test-error"
                },
                new NLogEtwEventData {
                    ProviderName = providerName, TaskName = "EventWriteString", Level = TraceEventLevel.Critical, Message = "FATAL|A|test-fatal"
                }
            };

            resetEvent.WaitOne(20000);
            Assert.Equal(expectedEvents.Length, collectedEvents.Count);
            Assert.Equal(expectedEvents, collectedEvents);
        }
        public void Writing_Exception_To_Etw()
        {
            var resetEvent      = new ManualResetEvent(false);
            var collectedEvents = new List <NLogEtwEventData>(5);

            var providerName = etwTarget.ProviderName.Render(LogEventInfo.CreateNullEvent());

            using (var session = new TraceEventSession("SimpleMonitorSession"))
            {
                // Dynamic-Parser does not work with pure EventSource-objects
                session.Source.Registered.All += delegate(TraceEvent data)
                {
                    if (data.Level == TraceEventLevel.Always)
                    {
                        return;   // Not ours
                    }
                    string exception = ((string)data.PayloadByName("Exception"));
                    exception = exception?.Substring(0, exception.IndexOf(Environment.NewLine)) ?? string.Empty;

                    collectedEvents.Add(new NLogEtwEventData(data)
                    {
                        EventId    = 0, // Raw EventSource gives "random" EventId
                        LoggerName = (string)data.PayloadByName("LoggerName"),
                        Message    = (string)data.PayloadByName("Message") + "|" + exception,
                    });

                    if (collectedEvents.Count == 1)
                    {
                        resetEvent.Set();
                    }
                };
                session.Source.UnhandledEvents += delegate(TraceEvent data)
                {
                    if ((int)data.ID == 0xFFFE)
                    {
                        return; // The EventSource manifest events show up as unhanded, filter them out.
                    }
                };

                var task = System.Threading.Tasks.Task.Run(() => session.Source.Process());

                Thread.Sleep(1000);

                session.EnableProvider(providerName);

                Thread.Sleep(1000);

                // send events to session
                var logger = LogManager.GetLogger("A");
                try
                {
                    if (!task.IsCanceled)
                    {
                        throw new InvalidProgramException("Best exception in the world");
                    }
                }
                catch (Exception ex)
                {
                    logger.Fatal(ex, "test-fatal");
                }

                resetEvent.WaitOne(20000);
            }

            // assert collected events
            var expectedEvents = new NLogEtwEventData[] {
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Fatal", Level = TraceEventLevel.Critical, LoggerName = "A", Message = "FATAL|A|test-fatal|System.InvalidProgramException: Best exception in the world"
                }
            };

            Assert.Equal(collectedEvents, expectedEvents);
        }
        public void Writing_Message_To_Etw()
        {
            var resetEvent      = new ManualResetEvent(false);
            var collectedEvents = new List <NLogEtwEventData>(5);

            var providerName = etwTarget.ProviderName.Render(LogEventInfo.CreateNullEvent());

            using (var session = new TraceEventSession("SimpleMonitorSession"))
            {
                // Dynamic-Parser does not work with pure EventSource-objects
                session.Source.Registered.All += delegate(TraceEvent data)
                {
                    if (data.Level == TraceEventLevel.Always)
                    {
                        return;   // Not ours
                    }
                    collectedEvents.Add(new NLogEtwEventData(data)
                    {
                        EventId    = 0, // Raw EventSource gives "random" EventId
                        LoggerName = (string)data.PayloadByName("LoggerName"),
                        Message    = (string)data.PayloadByName("Message"),
                    });

                    if (collectedEvents.Count == 5)
                    {
                        resetEvent.Set();
                    }
                };
                session.Source.UnhandledEvents += delegate(TraceEvent data)
                {
                    if ((int)data.ID == 0xFFFE)
                    {
                        return; // The EventSource manifest events show up as unhanded, filter them out.
                    }
                };

                var task = System.Threading.Tasks.Task.Run(() => session.Source.Process());

                Thread.Sleep(1000);

                session.EnableProvider(providerName);

                Thread.Sleep(1000);

                // send events to session
                var logger = LogManager.GetLogger("A");
                logger.Debug("test-debug");
                logger.Info("test-info");
                logger.Warn("test-warn");
                logger.Error("test-error");
                logger.Fatal("test-fatal");

                try
                {
                    var eventSourceGuid = TraceEventProviders.GetEventSourceGuidFromName(providerName);
                    session.DisableProvider(eventSourceGuid);
                }
                catch
                {
                    Thread.Sleep(1000);
                    var eventSourceGuid = TraceEventProviders.GetEventSourceGuidFromName(providerName);
                    session.DisableProvider(eventSourceGuid);
                }

                Thread.Sleep(1000);

                logger.Fatal("don't log this one");
                resetEvent.WaitOne(20000);
            }

            // assert collected events
            var expectedEvents = new NLogEtwEventData[] {
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Debug", Level = TraceEventLevel.Verbose, LoggerName = "A", Message = "DEBUG|A|test-debug"
                },
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Info", Level = TraceEventLevel.Informational, LoggerName = "A", Message = "INFO|A|test-info"
                },
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Warn", Level = TraceEventLevel.Warning, LoggerName = "A", Message = "WARN|A|test-warn"
                },
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Error", Level = TraceEventLevel.Error, LoggerName = "A", Message = "ERROR|A|test-error"
                },
                new NLogEtwEventData {
                    EventId = 0, ProviderName = providerName, TaskName = "Fatal", Level = TraceEventLevel.Critical, LoggerName = "A", Message = "FATAL|A|test-fatal"
                }
            };

            Assert.Equal(collectedEvents, expectedEvents);
        }
Beispiel #4
0
        public void Writing_Message_To_Etw()
        {
            var providerName = "LowLevelDesign-NLogEtwSource";
            var resetEvent   = new ManualResetEvent(false);
            var fpath        = Path.Combine(Path.GetTempPath(), "_etwnlogtest.etl");

            using (var session = new TraceEventSession("SimpleMonitorSession", fpath))
            {
                Thread.Sleep(1000);

                var eventSourceGuid = TraceEventProviders.GetEventSourceGuidFromName(providerName);

                try
                {
                    session.EnableProvider(eventSourceGuid);
                    Thread.Sleep(1000);
                }
                catch
                {
                    Thread.Sleep(1000);
                    eventSourceGuid = TraceEventProviders.GetEventSourceGuidFromName(providerName);
                    session.EnableProvider(eventSourceGuid);
                }

                // send events to session
                var logger = LogManager.GetLogger("A");
                logger.Debug("test-debug");
                logger.Info("test-info");
                logger.Warn("test-warn");
                logger.Error("test-error");
                logger.Fatal("test-fatal");

                try
                {
                    Thread.Sleep(1000);
                    session.DisableProvider(eventSourceGuid);
                }
                catch
                {
                    Thread.Sleep(1000);
                    session.DisableProvider(eventSourceGuid);
                }

                Thread.Sleep(1000);

                logger.Fatal("don't log this one");
            }

            var collectedEvents = new List <NLogEtwEventData>(5);

            using (var source = new ETWTraceEventSource(fpath))
            {
                var parser = new DynamicTraceEventParser(source);
                parser.All += delegate(TraceEvent data)
                {
                    if (data.Level == TraceEventLevel.Always)
                    {
                        return;   // Not ours
                    }
                    collectedEvents.Add(new NLogEtwEventData(data)
                    {
                        LoggerName = (string)data.PayloadByName("LoggerName"),
                        Message    = (string)data.PayloadByName("Message")
                    });

                    if (collectedEvents.Count == 5)
                    {
                        resetEvent.Set();
                    }
                };
                source.Process();
            }
            File.Delete(fpath);

            // assert collected events
            var expectedEvents = new NLogEtwEventData[] {
                new NLogEtwEventData {
                    EventId = 1, ProviderName = providerName, TaskName = "Verbose", LoggerName = "A", Level = TraceEventLevel.Verbose, Message = "DEBUG|A|test-debug"
                },
                new NLogEtwEventData {
                    EventId = 2, ProviderName = providerName, TaskName = "Info", LoggerName = "A", Level = TraceEventLevel.Informational, Message = "INFO|A|test-info"
                },
                new NLogEtwEventData {
                    EventId = 3, ProviderName = providerName, TaskName = "Warn", LoggerName = "A", Level = TraceEventLevel.Warning, Message = "WARN|A|test-warn"
                },
                new NLogEtwEventData {
                    EventId = 4, ProviderName = providerName, TaskName = "Error", LoggerName = "A", Level = TraceEventLevel.Error, Message = "ERROR|A|test-error"
                },
                new NLogEtwEventData {
                    EventId = 5, ProviderName = providerName, TaskName = "Critical", LoggerName = "A", Level = TraceEventLevel.Critical, Message = "FATAL|A|test-fatal"
                }
            };

            resetEvent.WaitOne(20000);
            Assert.Equal(collectedEvents, expectedEvents);
        }