public void FormatterListenerWithStoredProcsAndWrongDbInstance() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(WrongConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, new LogEntry("message", "cat1", 0, 0, TraceEventType.Error, "title", null)); }
public void FormatterListenerWriteWithStoredProcsAndDbInstance() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(ConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); listener.Write("FormatterListenerWriteWithStoredProcsAndDbInstance"); string messageContents = GetLastLogMessage(); Assert.AreEqual("FormatterListenerWriteWithStoredProcsAndDbInstance", messageContents, true); }
public void FormatterListenerWithStoredProcsAndDbInstance() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(ConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, new LogEntry("FormatterListenerWithStoredProcsAndDbInstance", "cat1", 0, 0, TraceEventType.Error, "title", null)); string messageContents = GetLastLogMessage(); Assert.AreEqual("FormatterListenerWithStoredProcsAndDbInstance", messageContents); }
public void FormatterListenerAsString() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(ConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, "FormatterListenerAsString"); string messageContents = GetLastLogMessage(); Assert.AreEqual("FormatterListenerAsString", messageContents, true); }
public void LogToDatabaseApplyingFilter() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(ConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); // Apply filter listener.Filter = new EventTypeFilter(SourceLevels.Information); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, new LogEntry("LogToDatabaseApplyingFilter", "cat1", 0, 0, TraceEventType.Verbose, "title", null)); string result = GetLastLogMessage(); Assert.AreNotEqual(0, result.Length); Assert.AreEqual("LogToDatabaseApplyingFilter", result); }
/// <summary> /// Sets up the TraceListener used to write log messages to the database. /// </summary> /// <param name="traceSource">A TraceSource that raises log messages.</param> /// <param name="databaseListenerName">The name of the TraceListener the TraceSource uses /// to write to the database.</param> /// <param name="configSettingsSectionName">The name of the section in the config file /// that contains connection information for the database.</param> /// <remarks>Only need to call this once, for one of the trace sources, as the /// TraceListener is shared between all the trace sources.</remarks> public static void SetupDatabaseLogging(TraceSource traceSource, string databaseListenerName, SystemCoreSettings systemSettings) { lock (_lockSetupDatabaseLogging) { DatabaseManager2 databaseManager = BBDatabase.GetDatabaseManager(systemSettings); DatabaseTraceListener databaseLogger = (DatabaseTraceListener)(traceSource.Listeners[databaseListenerName]); databaseLogger.DatabaseManager = databaseManager; Assembly callingAssembly = Assembly.GetCallingAssembly(); AssemblyTitleAttribute assemblyTitle = ReflectionHelper.GetAssemblyAttribute <AssemblyTitleAttribute>(callingAssembly); databaseLogger.ApplicationName = assemblyTitle.Title; databaseLogger.TraceOutputOptions |= TraceOptions.ProcessId; databaseLogger.TraceOutputOptions |= TraceOptions.ThreadId; // Skip the following helper methods when determining the name of the method that // raised the log message. databaseLogger.MethodNameMethodsToIgnore.Add(LogMethodsToIgnore.LogException); databaseLogger.MethodNameMethodsToIgnore.Add( LogMethodsToIgnore.LogStoredProcResults); } }
public void ShouldFilterLogToDatabase() { DatabaseTraceListener listener = new DatabaseTraceListener(new SqlDatabase(ConnectionString)); listener.Attributes.Add("ApplicationName", "LoggingUnitTests"); // Disable filtering listener.Filter = new EventTypeFilter(SourceLevels.Off); string message = DateTime.UtcNow.ToLongTimeString(); listener.TraceData(new TraceEventCache(), "source", TraceEventType.Error, 0, new LogEntry(message, "cat1", 0, 0, TraceEventType.Error, "title", null)); string result = GetLastLogMessage(); Assert.AreNotEqual(message, result, true); }