示例#1
0
        private void ProcessJsonRequest(HttpListenerContext context, string path)
        {
            try
            {
                context.Response.AddHeader("Cache-Control", "no-cache");
                if (path.StartsWith(DiagnosticsRequest, StringComparison.OrdinalIgnoreCase))
                {
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(JsonDiag));
                    JsonDiag 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");

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

                    List<JsonEtwProvider> providers = new List<JsonEtwProvider>();
                    foreach (EtwProviderElement provider in ServiceSection.Current.EtwListener.Providers)
                    {
                        JsonEtwProvider 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);
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(JsonTraces));
                    JsonTraces 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;
                    traces.Records = Program._service._listenerService.Buffer.GetTail(startIndex, out lostCount);
                    Console.WriteLine("start index:" + startIndex + " lost:" + lostCount + " count:" + traces.Records.Length);

                    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";
            }
            catch (HttpListenerException he)
            {
                // client is probably dead ...
                Host.Log(this, "Exception (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he);
            }
            catch (Exception ex)
            {
                Host.Log(this, "Exception: " + ex);
            }
        }
示例#2
0
        private void ProcessJsonRequest(HttpListenerContext context, string path)
        {
            try
            {
                context.Response.AddHeader("Cache-Control", "no-cache");
                if (path.StartsWith(DiagnosticsRequest, StringComparison.OrdinalIgnoreCase))
                {
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(JsonDiag));
                    JsonDiag 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");

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

                    List <JsonEtwProvider> providers = new List <JsonEtwProvider>();
                    foreach (EtwProviderElement provider in ServiceSection.Current.EtwListener.Providers)
                    {
                        JsonEtwProvider 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);
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(JsonTraces));
                    JsonTraces 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;
                    traces.Records = Program._service._listenerService.Buffer.GetTail(startIndex, out lostCount);
                    Console.WriteLine("start index:" + startIndex + " lost:" + lostCount + " count:" + traces.Records.Length);

                    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";
            }
            catch (HttpListenerException he)
            {
                // client is probably dead ...
                Host.Log(this, "Exception (code: " + he.ErrorCode + "/" + he.NativeErrorCode + "): " + he);
            }
            catch (Exception ex)
            {
                Host.Log(this, "Exception: " + ex);
            }
        }