Example #1
0
        private void ProcessJsonRequest(HttpListenerContext context, string path)
        {
            context.Response.AddHeader("Cache-Control", "no-cache");
            if (path.StartsWith(DiagnosticsRequest, StringComparison.OrdinalIgnoreCase))
            {
                var s = new DataContractJsonSerializer(typeof(JsonDiag));
                var diag = new JsonDiag();

                diag.Version = Assembly.GetExecutingAssembly().GetInformationalVersion() + " - " + Assembly.GetExecutingAssembly().GetConfiguration();
                diag.Time = DateTime.UtcNow.ToString("r");
                diag.AssemblyDate = Extensions.AssemblyDate.ToString("r");
                diag.OSVersion = Environment.OSVersion.ToString();
                diag.ClrVersion = Environment.Version.ToString() + " - " + (IntPtr.Size == 8 ? "64" : "32") + "-bit";
                diag.ProcessorCount = Environment.ProcessorCount;
                diag.WorkingSet = Extensions.FormatFileSize(Environment.WorkingSet);
                diag.CpuUsage = AppDomain.MonitoringIsEnabled ? AppDomainMonitor.CpuUsage + " %" : "n/a";
                diag.MemoryUsage = AppDomain.MonitoringIsEnabled ? Extensions.FormatFileSize(AppDomainMonitor.MemoryUsage) : "n/a";

                diag.BufferCapacity = Program._service._listenerService.Buffer.Capacity;
                diag.BufferCount = Program._service._listenerService.Buffer.Count;
                diag.BufferTotalCount = Program._service._listenerService.Buffer.TotalCount;
                diag.SessionId = Program._service._listenerService.SessionId.ToString("N");

                var prefixes = new List<JsonPrefix>();
                foreach (PrefixElement prefix in ServiceSection.Current.WebServer.Prefixes)
                {
                    var jp = new JsonPrefix();
                    jp.Enabled = prefix.Enabled;
                    jp.Uri = prefix.Uri;
                    jp.BasePath = prefix.BasePath;
                    prefixes.Add(jp);
                }
                diag.Prefixes = prefixes.ToArray();

                var providers = new List<JsonEtwProvider>();
                foreach (EtwProviderElement provider in ServiceSection.Current.EtwListener.Providers)
                {
                    var jpr = new JsonEtwProvider();
                    jpr.Enabled = provider.Enabled;
                    jpr.Description = provider.Description;
                    jpr.Guid = provider.Guid.ToString("N");
                    jpr.TraceLevel = provider.TraceLevel;
                    providers.Add(jpr);
                }
                diag.EtwProviders = providers.ToArray();

                s.WriteObject(context.Response.OutputStream, diag);
            }
            else if (path.StartsWith(TracesRequest, StringComparison.OrdinalIgnoreCase))
            {
                long startIndex = GetStartIndex(path, out Guid sessionId);
                var s = new DataContractJsonSerializer(typeof(JsonTraces));
                var traces = new JsonTraces();
                traces.BufferCapacity = Program._service._listenerService.Buffer.Capacity;
                traces.BufferCount = Program._service._listenerService.Buffer.Count;
                traces.BufferTotalCount = Program._service._listenerService.Buffer.TotalCount;
                traces.SessionId = Program._service._listenerService.SessionId.ToString("N");

                long lostCount;
                long totalCount;
                if (sessionId != Program._service._listenerService.SessionId)
                {
                    Console.WriteLine("new session");
                    startIndex = 0;
                    lostCount = -1; // new session
                    totalCount = -1;
                }
                else
                {
                    // an error here also sets lostcount to -1
                    int maxCount = 1000;
                    traces.Records = Program._service._listenerService.Buffer.GetTail(startIndex, maxCount, false, out totalCount, out lostCount);
                }
                Console.WriteLine("start index:" + startIndex + " totalCount:" + totalCount + " lostCount:" + lostCount + " records:" + (traces.Records != null ? traces.Records.Length : 0));

                traces.LostCount = lostCount;
                s.WriteObject(context.Response.OutputStream, traces);
            }
            else
            {
                Write404(context.Response, path);
                return;
            }

            context.Response.StatusCode = (int)HttpStatusCode.OK;
            context.Response.StatusDescription = "OK";
            context.Response.ContentType = "application/json; charset=utf-8";
        }
Example #2
0
        private void ListenerCallback(IAsyncResult result)
        {
            //Host.Log(this, "ListenerCallback result: " + result);
            if (!_listener.IsListening)
                return;

            if (!string.IsNullOrEmpty(Program.OptionLcid))
            {
                Extensions.SetCurrentThreadCulture(Program.OptionLcid);
            }

            _listener.BeginGetContext(ListenerCallback, null);
            
            HttpListenerContext context = _listener.EndGetContext(result);
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;

            if (_listener.AuthenticationSchemes != AuthenticationSchemes.Anonymous)
            {
                if (!OnAuthenticate(context))
                {
                    Host.Log(this, "Request Access is denied");
                    SafeWrite(response, "Access is denied", (int)HttpStatusCode.Unauthorized, "Access is denied");
                    response.OutputStream.Close();
                    return;
                }
            }

            //Host.Log(this, "AssemblyDate: " + Extensions.AssemblyDate);
            //Host.Log(this, "Request LocalEndPoint: " + request.LocalEndPoint);
            //Host.Log(this, "Request ServiceName: " + request.ServiceName);
            Host.Log(this, "Request Method: " + request.HttpMethod);
            //Host.Log(this, "Request RemoteEndPoint: " + request.RemoteEndPoint);
            Host.Log(this, "Request Url: " + request.Url);
            //Host.Log(this, "Request RawUrl: " + request.RawUrl);
            foreach (string name in request.Headers)
            {
                Host.Log(this, "Header " + name + ": " + request.Headers[name]);
            }

            if (request.Url == null)
                return;

            Host.Log(this, "Request RelativeUrl: " + ServiceSection.Current.WebServer.Prefixes.GetRelativePath(request.RawUrl));

            try
            {
                context.Response.AddHeader("Server", "TraceSpyServer/" + Assembly.GetExecutingAssembly().GetInformationalVersion() + "/");
                ProcessRequest(context);
            }
            catch (HttpListenerException he)
            {
                Host.Log(this, "HttpListenerException (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he);
                if (IsDeadClient(he))
                {
                    // don't log this
                    Host.Log(this, "Client is dead.");
                    return;
                }
                Write(response, he);
                if (Debugger.IsAttached)
                    throw;
            }
            catch (Exception e)
            {
                Host.Log(this, "Exception: " + e);
                Write(response, e);
                if (Debugger.IsAttached)
                    throw;
            }

            try
            {
                response.OutputStream.Close();
            }
            catch (HttpListenerException he)
            {
                // client may have disappeared
                Host.Log(this, "HttpListenerException (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he.Message);
            }
            catch (Exception e)
            {
                Host.Log(this, "write Exception:" + e.Message);
                if (Debugger.IsAttached)
                    throw;
            }
        }