Esempio n. 1
0
        public void TestRequired(string sourceId)
        {
            var    config          = TestUtility.GetConfig("Sources", sourceId);
            string logName         = config["LogName"];
            string requiredSetting = config["Required"];
            bool   required        = string.IsNullOrWhiteSpace(requiredSetting) ?
                                     true : bool.Parse(requiredSetting);
            MemoryLogger logger = new MemoryLogger(null);

            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(config, logger, null, _bookmarkManager)))
            {
                source.Start();

                System.Threading.Thread.Sleep(1000);

                if (required)
                {
                    Assert.NotEmpty(logger.Entries.Where(e => e.Contains("error")));
                }
                else
                {
                    Assert.Empty(logger.Entries.Where(e => e.Contains("error")));
                }
            }
        }
Esempio n. 2
0
        public void TestSavingOutdatedBookmark()
        {
            string logName   = "Application";
            string logSource = nameof(TestSavingOutdatedBookmark);

            DeleteExistingBookmarkFile(logSource);

            if (!EventLog.SourceExists(logSource))
            {
                EventLog.CreateEventSource(logSource, logName);
            }

            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(null, null, null, _bookmarkManager)))
            {
                source.Id = logSource;
                source.InitialPosition = InitialPositionEnum.Bookmark;
                source.Start();
                System.Threading.Thread.Sleep(1000);
                EventLog.WriteEntry(logSource, "TestSavingOutdatedBookmark test message");
                System.Threading.Thread.Sleep(1000);
                var lastSavedBookmark = source.LastSavedBookmark;

                // execute a save bookmark operation at the '0' position
                source.SaveBookmarkInternal(0, false);

                // assert that 'SaveBookmarkInternal' does not throw exception,
                // and the saved bookmark remains the latest position.
                Assert.Equal(lastSavedBookmark, source.LastSavedBookmark);
            }
        }
 // General Exception into constructor
 public EventLog(Exception ex)
 {
     Id = Guid.NewGuid();
     Title = ex.Message;
     if (ex.InnerException != null) Details = ex.InnerException.Message;
     DateCreated = DateTime.UtcNow;
     Source = EventLogSource.Internal;
     Type = EventLogType.Error;
 }
 // DNN EventLog object
 // TODO: Needs work!
 public EventLog(DotNetNuke.Services.Log.EventLog.LogInfo e)
 {
     Id          = Guid.NewGuid();
     Title       = e.LogPortalName;
     Details     = e.LogProperties.ToString();
     DateCreated = e.LogCreateDate;
     Source      = EventLogSource.Db;
     Type        = EventLogType.Error;
 }
 // General Exception into constructor
 public EventLog(Exception ex)
 {
     Id    = Guid.NewGuid();
     Title = ex.Message;
     if (ex.InnerException != null)
     {
         Details = ex.InnerException.Message;
     }
     DateCreated = DateTime.UtcNow;
     Source      = EventLogSource.Internal;
     Type        = EventLogType.Error;
 }
        // log4net Logging Event object into constructor
        public EventLog(LoggingEvent e)
        {
            Id          = Guid.NewGuid();
            Title       = e.MessageObject is Exception ? ((Exception)e.MessageObject).Message : e.MessageObject.ToString();
            Details     = e.RenderedMessage;
            DateCreated = e.TimeStamp;
            Source      = EventLogSource.Internal;

            Type =
                e.Level == Level.Info ? EventLogType.Info :
                e.Level == Level.Debug ? EventLogType.Debug :
                e.Level == Level.Warn ? EventLogType.Warn :
                e.Level == Level.Error ? EventLogType.Error :
                e.Level == Level.Fatal ? EventLogType.Fatal :
                EventLogType.Log;
        }
Esempio n. 7
0
        private void GenerateAndCaptureEvents(ListEventSink records, IConfiguration config)
        {
            using (EventLogSource source = new EventLogSource(LogName, null, new PluginContext(config, null, null, _bookmarkManager)))
            {
                source.Subscribe(records);
                source.Start();

                string msg     = $"Message generated by EventLogTest {DateTime.Now}";
                int    eventId = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                EventLog.WriteEntry(LogSource, msg, EventLogEntryType.Information, eventId);

                System.Threading.Thread.Sleep(1000);

                source.Stop();
            }
        }
        // log4net Logging Event object into constructor
        public EventLog(LoggingEvent e)
        {
            Id = Guid.NewGuid();
            Title = e.MessageObject is Exception ? ((Exception)e.MessageObject).Message : e.MessageObject.ToString();
            Details = e.RenderedMessage;
            DateCreated = e.TimeStamp;
            Source = EventLogSource.Internal;

            Type =
                e.Level == Level.Info ? EventLogType.Info :
                e.Level == Level.Debug ? EventLogType.Debug :
                e.Level == Level.Warn ? EventLogType.Warn :
                e.Level == Level.Error ? EventLogType.Error :
                e.Level == Level.Fatal ? EventLogType.Fatal :
                EventLogType.Log;
        }
Esempio n. 9
0
        public void TestInitialPositionTimeStamp()
        {
            string        logName          = "Application";
            string        logSource        = "EventLogTest";
            ListEventSink records          = new ListEventSink();
            DateTime      initialTimestamp = DateTime.Now.AddDays(-1);
            DateTime      nowUtc           = DateTime.UtcNow;
            string        sourceId         = "TestInitialPositionTimeStamp";

            DeleteExistingBookmarkFile(sourceId);

            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(null, null, null, _bookmarkManager)))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition          = InitialPositionEnum.Timestamp;
                source.InitialPositionTimestamp = initialTimestamp;
                source.Start();

                do
                {
                    EventLog.WriteEntry(logSource, "A fresh message", EventLogEntryType.Information, 0);
                    System.Threading.Thread.Sleep(1000);
                }while (source.LastEventLatency > new TimeSpan(0, 0, 1));

                source.Stop();
                Assert.True(records.Count > 0, "There is an event after the timestamp.");
                Assert.True(records[0].Timestamp >= initialTimestamp.ToUniversalTime() && records[0].Timestamp < nowUtc, "There is an earlier event after the initial timestamp.");
                DateTime dateTime1 = records[records.Count - 1].Timestamp;

                //Write some new logs after the source stop
                DateTime dateTime2 = DateTime.Now;
                string   msg       = $"Message generated by EventLogTest {dateTime2}";
                int      eventId   = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);
                System.Threading.Thread.Sleep(1000);

                records.Clear();
                source.Start();
                System.Threading.Thread.Sleep(2000);
                //Should get the record when the source is stopped
                Assert.True(records.Count > 0, "Should get the new record.");
                Assert.True(records[0].Timestamp >= dateTime1, "Should pick up new records.");
            }
        }
Esempio n. 10
0
        public void TestInitialPositionBookMark()
        {
            string        logName   = "Application";
            string        logSource = "Test";
            ListEventSink records   = new ListEventSink();
            string        sourceId  = "TestInitialPositionBookMark";

            DeleteExistingBookmarkFile(sourceId);

            //This should generate a water mark file
            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(null, null, null)))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition = InitialPositionEnum.Bookmark;
                source.Start();

                var    nowUtc  = DateTime.UtcNow;
                string msg     = $"Message generated by EventLogTest {nowUtc}";
                int    eventId = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                if (!EventLog.SourceExists(logSource))
                {
                    EventLog.CreateEventSource(logSource, logName);
                }
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);

                System.Threading.Thread.Sleep(1000);
                source.Stop();

                //Write some new logs after the source stop
                DateTime dateTime2Utc = DateTime.UtcNow;
                msg = $"Message generated by EventLogTest {dateTime2Utc}";
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);
                System.Threading.Thread.Sleep(1000);


                records.Clear();
                source.Start();
                System.Threading.Thread.Sleep(1000);
                //Should get the record when the source is stopped
                var foundRecord = records.FirstOrDefault(r => msg.Equals(((EventRecordEnvelope)r).Data.Description));
                Assert.NotNull(foundRecord);
                Assert.True(foundRecord.Timestamp >= dateTime2Utc);
            }
        }
Esempio n. 11
0
        public void TestInitialPositionBOS()
        {
            string        logName   = "Application";
            string        logSource = "Test";
            ListEventSink records   = new ListEventSink();
            DateTime      nowUtc    = DateTime.UtcNow;
            string        sourceId  = "TestInitialPositionTimeStamp";

            DeleteExistingBookmarkFile(sourceId);

            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(null, null, null)))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition = InitialPositionEnum.BOS;
                source.Start();

                do
                {
                    EventLog.WriteEntry(logSource, "A fresh message", EventLogEntryType.Information, 0);
                    System.Threading.Thread.Sleep(1000);
                }while (source.LastEventLatency > new TimeSpan(0, 0, 1));
                System.Threading.Thread.Sleep(1000);

                source.Stop();
                Assert.True(records.Count > 0);
                Assert.True(records[0].Timestamp < nowUtc);

                //Write some new logs after the source stop
                DateTime dateTime2Utc = DateTime.UtcNow;
                string   msg          = $"Message generated by EventLogTest {dateTime2Utc}";
                int      eventId      = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);
                System.Threading.Thread.Sleep(1000);

                records.Clear();
                source.Start();
                System.Threading.Thread.Sleep(1000);
                //Should get the record when the source is stopped
                Assert.True(records.Count > 0);
                Assert.True(records[0].Timestamp >= dateTime2Utc);
            }
        }
        public void TestInitialPositionBookMark()
        {
            string        logName   = "Application";
            string        logSource = "Test";
            ListEventSink records   = new ListEventSink();
            string        sourceId  = "TestInitialPositionBookMark";

            DeleteExistingBookmarkFile(sourceId);

            //This should generate a water mark file
            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(null, null, null)))
            {
                source.Subscribe(records);
                source.Id = sourceId;
                source.InitialPosition = InitialPositionEnum.Bookmark;
                source.Start();

                string msg     = $"Message generated by EvengLogTest {DateTime.Now}";
                int    eventId = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                if (!EventLog.SourceExists(logSource))
                {
                    EventLog.CreateEventSource(logSource, logName);
                }
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);

                System.Threading.Thread.Sleep(1000);
                source.Stop();

                //Write some new logs afte the source stop
                DateTime dateTime2 = DateTime.Now;
                msg = $"Message generated by EvengLogTest {dateTime2}";
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);
                System.Threading.Thread.Sleep(1000);


                records.Clear();
                source.Start();
                System.Threading.Thread.Sleep(1000);
                //Should get the record when the souce is stopped
                Assert.True(records.Count > 0);
                Assert.True(records[0].Timestamp >= dateTime2);
            }
        }
Esempio n. 13
0
        private static void GenerateAndCaptureEvents(ListEventSink records, IConfiguration config)
        {
            string logName   = "Application";
            string logSource = "Test";

            using (EventLogSource source = new EventLogSource(logName, null, new PluginContext(config, null, null)))
            {
                source.Subscribe(records);
                source.Start();

                string msg     = $"Message generated by EvengLogTest {DateTime.Now}";
                int    eventId = (int)(DateTime.Now.Ticks % ushort.MaxValue);
                if (!EventLog.SourceExists(logSource))
                {
                    EventLog.CreateEventSource(logSource, logName);
                }
                EventLog.WriteEntry(logSource, msg, EventLogEntryType.Information, eventId);

                System.Threading.Thread.Sleep(1000);

                source.Stop();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EventLogLogger"/> class.
 /// </summary>
 /// <param name="log">The log.</param>
 /// <param name="source">The source.</param>
 public EventLogLogger(EventLogSource log, string source)
     : this(log.ToString(), source)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EventLogLogger"/> class.
 /// </summary>
 /// <param name="log">The log.</param>
 public EventLogLogger(EventLogSource log)
     : this(log, DefaultSource)
 {
 }
 // DNN EventLog object
 // TODO: Needs work!
 public EventLog(DotNetNuke.Services.Log.EventLog.LogInfo e)
 {
     Id = Guid.NewGuid();
     Title = e.LogPortalName;
     Details = e.LogProperties.ToString();
     DateCreated = e.LogCreateDate;
     Source = EventLogSource.Db;
     Type = EventLogType.Error;
 }
Esempio n. 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventLogLogger"/> class.
 /// </summary>
 /// <param name="log">The log.</param>
 public EventLogLogger(EventLogSource log)
     : this(log, DefaultSource)
 {
 }
Esempio n. 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="EventLogLogger"/> class.
 /// </summary>
 /// <param name="log">The log.</param>
 /// <param name="source">The source.</param>
 public EventLogLogger(EventLogSource log, string source)
     : this(log.ToString(), source)
 {
 }
Esempio n. 19
0
        protected override void Execute(CodeActivityContext context)
        {
            console = ActivityConsole.GetDefaultOrNew(context);

            string machineName = MachineName.Get(context);

            machineName = (string.IsNullOrEmpty(machineName) ? "." : machineName);

            bool throwOnError         = ThrowOnError.Get(context);
            bool treatExistsAsSuccess = TreatExistAsSuccess.Get(context);

            string errorMessage = "An unknown error has occurred.";
            bool   created      = CreateLogSource(machineName, EventLogName.Get(context), EventLogSource.Get(context), out errorMessage, treatExistsAsSuccess);

            if (created)
            {
                console.WriteLine(string.Format("Successfully created or verified event log and source : {0} - {1}", EventLogName.Get(context), EventLogSource.Get(context)));
            }
            else
            {
                if (throwOnError)
                {
                    throw new ArgumentException(errorMessage);
                }
                else
                {
                    console.WriteLine(string.Format("Error : {0}", errorMessage));
                }
            }
        }