Ejemplo n.º 1
0
 public static void StopTracing()
 {
     lock (TracingMutex)
     {
         if (_tracing == null || _tracingState == 0)
         {
             return;
         }
         _tracing.Stop();
         _tracing      = null;
         _tracingState = 0;
     }
 }
Ejemplo n.º 2
0
        public static void StartTracing(string traceFileName)
        {
            lock (TracingMutex)
            {
                if (_tracing != null || _tracingState == 1)
                {
                    return;
                }
                _tracing = new CollectTrace.Tracing();
            }

            Task.Run(async() => await CollectTrace.Collect(_traceSizeChangeDelegates.Values, _tracing,
                                                           new FileInfo(traceFileName + ".nettrace")));

            lock (TracingMutex)
            {
                _tracingState = 1;
            }
        }
Ejemplo n.º 3
0
        public static void StartTracing(string traceFileName, string traceFileFormatName)
        {
            lock (TracingMutex)
            {
                if (_tracing != null || _tracingState == 1)
                {
                    return;
                }
                _tracing = new CollectTrace.Tracing();
            }

            if (!Enum.TryParse <TraceFileFormat>(traceFileFormatName, true, out var traceFileFormat))
            {
                traceFileFormat = TraceFileFormat.NetTrace;
            }

            Task.Run(async() => await CollectTraceClient.Collect(TraceSizeChangeDelegates.Values, _tracing,
                                                                 new FileInfo(traceFileName + ".nettrace"), format: traceFileFormat));

            lock (TracingMutex)
            {
                _tracingState = 1;
            }
        }
Ejemplo n.º 4
0
        public static async Task <int> Collect(ICollection <Action <long> > sizeChangeCallbacks, CollectTrace.Tracing tracing, FileInfo output, TraceFileFormat format = TraceFileFormat.NetTrace)
        {
            var processId = Process.GetCurrentProcess().Id;

            var providerCollection = CreateProviderCollection();

            var diagnosticsClient    = new DiagnosticsClient(processId);
            EventPipeSession session = null;

            try
            {
                session = diagnosticsClient.StartEventPipeSession(providerCollection, true);
            }
            catch (DiagnosticsClientException e)
            {
                Console.Error.WriteLine($"Unable to start a tracing session: {e.ToString()}");
            }

            if (session == null)
            {
                Console.Error.WriteLine("Unable to create session.");
                return(ErrorCodes.SessionCreationError);
            }

            var failed     = false;
            var terminated = false;

            try
            {
                var collectingTask = new Task(() =>
                {
                    try
                    {
                        using (var fs = new FileStream(output.FullName, FileMode.Create, FileAccess.Write))
                        {
                            var buffer = new byte[16 * 1024];

                            while (true)
                            {
                                int nBytesRead = session.EventStream.Read(buffer, 0, buffer.Length);
                                if (nBytesRead <= 0)
                                {
                                    break;
                                }
                                fs.Write(buffer, 0, nBytesRead);
                                foreach (var sizeChangeCallback in sizeChangeCallbacks)
                                {
                                    sizeChangeCallback(fs.Length);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        failed = true;
                        Console.Error.WriteLine($"[ERROR] {ex.ToString()}");
                    }
                    finally
                    {
                        terminated = true;
                    }
                });
                collectingTask.Start();

                tracing.Wait();

                session.Stop();

                await collectingTask;

                //if (format != TraceFileFormat.NetTrace)
                //    TraceFileFormatConverter.ConvertToFormat(format, output.FullName);

                return(failed ? ErrorCodes.TracingError : 0);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"[ERROR] {ex.ToString()}");
                return(ErrorCodes.UnknownError);
            }
        }