public void SendWithEmptyEventSource() { // create an invalid set of params EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventSourceName = string.Empty; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); CommonUtil.SendTestMessage(sink); Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm there is one new entry in the Application event log"); // string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine + // "Event Log Sink is missing the key -EventSourceName- in the Distributor's configuration file.\r\n\r\nMessage: \r\nTimestamp: 12/31/9999 11:59:59 PM\r\nMessage: My message body\r\nCategory: foo\r\nPriority: 100\r\nEventId: 1\r\nSeverity: Unspecified\r\nTitle:=== Header ===\r\nMachine: machine" + // "\r\nApplication: domain-Microsoft.Practices.EnterpriseLibrary.Logging.dll\r\nAssembly: " + Assembly.GetExecutingAssembly().FullName + // "\r\nThread: " + AppDomain.GetCurrentThreadId().ToString() + "\r\nUser: "******"\" + Environment.UserName + "\r\nExtended Properties: "; string expected = SR.DefaultLogDestinationMessage + Environment.NewLine + Environment.NewLine + "Event Log Sink is missing the key -EventSourceName- in the Distributor's configuration file.\r\n\r\nMessage: \r\n" + CommonUtil.FormattedMessage; string actual = CommonUtil.GetLastEventLogEntry(); Assert.AreEqual(expected, actual, "confirm the one entry in the log is the error"); }
public void SendToCustomEventLog() { string eventLogName = "EntLib Tests"; EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = eventLogName; sinkParams.EventSourceName = "EntLibSource"; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); int count = 0; EventLog entlibLog = null; try { entlibLog = new EventLog(eventLogName); count = entlibLog.Entries.Count; } catch (InvalidOperationException) { /* event log doesn't exist yet */ } CommonUtil.SendTestMessage(sink); entlibLog = new EventLog(eventLogName); Assert.AreEqual(1, entlibLog.Entries.Count - count, "confirm there is only one entry in the event log"); string actual = entlibLog.Entries[entlibLog.Entries.Count - 1].Message; Assert.AreEqual(CommonUtil.FormattedMessage, actual); }
public void SendTextFormattedMessage() { TextFormatter formatter = CommonUtil.CreateTextFormatter("Timestamp: {timestamp}\n\nTitle: {title}\n\nBody: {message}"); EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = CommonUtil.EventLogSourceName; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); sink.Formatter = formatter; LogEntry entry = CommonUtil.GetDefaultLogEntry(); sink.SendMessage(entry); string expected = "Timestamp: " + entry.TimeStampString + "\n\nTitle: " + entry.Title + "\n\nBody: " + entry.Message; using (EventLog customLog = CommonUtil.GetCustomEventLog()) { Assert.AreEqual(expected, customLog.Entries[customLog.Entries.Count - 1].Message); } }
public void LogMessageToEventLogWithCategoryId() { EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = CommonUtil.EventLogSourceName; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); LogEntry logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.EventId = 1234; logEntry.ExtendedProperties["CategoryID"] = "4567"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual("Enterprise Library Unit Tests", entry.Source); Assert.AreEqual(1234, entry.EventID); Assert.AreEqual(4567, entry.CategoryNumber); } }
public void SinkDataConstructor() { EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = CommonUtil.EventLogSourceName; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); Assert.IsNotNull(sink); }
public void LogMessageToEventLog() { EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = CommonUtil.EventLogSourceName; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); CommonUtil.SendTestMessage(sink); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { Assert.AreEqual(1, CommonUtil.EventLogEntryCountCustom()); Assert.AreEqual(CommonUtil.FormattedMessage, customLog.Entries[customLog.Entries.Count - 1].Message); } }
public void SendWithEmptyEventLog() { EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = null; sinkParams.EventSourceName = "EntLib Unit Tests"; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); CommonUtil.SendTestMessage(sink); Assert.AreEqual(1, CommonUtil.EventLogEntryCount(), "confirm there is one entry in the Application event log"); Assert.AreEqual(CommonUtil.FormattedMessage, CommonUtil.GetLastEventLogEntry()); }
public void SendWithCustomEventSource() { string expected = "MyCustomSource"; EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = expected; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); CommonUtil.SendTestMessage(sink); Assert.AreEqual(1, CommonUtil.EventLogEntryCountCustom(), "confirm there is only one entry in the event log"); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { string actual = customLog.Entries[customLog.Entries.Count - 1].Source; Assert.AreEqual(expected, actual); } }
public void TestSolutiaLogToEventLogSucceeds() { var elSink = new EventLogSink() { Name = "TestLogger", Source = "TestLogger", Log = "TestLogger", MinimumEventLevel = EventLevel.Trace, LogEntryConfiguration = new List <LogEntryComponent>() { LogEntryComponent.Date, LogEntryComponent.MachineName, LogEntryComponent.Message, LogEntryComponent.LogLevel } }; new Solutia.Logging.Nlog.Implementation.LogWriter("d") .Configure(new List <ILogSink>() { elSink }) .Log(new LogMessage("Hello from Solutia Event Logger", EventLevel.Debug)); }
public void EventLoggedToNewLogAndSourceGoToRightLog() { EventLogSink sink = new EventLogSink(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()); sink.SendMessage(new LogEntry("foo", "", 1, 2, Severity.Error, "FooTitle", null)); }
public void TestSolutiaLogToDatabaseFileAndEventLogSucceeds() { var entryConfig = new List <LogEntryComponent>() { LogEntryComponent.Date, LogEntryComponent.MachineName, LogEntryComponent.Message, LogEntryComponent.LogLevel, LogEntryComponent.ApplicationName }; var fileSink = new FileLogSink() { Name = "TestLogger", MinimumEventLevel = EventLevel.Trace, FileName = "c:\\solutiatest\\testlog.txt", LogEntryConfiguration = entryConfig }; var elSink = new EventLogSink() { Name = "TestLogger", Source = "TestLogger", Log = "TestLogger", MinimumEventLevel = EventLevel.Trace, LogEntryConfiguration = entryConfig }; var dbSink = new DatabaseLogSink() { ConnectionString = "Data Source=LAPTOP-6EA15661\\SQLEXPRESS;Initial Catalog=Logging;Integrated Security=True", CommandText = "WriteLog", Name = "TestLogger", CommandType = CommandType.StoredProcedure, Parameters = new List <DbLogSinkCommandParameter>() { new DbLogSinkCommandParameter() { Name = "SeverityLevel", LogEntryComponent = LogEntryComponent.LogLevel }, new DbLogSinkCommandParameter() { Name = "Source", LogEntryComponent = LogEntryComponent.MachineName }, new DbLogSinkCommandParameter() { Name = "Message", LogEntryComponent = LogEntryComponent.Message }, new DbLogSinkCommandParameter() { Name = "LogId", LogEntryComponent = LogEntryComponent.LogId }, new DbLogSinkCommandParameter() { Name = "ApplicationName", LogEntryComponent = LogEntryComponent.ApplicationName } } }; ILogWriter writer = new LogWriter("d") .Configure(new List <ILogSink>() { fileSink, elSink, dbSink }) .Log(new LogMessage("Hello from Solutia File Logger", EventLevel.Debug)); writer.Log(new ExceptionLogMessage(new NullReferenceException("This is an exceptional exception."))); writer.Log(new ApplicationLogMessage("ThisApp", "This Message", EventLevel.Debug)); }
public void DefaultConstructor() { EventLogSink sink = new EventLogSink(); Assert.IsNotNull(sink); }
public void LogMessageToEventLogWithCategoryIdSpellings() { EventLogSinkData sinkParams = new EventLogSinkData(); sinkParams.EventLogName = CommonUtil.EventLogNameCustom; sinkParams.EventSourceName = CommonUtil.EventLogSourceName; EventLogSink sink = new EventLogSink(); sink.Initialize(new TestLogSinkConfigurationView(sinkParams)); LogEntry logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.ExtendedProperties["CategoryID"] = "4567"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual(4567, entry.CategoryNumber); } logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.ExtendedProperties["CategoryId"] = "890"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual(890, entry.CategoryNumber); } logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.ExtendedProperties["categoryId"] = "321"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual(321, entry.CategoryNumber); } logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.ExtendedProperties["categoryid"] = "654"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual(654, entry.CategoryNumber); } logEntry = CommonUtil.GetDefaultLogEntry(); logEntry.ExtendedProperties = new Hashtable(); logEntry.ExtendedProperties["cATEGoryid"] = "0"; sink.SendMessage(logEntry); using (EventLog customLog = CommonUtil.GetCustomEventLog()) { EventLogEntry entry = customLog.Entries[customLog.Entries.Count - 1]; Assert.AreEqual(0, entry.CategoryNumber); } }
public EventLogLogger(ILoggerSink sink) { _Sink = sink as EventLogSink; }