How to prevent tracing content from components in a TraceListener?

231 Views Asked by At

I am using SMO (added to my project using the NuGet package named Microsoft.SqlServer.SqlManagementObjects - version 160.200421.0) and noticed that all of a sudden, my logs are filled with tracing from that library which is being way too verbose!

For example, this code:

using System;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.SqlServer.Management.Smo;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strServerName = @"private";
            string strScriptDatabaseName = "private";
            string strUid = "private";
            string strPwd = "private";

            Server _objServer = new Server();
            _objServer.ConnectionContext.ServerInstance = strServerName;
            _objServer.ConnectionContext.LoginSecure = false;
            _objServer.ConnectionContext.Login = strUid;
            _objServer.ConnectionContext.Password = strPwd;
            Trace.WriteLine("before Connect", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            _objServer.ConnectionContext.Connect();
            Trace.WriteLine("after Connect", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

            Trace.WriteLine($"before Databases", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
            Database _objDatabase = _objServer.Databases[strScriptDatabaseName];
            Trace.WriteLine($"after Databases", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
        }
    }
}

Generates these lines in my Ouput Window:

  1. 2020-05-26 10:33:26: before Databases 'WindowsFormsApp1.exe' (CLR v4.0.30319: WindowsFormsApp1.exe): Loaded
  2. WindowsFormsApp1.exe Information: 0 : 2020-05-26T10:33:27.5115418-04:00 - get data for urn: Server/Database[@Name='master']
  3. WindowsFormsApp1.exe Information: 0 : 2020-05-26T10:33:27.6665197-04:00 - get data for urn: Server[@Name='XXXXXXXXXXXX']/Database[@Name='Maintenace']
  4. WindowsFormsApp1.exe Information: 0 : 2020-05-26T10:33:27.7045275-04:00 - Failed to Initialize urn Server[@Name='XXXXXXXXX']/Database[@Name='Maintenace']
  5. 2020-05-26 10:33:31: after Databases

And since I do log using a TraceListener, the 3 lines between my 2 Trace.WriteLine are also getting into my logs and I don't want them. Other operations from the same library are generating 100s of rows making my logs unreadable and useless.

How to prevent the tracing from catching these rows? I don't want these rows to reach my logs.

0

There are 0 best solutions below