Esempio n. 1
0
        private static IEnumerable <string> WriteEventLogEntryForOverflow(EventLogTargetOverflowAction overflowAction, string message)
        {
            string sourceName = Guid.NewGuid().ToString();
            var    target     = CreateEventLogTarget(null, sourceName);

            target.OnOverflow = overflowAction;
            target.Layout     = new SimpleLayout("${message}");
            LoggingConfiguration config = new LoggingConfiguration();
            LoggingRule          rule   = new LoggingRule("*", LogLevel.Info, target);

            config.LoggingRules.Add(rule);
            LogManager.Configuration = config;

            var logger = LogManager.GetLogger("WriteEventLogEntryOverflow");
            var el     = new EventLog(target.Log);

            var loggedNotBefore = DateTime.Now.AddMinutes(-1);

            logger.Log(LogLevel.Info, message);

            var entries = GetEventRecords(el.Log).TakeWhile(e => e.TimeCreated > loggedNotBefore && e.ProviderName == sourceName).ToList();

            var chunks = from entry in entries
                         from prop in entry.Properties
                         let msg = Convert.ToString(prop.Value)
                                   where msg.StartsWith("a")
                                   orderby msg descending
                                   select msg;

            return(chunks);
        }
Esempio n. 2
0
        private static TEventLogTarget CreateEventLogTarget <TEventLogTarget>(
            string sourceName, EventLogTargetOverflowAction overflowAction, int maxMessageLength, Layout entryType = null)
            where TEventLogTarget : EventLogTarget, new()
        {
            var target = new TEventLogTarget
            {
                Log              = "application",                  // The Log to write to is intentionally lower case!!
                Source           = sourceName,                     // set the source explicitly to prevent random AppDomain name being used as the source name
                Layout           = new SimpleLayout("${message}"), //Be able to check message length and content, the Layout is intentionally only ${message}.
                OnOverflow       = overflowAction,
                MaxMessageLength = maxMessageLength,
            };

            if (entryType != null)
            {
                target.EntryType = entryType;
            }

            return(target);
        }
Esempio n. 3
0
        private static TEventLogTarget CreateEventLogTarget <TEventLogTarget>(Layout entryType, string sourceName, EventLogTargetOverflowAction overflowAction, int maxMessageLength)
            where TEventLogTarget : EventLogTarget, new()
        {
            var target = new TEventLogTarget();

            //The Log to write to is intentionally lower case!!
            target.Log = "application";
            // set the source explicitly to prevent random AppDomain name being used as the source name
            target.Source = sourceName;
            //Be able to check message length and content, the Layout is intentionally only ${message}.
            target.Layout = new SimpleLayout("${message}");
            if (entryType != null)
            {
                //set only when not default
                target.EntryType = entryType;
            }

            target.OnOverflow       = overflowAction;
            target.MaxMessageLength = maxMessageLength;

            return(target);
        }
Esempio n. 4
0
        private static IEnumerable <EventRecord> WriteWithMock(LogLevel logLevel, EventLogEntryType expectedEventLogEntryType,
                                                               string logMessage, Layout entryType = null, EventLogTargetOverflowAction overflowAction = EventLogTargetOverflowAction.Truncate, int maxMessageLength = 16384)
        {
            var target = CreateEventLogTarget <EventLogTargetMock>(entryType, "NLog.UnitTests" + Guid.NewGuid().ToString("N"), overflowAction, maxMessageLength);

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            var logger = LogManager.GetLogger("WriteEventLogEntry");

            logger.Log(logLevel, logMessage);

            var entries = target.CapturedEvents;

            var expectedSource = target.GetFixedSource();

            var filteredEntries = entries.Where(entry =>
                                                entry.ProviderName == expectedSource &&
                                                HasEntryType(entry, expectedEventLogEntryType)
                                                );

            if (overflowAction == EventLogTargetOverflowAction.Discard && logMessage.Length > maxMessageLength)
            {
                Assert.False(filteredEntries.Any(),
                             $"No message is expected. But {filteredEntries.Count()} message(s) found entry of type '{expectedEventLogEntryType}' from source '{expectedSource}'.");
            }
            else
            {
                Assert.True(filteredEntries.Any(),
                            $"Failed to find entry of type '{expectedEventLogEntryType}' from source '{expectedSource}'");
            }

            return(filteredEntries);
        }
Esempio n. 5
0
        private static EventLogTarget CreateEventLogTarget(Layout entryType, string sourceName, EventLogTargetOverflowAction overflowAction, int maxMessageLength)
        {
            var target = new EventLogTarget();
            //The Log to write to is intentionally lower case!!
            target.Log = "application";
            // set the source explicitly to prevent random AppDomain name being used as the source name
            target.Source = sourceName;
            //Be able to check message length and content, the Layout is intentionally only ${message}.
            target.Layout = new SimpleLayout("${message}");
            if (entryType != null)
            {
                //set only when not default
                target.EntryType = entryType;
            }

            target.OnOverflow = overflowAction;
            target.MaxMessageLength = maxMessageLength;

            return target;
        }
Esempio n. 6
0
        private static IEnumerable<EventRecord> Write(LogLevel logLevel, EventLogEntryType expectedEventLogEntryType, string logMessage, Layout entryType = null, EventLogTargetOverflowAction overflowAction = EventLogTargetOverflowAction.Truncate, int maxMessageLength = 16384)
        {
            var target = CreateEventLogTarget(entryType, "NLog.UnitTests" + Guid.NewGuid().ToString("N"), overflowAction, maxMessageLength);
            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            var logger = LogManager.GetLogger("WriteEventLogEntry");
            logger.Log(logLevel, logMessage);

            var eventLog = new EventLog(target.Log);

            var entries = GetEventRecords(eventLog.Log).ToList();

            var expectedSource = target.GetFixedSource();

            var filteredEntries = entries.Where(entry =>
                                            entry.ProviderName == expectedSource &&
                                            HasEntryType(entry, expectedEventLogEntryType)
                                            );
            if (overflowAction == EventLogTargetOverflowAction.Discard && logMessage.Length > maxMessageLength)
            {
                Assert.False(filteredEntries.Any(), string.Format("No message is expected. But {0} message(s) found entry of type '{1}' from source '{2}'.", filteredEntries.Count(), expectedEventLogEntryType, expectedSource));
            }
            else
            {
                Assert.True(filteredEntries.Any(), string.Format("Failed to find entry of type '{0}' from source '{1}'", expectedEventLogEntryType, expectedSource));
            }

            return filteredEntries;
        }
Esempio n. 7
0
        private static IEnumerable <EventRecord> Write(LogLevel logLevel, EventLogEntryType expectedEventLogEntryType, string logMessage, Layout entryType = null, EventLogTargetOverflowAction overflowAction = EventLogTargetOverflowAction.Truncate)
        {
            var target = CreateEventLogTarget(entryType, "NLog.UnitTests" + Guid.NewGuid().ToString("N"), overflowAction);

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            var logger = LogManager.GetLogger("WriteEventLogEntry");

            logger.Log(logLevel, logMessage);

            var eventLog = new EventLog(target.Log);

            var entries = GetEventRecords(eventLog.Log).ToList();

            var expectedSource = target.GetFixedSource();

            var filteredEntries = entries.Where(entry =>
                                                entry.ProviderName == expectedSource &&
                                                HasEntryType(entry, expectedEventLogEntryType)
                                                );

            if (overflowAction == EventLogTargetOverflowAction.Discard && logMessage.Length > MaxMessageSize)
            {
                Assert.False(filteredEntries.Any(), string.Format("No message is expected. But {0} message(s) found entry of type '{1}' from source '{2}'.", filteredEntries.Count(), expectedEventLogEntryType, expectedSource));
            }
            else
            {
                Assert.True(filteredEntries.Any(), string.Format("Failed to find entry of type '{0}' from source '{1}'", expectedEventLogEntryType, expectedSource));
            }

            return(filteredEntries);
        }
Esempio n. 8
0
        private static EventLogTarget InitializeEventLogTarget(EventLogTarget target, string sourceName, EventLogTargetOverflowAction overflowAction, int maxMessageLength, Layout entryType)
        {
            target.Name             = "eventlog";
            target.Log              = "application";                  // The Log to write to is intentionally lower case!!
            target.Source           = sourceName;                     // set the source explicitly to prevent random AppDomain name being used as the source name
            target.Layout           = new SimpleLayout("${message}"); //Be able to check message length and content, the Layout is intentionally only ${message}.
            target.OnOverflow       = overflowAction;
            target.MaxMessageLength = maxMessageLength;

            if (entryType != null)
            {
                target.EntryType = entryType;
            }

            return(target);
        }
Esempio n. 9
0
 private static EventLogTarget CreateEventLogTarget(string sourceName, EventLogTargetOverflowAction overflowAction, int maxMessageLength, Layout entryType = null)
 {
     return(InitializeEventLogTarget(new EventLogTarget(), sourceName, overflowAction, maxMessageLength, entryType));
 }
Esempio n. 10
0
        private static IEnumerable <EventLogMock.EventRecord> WriteWithMock(LogLevel logLevel, EventLogEntryType expectedEventLogEntryType,
                                                                            string logMessage, Layout entryType = null, EventLogTargetOverflowAction overflowAction = EventLogTargetOverflowAction.Truncate, int maxMessageLength = MaxMessageLength)
        {
            var sourceName = "NLog.UnitTests" + Guid.NewGuid().ToString("N");

            var eventLogMock = new EventLogMock(
                deleteEventSourceFunction: (source, machineName) => { },
                sourceExistsFunction: (source, machineName) => false,
                logNameFromSourceNameFunction: (source, machineName) => string.Empty,
                createEventSourceFunction: (sourceData) => { });
            var target = new EventLogTarget(eventLogMock, null);

            InitializeEventLogTarget(target, sourceName, overflowAction, maxMessageLength, entryType);

            SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

            var logger = LogManager.GetLogger("WriteEventLogEntry");

            logger.Log(logLevel, logMessage);

            var entries = eventLogMock.WrittenEntries;

            var expectedSource = target.GetFixedSource();

            var filteredEntries = entries.Where(entry =>
                                                entry.Source == expectedSource &&
                                                entry.EntryType == expectedEventLogEntryType
                                                );

            if (overflowAction == EventLogTargetOverflowAction.Discard && logMessage.Length > maxMessageLength)
            {
                Assert.False(filteredEntries.Any(),
                             $"No message is expected. But {filteredEntries.Count()} message(s) found entry of type '{expectedEventLogEntryType}' from source '{expectedSource}'.");
            }
            else
            {
                Assert.True(filteredEntries.Any(),
                            $"Failed to find entry of type '{expectedEventLogEntryType}' from source '{expectedSource}'");
            }

            return(filteredEntries);
        }