Exemple #1
0
        public static TraceServer BeginTrace(SqlConnectionInfo sci)
        {
            TraceServer traceServerReader = new TraceServer();

            traceServerReader.InitializeAsReader(sci, GetTraceTemplateName());
            return(traceServerReader);
        }
Exemple #2
0
        private static void Main()
        {
            var connectionInfo = new SqlConnectionInfo("(localdb)\\MSSQLLocalDb")
            {
                UseIntegratedSecurity = true
            };

            var trace = new TraceServer();

            trace.InitializeAsReader(connectionInfo, "trace.trc");

            while (trace.Read())
            {
                if (trace["TextData"] == null)
                {
                    continue;
                }
                if (RecordDoesNotContainNotSupportedEvent(trace["TextData"].ToString()) == false)
                {
                    continue;
                }

                Console.WriteLine("Event : " + trace["EventClass"]);
                Console.WriteLine("SPID  : " + trace["SPID"]);
                Console.WriteLine("Timestamp: " + DateTime.Now);
                Console.WriteLine("Text  : " + trace["TextData"]);
                AnalyzeQuery(trace["TextData"].ToString());

                Console.WriteLine();
            }
        }
Exemple #3
0
        private void Trace()
        {
            try
            {
                SqlConnectionInfo connInfo = new SqlConnectionInfo();
                if (_dbConnection.IntegratedSecurity)
                {
                    connInfo.UseIntegratedSecurity = true;
                }
                else
                {
                    connInfo.ServerName = _dbConnection.Server;
                    connInfo.UserName   = _dbConnection.UserID;
                    connInfo.Password   = _dbConnection.Password;
                }

                TraceServer trace = new TraceServer();
                trace.InitializeAsReader(connInfo, @"trace.tdf");


                while (trace.Read())
                {
                    if (!_stopTrace)
                    {
                        var record = new Trace(trace);
                        this.dgvRecord.Invoke(new ShowRecordDelegate(AddTrace), record);
                    }
                }
            }
            catch (ThreadAbortException e) { }
        }
Exemple #4
0
        /// <summary>
        /// Configure the trace to run against the local SQL Server.
        /// </summary>
        private static void SQLTraceLiveReader()
        {
            TraceServer       traceServerReader;
            SqlConnectionInfo sci;
            string            traceConfigFileName;
            string            programFilesPath;

            // Setup connection to the SQL Server
            traceServerReader = new TraceServer();

            // Use the local SQL Server
            sci = new SqlConnectionInfo();
            sci.UseIntegratedSecurity = true;

            // Test for SQL Express
            Server srvr = new Server(sci.ServerName);

            if (srvr.Information.Edition != @"Express Edition")
            {
                // Configure the reader
                // Use the Standard profiler configuration
                traceConfigFileName = Properties.Settings.Default.TraceConfigFile;
                programFilesPath    = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                traceServerReader.InitializeAsReader(sci, programFilesPath + traceConfigFileName);

                // Start reading the trace information
                SQLTraceReader(traceServerReader);
            }
            else
            {
                Console.WriteLine("SQL Express is not supported for tracing");
            }
        }
Exemple #5
0
        /// <summary>
        /// Configure the trace to run against the local SQL Server.
        /// </summary>
        private static void SQLTraceLiveReader()
        {
            TraceServer traceServerReader;
            SqlConnectionInfo sci;
            string traceConfigFileName;
            string programFilesPath;

            // Setup connection to the SQL Server
            traceServerReader = new TraceServer();

            // Use the local SQL Server
            sci = new SqlConnectionInfo();
            sci.UseIntegratedSecurity = true;

            // Test for SQL Express
            Server srvr = new Server(sci.ServerName);
            if (srvr.Information.Edition != @"Express Edition")
            {
                // Configure the reader
                // Use the Standard profiler configuration
                traceConfigFileName = Properties.Settings.Default.TraceConfigFile;
                programFilesPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
                traceServerReader.InitializeAsReader(sci, programFilesPath + traceConfigFileName);

                // Start reading the trace information
                SQLTraceReader(traceServerReader);
            }
            else
            {
                Console.WriteLine("SQL Express is not supported for tracing");
            }
        }
Exemple #6
0
        static void Main(string[] args)
        {
            // 1. Run trace server
            var connectionInfo = new SqlConnectionInfo(args[0])
            {
                DatabaseName          = args[1],
                UseIntegratedSecurity = true
            };
            var trace = new TraceServer();

            trace.InitializeAsReader(connectionInfo, args[2]);

            // 2. Continuously read trace and send them to event hubs
            var tokenSource = new CancellationTokenSource();
            var readerTask  = Task.Factory.StartNew(() => ReadTrace(trace, tokenSource.Token), tokenSource.Token);
            var senderTask  = Task.Factory.StartNew(() => SendToEventHubs(tokenSource.Token), tokenSource.Token);

            // 3. Stop the trace
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();
            Console.WriteLine("Stopping....");
            tokenSource.Cancel();

            Task.WaitAll(readerTask, senderTask);
            Console.WriteLine("Stopped");
        }
Exemple #7
0
		public void Start()
		{
			running = true;
			reader = new TraceServer();
			System.IO.FileInfo fi = new System.IO.FileInfo(@".\Sql\SqlTraceTemplate.tdf");
			reader.InitializeAsReader(ci, fi.FullName);
			Thread t = new Thread(new ThreadStart(DoTrace));
			t.Start();
		}
Exemple #8
0
        bool ConnectOlap(out TraceServer traceServer, string SSASserver)
        {
            OlapConnectionInfo ci = new OlapConnectionInfo();

            traceServer = new TraceServer();
            StringBuilder messageText = new StringBuilder();

            try
            {
                ci.UseIntegratedSecurity = true;
                ci.ServerName            = SSASserver;
                string tracetemplate = localPath + "\\" + Properties.Settings.Default.TraceDefinition;
                traceServer.InitializeAsReader(ci, tracetemplate);

                lock (traceServers)
                {
                    traceServers.Add(traceServer);
                }

                messageText.Append(
                    DateTime.Now.ToString()
                    + ":  Created trace for Analysis Server : '"
                    + SSASserver
                    + "'");

                WriteLog(messageText.ToString());
                EventLog.WriteEntry(this.ServiceName, messageText.ToString(), EventLogEntryType.Information);

                return(true);
            }

            catch (Exception e)
            {
                messageText.Append(DateTime.Now.ToString() + ":  Cannot start Analysis Server trace: ").AppendLine();
                messageText.Append(DateTime.Now.ToString() + ":  Analysis Server name: '" + SSASserver + "'").AppendLine();
                messageText.Append(DateTime.Now.ToString() + ":  Trace definition : '" + localPath + "\\" + Properties.Settings.Default.TraceDefinition + "'").AppendLine();
                messageText.Append(DateTime.Now.ToString() + ":  Error: " + e.Message).AppendLine();
                messageText.Append(DateTime.Now.ToString() + ":  Stack Trace: " + e.StackTrace).AppendLine();

                while (e.InnerException != null)
                {
                    messageText.Append("INNER EXCEPTION: ");
                    messageText.Append(e.InnerException.Message).AppendLine();
                    messageText.Append(e.InnerException.StackTrace).AppendLine();

                    e = e.InnerException;
                }

                WriteLog(messageText.ToString());
                EventLog.WriteEntry(this.ServiceName, messageText.ToString(), EventLogEntryType.Error);

                return(false);
            }
        }
Exemple #9
0
 public bool Connect()
 {
     Logger.Log("Trace:Connect(): Started", LogEntryType.Information);
     if (!FileExists(TraceTemplateFilePath))
     {
         OnTraceFileNotFoundError();
         return(false);
     }
     try
     {
         _traceServer.InitializeAsReader(_connectionInfo, TraceTemplateFilePath);
     }
     catch (Exception e)
     {
         OnConnectError(e);
         return(false);
     }
     Logger.Log("Trace:Connect(): Finished", LogEntryType.Information);
     return(true);
 }
Exemple #10
0
 public IObservable<IEventBase> GetEvents()
 {
     if (null != connectionInfo)
     {
         return GetEvents(() =>
             {
                 var trace = new TraceServer();
                 trace.InitializeAsReader(this.connectionInfo, this.filename);
                 return trace;
             });
     }
     else
     {
         return GetEvents(() =>
         {
             var trace = new TraceFile();
             trace.InitializeAsReader(filename);
             return trace;
         });
     }
 }
Exemple #11
0
        static void init()
        {
            try
            {
                // Clean up trace on process exit
                StopAndCloseTraceOnExit traceCleanup = new StopAndCloseTraceOnExit(reader);

                // Start a trace using the trace definition file (passed as first command line parameters)
                Console.WriteLine("Starting trace ...\n");

                //atring strTraceFile = @"..\..\..\doc\Standard.tdf";  // anyCPU path to doc
                string strTraceFile = @"..\..\..\doc\Standard.tdf";  // anyCPU path to doc

                ((SqlConnectionInfo)ci).UseIntegratedSecurity = true;
                // reader.InitializeAsReader(ci, @"..\..\..\doc\Standard.tdf");  // anyCPU path
                if (File.Exists(strTraceFile))
                {
                    reader.InitializeAsReader(ci, strTraceFile); // this is a template file created in SQL Profiler! e.g.  \Program Files\Microsoft SQL Server\90\Tools\Profiler\Templates folder
                                                                 // C:\Users\%USERNAME%\AppData\Roaming\Microsoft\SQL Profiler\11.0\Templates\Microsoft SQL Server\110
                                                                 // or \Program Files (x86)\Microsoft SQL Server\110\Tools\Profiler\Templates\Microsoft SQL Server\110
                }
                else
                {
                    Console.WriteLine("Trace file " + Path.GetFullPath(strTraceFile) + " not found in " + Directory.GetCurrentDirectory());
                }
            }
            catch (SqlTraceException ex)
            {
                Console.WriteLine(ex.Message);

                if (ex.InnerException != null)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }
Exemple #12
0
 public static TraceServer BeginTrace(SqlConnectionInfo sci)
 {
     TraceServer traceServerReader = new TraceServer();
     traceServerReader.InitializeAsReader(sci, GetTraceTemplateName());
     return traceServerReader;
 }
        protected void StartTrace()
        {
            try
            {
                trace = new TraceServer();
                trace.InitializeAsReader(connectionInfo, traceDefinitionFile);
                object[] userState = new object[2];
                userState[0] = this.trace;
                this.State = TraceState.Started;
                DataTable schema=null;
                while (this.trace.Read())
                {
                    try
                    {
                        if (schema == null) schema = FromTraceReaderSchema(this.trace.GetSchemaTable());
                        schema = (DataTable)schema.Clone();
                        //schmea.Clear();
                        //schema.AcceptChanges();
                        //schema = FromTraceReaderSchema(this.trace.GetSchemaTable());
                        userState[0] = ReaderToDataRow(this.trace, schema);
                        userState[1] = this.State;
                        this.backWorker.ReportProgress(0, userState);
                    }
                    catch (Exception ex)//System.AccessViolationException in Microsoft.SqlServer.ConnectionInfoExtended.dll
                    {//Microsoft.SqlServer.Management.Trace.SqlTraceException
                        System.Diagnostics.Trace.WriteLine(ex.StackTrace);
                        System.Diagnostics.Trace.WriteLine(ex.Message);
                    }
                }
                this.State = TraceState.Stopped;
                userState[1] = this.State;

            }
            catch (Exception ex)
            {
                trace = null;
                this.State = TraceState.NotInitiated;
                throw ex;
            }
        }