void GetTraceTable(object sender) { if (traceId == 0) { return; } try { if (Monitor.TryEnter(syncObj)) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = string.Format("select * from (select ROW_NUMBER() OVER (order by StartTime) as RowNum, {0} from fn_trace_gettable(@filename, default)) as dt where RowNum > @lastrownum", this.traceFields.Count == 0 ? "*" : string.Join(",", traceFields.ToArray())); MsSqlUtil.AddInParam(cmd, "@filename", traceFilePath); MsSqlUtil.AddInParam(cmd, "@lastrownum", lastRowNum); DataTable table = MsSqlUtil.ExecuteAsDataTable(cmd, connInfo.CreateConnectionObject()); if (table.Rows.Count > 0) { lastRowNum = Convert.ToInt32(table.Compute("max(RowNum)", string.Empty)); } Monitor.Exit(syncObj); OnTraceEvent(table); } } catch (Exception exc) { Console.WriteLine(exc); } }
private StatusErrorCode SetTraceStatus(TraceStatus status) { if (traceId == 0) { throw new NotInitializedProfilerException(); } StatusErrorCode code = StatusErrorCode.IsInvalid; try { SqlCommand cmd = MsSqlUtil.NewStoredProcedure("sp_trace_setstatus"); MsSqlUtil.AddInParam(cmd, "@traceid", traceId); MsSqlUtil.AddInParam(cmd, "@status", status); code = (StatusErrorCode)MsSqlUtil.ExecuteStoredProcedure(cmd, connInfo.CreateConnectionObject()); if (code == StatusErrorCode.NoError) { this.traceStatus = status; } } catch (SqlException exc) { if (exc.Message.Contains("Could not find the requested trace")) { return(StatusErrorCode.TraceIsInvalid); } //throw; } return(code); }
private CreateTraceErrorCode InitTrace(TraceOptions traceOptions, string traceFilePath, int?maxFileSize, DateTime?stopTime) { SqlCommand cmd = MsSqlUtil.NewStoredProcedure("sp_trace_create"); SqlParameter tId = MsSqlUtil.AddOutParam(cmd, "@traceid", traceId); MsSqlUtil.AddInParam(cmd, "@options", (int)traceOptions); MsSqlUtil.AddInParam(cmd, "@tracefile", traceFilePath); if (maxFileSize != null) { MsSqlUtil.AddInParam(cmd, "@maxfilesize", maxFileSize); } if (stopTime != null) { MsSqlUtil.AddInParam(cmd, "@stoptime", stopTime); } int result = MsSqlUtil.ExecuteStoredProcedure(cmd, connInfo.CreateConnectionObject()); traceId = Convert.ToInt32(tId.Value); this.traceFilePath = traceFilePath + ".trc"; //Add filter to filter profiler stored procedures AddTraceFilter(TraceField.ApplicationName, LogicalOperator.AND, ComparisonOperator.NotEqual, connInfo.ApplicationName); return((CreateTraceErrorCode)result); }
private bool TraceExists(string tracePath) { SqlCommand cmd = MsSqlUtil.NewQuery("select count(*) from sys.traces where path = @tracePath"); MsSqlUtil.AddInParam(cmd, "@tracePath", tracePath); int count = (int)MsSqlUtil.ExecuteScalar(cmd, connInfo.CreateConnectionObject()); return(count > 0); }
public AddTraceFilterErrorCode AddTraceFilter <T>(TraceField traceField, LogicalOperator logicalOp, ComparisonOperator compOp, T value) { if (traceId == 0) { throw new NotInitializedProfilerException(); } SqlCommand cmd = MsSqlUtil.NewStoredProcedure("sp_trace_setfilter"); MsSqlUtil.AddInParam(cmd, "@traceid", traceId); MsSqlUtil.AddInParam(cmd, "@columnid", (int)traceField); MsSqlUtil.AddInParam(cmd, "@logical_operator", (int)logicalOp); MsSqlUtil.AddInParam(cmd, "@comparison_operator", (int)compOp); MsSqlUtil.AddInParam(cmd, "@value", value); return((AddTraceFilterErrorCode)MsSqlUtil.ExecuteStoredProcedure(cmd, connInfo.CreateConnectionObject())); }
public AddTraceEventErrorCode AddTraceEvent(TraceEvent traceEvent, params TraceField[] traceFields) { if (traceId == 0) { throw new NotInitializedProfilerException(); } foreach (TraceField field in traceFields) { SqlCommand cmd = MsSqlUtil.NewStoredProcedure("sp_trace_setevent"); MsSqlUtil.AddInParam(cmd, "@traceid", traceId); MsSqlUtil.AddInParam(cmd, "@eventid", (int)traceEvent); MsSqlUtil.AddInParam(cmd, "@columnid", (int)field); MsSqlUtil.AddInParam(cmd, "@on", true); MsSqlUtil.ExecuteStoredProcedure(cmd, connInfo.CreateConnectionObject()); if (!this.traceFields.Contains(field.ToString())) { this.traceFields.Add(field.ToString()); } } return(AddTraceEventErrorCode.NoError); }