Exemple #1
0
        public TraceGroupsReport <TKey> ReadGroupsReport <TKey>(TraceColumns groupingField)
        {
            if ((_columns & groupingField) == 0)
            {
                throw new ArgumentException("Axis field " + groupingField + " is not included in trace", "groupingField");
            }

            TraceFieldInfo info = TraceFieldInfo.Get(groupingField);

            string sql = string.Format(
                SQL_SELECT_GROUPS,
                info.GroupExpression);

            using (SqlConnection con = new SqlConnection(_connectionString))
            {
                if (con.State != ConnectionState.Open)
                {
                    con.Open();
                }

                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    cmd.Parameters.Add("@file", SqlDbType.NVarChar).Value = _traceFile + ".trc";
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        TraceGroupsReport <TKey> ret = new TraceGroupsReport <TKey>();
                        while (rdr.Read())
                        {
                            object      rawKey   = rdr.IsDBNull(0) ? null : rdr.GetValue(0);
                            int         count    = rdr.GetInt32(1);
                            SqlCounters counters = ReadCounters(rdr, 2);
                            if (counters != null)
                            {
                                counters.Requests = count;
                                TKey key = (TKey)rawKey;
                                SqlGroupCounters <TKey> group =
                                    new SqlGroupCounters <TKey>()
                                {
                                    Group = key, Count = count, Counters = counters
                                };

                                ret.Add(key, group);
                            }
                        }

                        return(ret);
                    }
                }
            }
        }
Exemple #2
0
        public void Start(string connectionString, string tracePath, TraceColumns columns, params TraceRowFilter[] rowFilters)
        {
            _connectionString = connectionString;
            _traceFile        = Path.Combine(tracePath, "trace_" + Guid.NewGuid().ToString("N"));
            _columns          = columns;

            // For non-local sql server it doesn't work
            if (_PrevCreatedDirectory != tracePath)
            {
                if (!Directory.Exists(tracePath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(_traceFile));
                }

                _PrevCreatedDirectory = tracePath;
            }

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                con.Open();

                StringBuilder sqlSetFields = new StringBuilder();
                foreach (TraceColumns field in TraceFieldInfo.ToArray(columns))
                {
                    TraceFieldInfo info = TraceFieldInfo.Get(field);
                    sqlSetFields.AppendFormat(SQL_SET_TRACE_COLUMN, info.SqlId, field);
                }

                List <SqlParameter> parameters        = new List <SqlParameter>();
                List <TraceColumns> rowFilterColumns  = new List <TraceColumns>();
                TraceRowFilter[]    distinctRowFilter = TraceRowFilter.GetDistinct(rowFilters);
                foreach (TraceRowFilter rowFilter in distinctRowFilter)
                {
                    TraceFieldInfo info  = TraceFieldInfo.Get(rowFilter.Column);
                    string         pName = "@filter_" + info.GroupExpression + "_" + parameters.Count;
                    string         comparisonOperator = rowFilter.StrictEquality ? "0" : "6";
                    object         comparisonValue    = rowFilter.Value;
                    if (!rowFilter.StrictEquality && comparisonValue != null)
                    {
                        comparisonValue = "%" + Convert.ToString(comparisonValue).Replace("'", "''") + "%";
                    }

                    sqlSetFields.AppendFormat("exec sp_trace_setfilter @TRACE, {0}, 0, {1}, {2} -- {3}",
                                              info.SqlId,
                                              comparisonOperator,
                                              pName,
                                              info.GroupExpression);

                    sqlSetFields.AppendLine();
                    SqlParameter p = new SqlParameter(pName, comparisonValue);
                    parameters.Add(p);

                    if (rowFilterColumns.Contains(rowFilter.Column))
                    {
                        throw new ArgumentException("Duplicate row filter column " + rowFilter.Column, "rowFilters");
                    }

                    rowFilterColumns.Add(rowFilter.Column);
                }

                string sqlCmd = SQL_START1_TRACE + sqlSetFields + SQL_START2_TRACE;
                // Console.WriteLine($"TRACE ON {connectionString}");
                using (SqlCommand cmd = new SqlCommand(sqlCmd, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@file", SqlDbType.NVarChar).Value = _traceFile;
                    var maxFileSize = Math.Max(8, MaxFileSize);
                    cmd.Parameters.Add("@MaxFileSize", SqlDbType.BigInt).Value = maxFileSize;
                    cmd.Parameters.AddRange(parameters.ToArray());
                    _traceId = (int)cmd.ExecuteScalar();
                    // PInvoke.DeleteFileOnReboot(_traceFile + ".trc");
                }
            }

            _NeedStop = true;
        }