Esempio n. 1
0
        public void LogWithMultipleCategories()
        {
            FormattedDatabaseTraceListener listener = new FormattedDatabaseTraceListener(new SqlDatabase(connectionString), "WriteLog", "AddCategory", new TextFormatter("TEST{newline}TEST"));

            // need to go through the source to get a TraceEventCache
            TraceSource source = new TraceSource("notfromconfig", SourceLevels.All);

            source.Listeners.Add(listener);
            LogEntry logEntry = new LogEntry();

            logEntry.Message = "message";
            logEntry.Categories.Add("FormattedCategory");
            logEntry.Categories.Add("DictionaryCategory");
            logEntry.EventId  = 123;
            logEntry.Priority = 11;
            logEntry.Severity = TraceEventType.Error;
            logEntry.Title    = "title";
            source.TraceData(TraceEventType.Error, 0, logEntry);

            DatabaseProviderFactory factory = new DatabaseProviderFactory(ConfigurationSourceFactory.Create());

            Data.Database db            = factory.CreateDefault();
            DbCommand     command       = db.GetSqlStringCommand("SELECT Count(*) FROM Category");
            int           categoryCount = Convert.ToInt32(db.ExecuteScalar(command));

            Assert.AreEqual(2, categoryCount);
        }
        int GetNumberOfLogMessage(string databaseName)
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();

            Data.Database db          = ((databaseName == null) || (databaseName.Length == 0)) ? factory.CreateDefault() : factory.Create(databaseName);
            DbCommand     command     = db.GetSqlStringCommand("SELECT COUNT(*) FROM Log");
            int           numMessages = Convert.ToInt32(db.ExecuteScalar(command));

            return(numMessages);
        }
        string GetLastLogMessage(string databaseName)
        {
            DatabaseProviderFactory factory = new DatabaseProviderFactory();

            Data.Database db              = ((databaseName == null) || (databaseName.Length == 0)) ? factory.CreateDefault() : factory.Create(databaseName);
            DbCommand     command         = db.GetSqlStringCommand("SELECT TOP 1 FormattedMessage FROM Log ORDER BY TimeStamp DESC");
            string        messageContents = Convert.ToString(db.ExecuteScalar(command));

            return(messageContents);
        }