public static TraceRowFilter CreateLikeApplication(string applicationName)
        {
            CheckArg("applicationName", applicationName);
            var ret = new TraceRowFilter(TraceColumns.Application, applicationName, false);

            return(ret);
        }
Beispiel #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;
        }