public String Post([FromBody] NewSessionInfo input) { SessionController sessionController = new SessionController(); List <Diagnoser> diagnosers = new List <Diagnoser>(sessionController.GetAllDiagnosers().Where(p => input.Diagnosers.Contains(p.Name))); String SessionId = ""; if (input.RunLive) { TimeSpan ts; if (!TimeSpan.TryParse(input.TimeSpan, out ts)) { ts = TimeSpan.FromMinutes(5); } if (input.CollectLogsOnly) { SessionId = sessionController.CollectLiveDataLogs(ts, diagnosers, null, input.Description).SessionId.ToString(); } else { SessionId = sessionController.TroubleshootLiveData(ts, diagnosers, null, input.Description).SessionId.ToString(); } } else { DateTime startTime = DateTime.Parse(input.StartTime); DateTime endTime = DateTime.Parse(input.EndTime); if (input.CollectLogsOnly) { SessionId = sessionController.CollectLogs(startTime, endTime, diagnosers, null, input.Description).SessionId.ToString(); } else { SessionId = sessionController.Troubleshoot(startTime, endTime, diagnosers, null, input.Description).SessionId.ToString(); } } return(SessionId); }
public HttpResponseMessage Post([FromBody] NewSessionInfo input) { //Simulate Delay //System.Threading.Thread.Sleep(20000); //Simulate Failure //return ""; String SessionId = String.Empty; string InstancesSubmitted = ""; try { SessionController sessionController = new SessionController(); sessionController.StartSessionRunner(); List <Diagnoser> diagnosers = new List <Diagnoser>(sessionController.GetAllDiagnosers().Where(p => input.Diagnosers.Contains(p.Name))); List <Instance> instances = new List <Instance>(); instances = input.Instances.Any() ? new List <Instance>(sessionController.GetAllRunningSiteInstances().Where(p => input.Instances.Contains(p.Name))) : new List <Instance>(sessionController.GetAllRunningSiteInstances()); InstancesSubmitted = string.Join(",", instances.Select(x => x.Name)); TimeSpan ts; if (!TimeSpan.TryParse(input.TimeSpan, out ts)) { ts = TimeSpan.FromMinutes(5); } if (input.RunLive) { if (input.CollectLogsOnly) { SessionId = sessionController.CollectLiveDataLogs(ts, diagnosers, false, instances, input.Description, input.BlobSasUri).SessionId.ToString(); } else { SessionId = sessionController.TroubleshootLiveData(ts, diagnosers, false, instances, input.Description, input.BlobSasUri).SessionId.ToString(); } } else { if (string.IsNullOrEmpty(input.StartTime)) { throw new ArgumentNullException("When RunLive is false or not specified, StartTime must be provided"); } DateTime startTime = DateTime.Parse(input.StartTime); //startTime = DateTime.SpecifyKind(startTime, DateTimeKind.Utc); if (startTime > DateTime.UtcNow) { DateTime endTime = startTime + ts; if (input.CollectLogsOnly) { SessionId = sessionController.CollectLogs(startTime, endTime, diagnosers, false, null, input.Description, input.BlobSasUri).SessionId.ToString(); } else { SessionId = sessionController.Troubleshoot(startTime, endTime, diagnosers, false, null, input.Description, input.BlobSasUri).SessionId.ToString(); } } else { if (input.CollectLogsOnly) { SessionId = sessionController.CollectLiveDataLogs(ts, diagnosers, false, instances, input.Description, input.BlobSasUri).SessionId.ToString(); } else { SessionId = sessionController.TroubleshootLiveData(ts, diagnosers, false, instances, input.Description, input.BlobSasUri).SessionId.ToString(); } } } } catch (Exception ex) { //log to Kusto and re-throw (it will be taken care in support API) Logger.LogErrorEvent("Encountered exception while submitting session", ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, $"Encountered exception {ex.Message} while submitting session")); } return(Request.CreateResponse(HttpStatusCode.OK, SessionId)); }
private static void CollectLogsAndTakeActions(Options options, string[] args, ref int argNum) { try { var diagnosersToRun = GetDiagnosersToRun(args, ref argNum); TimeSpan timeToRunFor = GetTimeSpanFromArg(args, ref argNum); string blobSasUri = GetBlobSasUriFromArg(args, ref argNum); bool runOnAllInstances = GetRunAllInstancesFromArg(args, ref argNum); var currentInstance = Instance.GetCurrentInstance(); List <Instance> Instances = new List <Instance>(); if (!runOnAllInstances) { Instances.Add(currentInstance); Console.WriteLine($"Running Diagnosers on {currentInstance.Name} instance only"); } else { // An empty diagnoser list means runs DaaS on all instances. Console.WriteLine("Running Diagnosers on all instances"); } Console.WriteLine($"BlobSaUri for the session is '{blobSasUri}'"); if (!string.IsNullOrWhiteSpace(blobSasUri)) { var message = DaaS.Storage.BlobController.ValidateBlobSasUri(blobSasUri); if (!string.IsNullOrWhiteSpace(message)) { throw new ApplicationException($"BlobSasUri specified is invalid. Failed with error - {message}"); } } var details = new { Diagnoser = string.Join(",", diagnosersToRun.Select(x => x.Name)), TimeSpanToRunFor = timeToRunFor.TotalSeconds.ToString("0"), HasBlobSasUri = !string.IsNullOrWhiteSpace(blobSasUri), AllInstances = runOnAllInstances, InstancesSelected = string.Join(",", Instances.Select(x => x.Name)), Options = options.ToString() }; var detailsString = JsonConvert.SerializeObject(details); Logger.LogDaasConsoleEvent("DaasConsole started a new Session", detailsString); EventLog.WriteEntry("Application", $"DaasConsole started with {detailsString} ", EventLogEntryType.Information); // Collect the logs for just the current instance Console.WriteLine("Starting data collection"); Session session = null; if (options == Options.CollectKillAnalyze || options == Options.CollectLogs) { session = SessionController.CollectLiveDataLogs( timeToRunFor, diagnosersToRun, true, Instances, null, blobSasUri); } else if (options == Options.Troubleshoot) { session = SessionController.TroubleshootLiveData( timeToRunFor, diagnosersToRun, true, Instances, null, blobSasUri); } Console.WriteLine("Waiting for collection to complete..."); Console.WriteLine($"Going to sleep for {timeToRunFor} seconds"); Thread.Sleep(timeToRunFor); Console.WriteLine("done sleeping"); do { Thread.Sleep(TimeSpan.FromSeconds(10)); Console.Write("."); session = SessionController.GetSessionWithId(session.SessionId); } while (session.Status == SessionStatus.Active); Console.WriteLine("Completed"); if (options == Options.CollectKillAnalyze) { Console.WriteLine("Analyzing collected logs"); // Say we want the logs analyzed SessionController.Analyze(session); // Kill the regular site's w3wp process. The logs can be analyzed by another instance or by this same instance when it is in a healtheir state Process mainSiteW3wpProcess = GetMainSiteW3wpProcess(); Console.WriteLine("Killing process {0} with pid {1}", mainSiteW3wpProcess.ProcessName, mainSiteW3wpProcess.Id); mainSiteW3wpProcess.Kill(); string sessionId = session != null?session.SessionId.ToString() : string.Empty; Logger.LogSessionVerboseEvent($"DaasConsole killed process {mainSiteW3wpProcess.ProcessName} with pid {mainSiteW3wpProcess.Id}", sessionId); } } catch (Exception ex) { string logMessage = $"Unhandled exception in DaasConsole.exe - {ex.GetType().ToString()}:{ex.Message}"; EventLog.WriteEntry("Application", logMessage, EventLogEntryType.Information); Console.WriteLine(logMessage); Logger.LogErrorEvent("Unhandled exception in DaasConsole.exe while collecting logs and taking actions", ex); } }