public void IfCreateEventSourceFailsUseDefaultPropertyBag()
        {
            config.ContainsReturnValue = true;
            config.GetBykeyReturnValue = "MyLog";
            Isolate.Fake.StaticMethods <EventLog>(Members.CallOriginal);
            Isolate.WhenCalled(() => EventLog.SourceExists("MyLog")).WithExactArguments().WillReturn(false);
            Isolate.WhenCalled(() => EventLog.CreateEventSource("MyLog", "Application")).WithExactArguments().DoInstead(
                delegate
            {
                throw new InvalidOperationException("TestException");
            });

            bool originalMessageCalled = false;

            Isolate.WhenCalled(() => EventLog.WriteEntry("Office SharePoint Server", "Category: Category1\nMessage", EventLogEntryType.SuccessAudit, 0))
            .WithExactArguments().DoInstead((context) => originalMessageCalled = true);

            string logError = string.Empty;

            Isolate.WhenCalled(() => EventLog.WriteEntry("Office SharePoint Server", "Could not create eventsource 'MyLog'.", EventLogEntryType.Error, 0))
            .DoInstead((context) => logError = context.Parameters[1] as string);

            EventLogLogger target = new TestableEventLogger();

            target.Log("Message", 99, EventLogEntryType.SuccessAudit, "Category1");

            Assert.IsTrue(originalMessageCalled);
            Assert.IsTrue(logError.Contains("'MyLog'"));
            Assert.IsTrue(logError.Contains("InvalidOperationException"));
            Assert.IsTrue(logError.Contains("TestException"));
        }
        public void CanLogErrorToEventLog()
        {
            bool eventLogWritten = false;

            config.ContainsReturnValue = true;
            config.GetBykeyReturnValue = "MyLog";
            Isolate.Fake.StaticMethods <EventLog>(Members.CallOriginal);
            Isolate.WhenCalled(() => EventLog.SourceExists("MyLog")).WithExactArguments().WillReturn(true);
            Isolate.WhenCalled(() => EventLog.WriteEntry("MyLog", "Category: Category1\nMessage", EventLogEntryType.Error, 99))
            .WithExactArguments().DoInstead((context) => eventLogWritten = true);


            EventLogLogger target = new TestableEventLogger();

            target.Log("Message", 99, EventLogEntryType.Error, "Category1");

            Assert.IsTrue(eventLogWritten);
        }
        public void UsesDefaultIfNoEventSourceNameConfigured()
        {
            config.ContainsReturnValue = false;

            bool eventLogWritten = false;

            Isolate.Fake.StaticMethods <EventLog>(Members.CallOriginal);
            Isolate.WhenCalled(() => EventLog.SourceExists("Office SharePoint Server")).WithExactArguments().WillReturn(true);
            Isolate.WhenCalled(() => EventLog.WriteEntry("Office SharePoint Server", "Category: Category1\nMessage", EventLogEntryType.Warning, 0))
            .WithExactArguments().DoInstead((context) => eventLogWritten = true);


            EventLogLogger target = new TestableEventLogger();

            target.Log("Message", 99, EventLogEntryType.Warning, "Category1");

            Assert.IsTrue(eventLogWritten);
        }
        public void CanCreateEventSource()
        {
            bool eventSourceCreated = false;

            config.ContainsReturnValue = true;
            config.GetBykeyReturnValue = "MyLog";
            Isolate.Fake.StaticMethods <EventLog>(Members.CallOriginal);
            Isolate.WhenCalled(() => EventLog.SourceExists("MyLog")).WithExactArguments().WillReturn(false);
            Isolate.WhenCalled(() => EventLog.CreateEventSource("MyLog", "Application")).WithExactArguments().DoInstead((context) => eventSourceCreated = true);

            Isolate.WhenCalled(() => EventLog.WriteEntry("MyLog", "Category: Category1\nMessage", EventLogEntryType.SuccessAudit, 99))
            .WithExactArguments().IgnoreCall();

            EventLogLogger target = new TestableEventLogger();

            target.Log("Message", 99, EventLogEntryType.SuccessAudit, "Category1");

            Assert.IsTrue(eventSourceCreated);
        }
        public void IfEventSourceIsDefaultEventSourceLogWarning()
        {
            config.ContainsReturnValue = true;
            config.GetBykeyReturnValue = "Office SharePoint Server";
            Isolate.Fake.StaticMethods <EventLog>(Members.CallOriginal);

            bool originalMessageCalled = false;

            Isolate.WhenCalled(() => EventLog.WriteEntry("Office SharePoint Server", "Category: Category1\nMessage", EventLogEntryType.SuccessAudit, 0))
            .WithExactArguments().DoInstead((context) => originalMessageCalled = true);

            bool loggedWarning = false;

            Isolate.WhenCalled(() => EventLog.WriteEntry("Office SharePoint Server", "The current application should not be logging using the 'Office SharePoint Server' event source. Please configure a new event source.", EventLogEntryType.Warning, 0))
            .WithExactArguments().DoInstead((context) => loggedWarning = true);

            EventLogLogger target = new TestableEventLogger();

            target.Log("Message", 99, EventLogEntryType.SuccessAudit, "Category1");

            Assert.IsTrue(originalMessageCalled);
            Assert.IsTrue(loggedWarning);
        }