0

I need to write a program which can collect SQL Server trace logs from C code. There seem to be a couple methods to do this:

  1. Using the most common SQL trace method where trace files are mapped to tables via stored procedures. This trace log can then be collected using SQL/ODBC. Problem with this is that trace files are rotated and limited in size. While the trace is collected it might be possible that the trace file is swapped and not all events are gathered. Second, these stored procedures will be removed in future versions, so it does not seem to be a good idea to build upon.
  2. The recommended way is using Extended Events instead. This supports ETW and file targets, but the setup seems to be more complex on the server side and not all SQL Server versions seem to support this.
  3. There is also SMO but this does not provide a C API.

So what's the recommended way to properly collect SQL Server trace logs?

b0ti
  • 2,319
  • 1
  • 18
  • 18
  • 1
    It seems like your only option is (1) if you need to support versions < SQL Server 2008 and you are committed to doing this in C. Your first objection to (1) is somewhat unfounded. Unless you are collecting millions of trace rows per minute, set the trace file size ridiculously low, or simply not running your program often enough, you really are in very little danger of missing anything. Your second objection is similarly unfounded - if you need to support SQL 2005 it's your only choice anyway, but "future versions" has yet to be defined. So you need to make a choice - support 2005 or not? – Aaron Bertrand Nov 27 '12 at 22:17

1 Answers1

-1

This seems to be a popular question. Learn C# and use

public void FileToTable()
{
    TraceServer reader = new TraceServer();

    ConnectionInfoBase ci = new SqlConnectionInfo("localhost");
    ((SqlConnectionInfo)ci).UseIntegratedSecurity = true;

    reader.InitializeAsReader(ci, @"Standard.tdf");

    int eventNumber = 0;

    while (reader.Read())
    {
        Console.Write( "{0}\n", reader.GetValue(0).ToString() );
    }
    reader.Close();        
}
brian beuning
  • 2,836
  • 18
  • 22
  • Sorry, but the question was how to do it from C, not how to do it from another language. – b0ti Dec 16 '12 at 10:23