private void WriteEndTrace() { try { var info = _infos.Pop(); var elapsed = DateTime.UtcNow - info.StartTime; var strb = new StringBuilder(); if (_isStartElement) { strb.Append("/>"); } else { strb.Append(new String(' ', _infos.Count * 2)); strb.Append("</step>"); } strb.AppendLine(String.Format("<!-- duration: {0:0}ms -->", elapsed.TotalMilliseconds)); FileSystemHelpers.AppendAllTextToFile(_file, strb.ToString()); _isStartElement = false; // adjust filename with statusCode if (info.Title == XmlTracer.OutgoingResponseTrace && _file.EndsWith(PendingXml, StringComparison.OrdinalIgnoreCase)) { if (!Int32.TryParse(info.Attributes["statusCode"], out _statusCode)) { _statusCode = 0; } } if (_infos.Count == 0) { // avoid spamming the traces if (!ShouldTrace(_level, info, _statusCode)) { FileSystemHelpers.DeleteFileSafe(_file); } else { var file = _file; if (_file.EndsWith(PendingXml, StringComparison.OrdinalIgnoreCase)) { file = file.Replace(PendingXml, _statusCode <= 0 ? ".xml" : String.Format("_{0}.xml", _statusCode)); } file = file.Replace(".xml", String.Format("_{0:0}s.xml", elapsed.TotalSeconds)); FileSystemHelpers.MoveFile(_file, file); } _file = null; } } catch (Exception ex) { WriteUnexpectedException(ex); } }
private IDisposable WriteStartElement(string title, IDictionary <string, string> attribs) { try { var info = new TraceInfo(title, attribs); var ensureMaxXmlFiles = false; if (String.IsNullOrEmpty(_file)) { ensureMaxXmlFiles = true; // generate trace file name base on attribs _file = GenerateFileName(info); } var strb = new StringBuilder(); if (_isStartElement) { strb.AppendLine(">"); } strb.Append(new String(' ', _infos.Count * 2)); strb.AppendFormat("<step title=\"{0}\" ", XmlUtility.EscapeXmlText(title)); strb.AppendFormat("date=\"{0}\" ", info.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fff")); if (_infos.Count == 0) { strb.AppendFormat("instance=\"{0}\" ", InstanceIdUtility.GetShortInstanceId()); } foreach (var attrib in attribs) { if (TraceExtensions.IsNonDisplayableAttribute(attrib.Key)) { continue; } strb.AppendFormat("{0}=\"{1}\" ", attrib.Key, XmlUtility.EscapeXmlText(attrib.Value)); } FileSystemHelpers.AppendAllTextToFile(_file, strb.ToString()); _infos.Push(info); _isStartElement = true; if (ensureMaxXmlFiles) { EnsureMaxXmlFiles(); } return(new DisposableAction(() => WriteEndTrace())); } catch (Exception ex) { WriteUnexpectedException(ex); } return(DisposableAction.Noop); }
private void WriteEndTrace() { try { var info = _infos.Pop(); var strb = new StringBuilder(); if (_isStartElement) { strb.AppendLine("/>"); } else { strb.Append(new String(' ', _infos.Count * 2)); strb.AppendLine("</step>"); } FileSystemHelpers.AppendAllTextToFile(_file, strb.ToString()); _isStartElement = false; // adjust filename with statusCode if (info.Title == XmlTracer.OutgoingResponseTrace && _file.EndsWith(PendingXml, StringComparison.OrdinalIgnoreCase)) { var file = _file.Replace(PendingXml, String.Format("_{0}.xml", info.Attributes["statusCode"])); FileSystemHelpers.MoveFile(_file, file); _file = file; } if (_infos.Count == 0) { // Suppress traces with NotModified statusCode to avoid client polling in nature if (_file.EndsWith("_304.xml", StringComparison.OrdinalIgnoreCase) && _level != System.Diagnostics.TraceLevel.Verbose) { FileSystemHelpers.DeleteFileSafe(_file); } else if (_file.EndsWith(PendingXml, StringComparison.OrdinalIgnoreCase)) { var file = _file.Replace(PendingXml, ".xml"); FileSystemHelpers.MoveFile(_file, file); } _file = null; } } catch (Exception ex) { WriteUnexpectedException(ex); } }
protected void SafeLogToFile(string path, string content, bool isAppend = true) { try { if (isAppend) { OperationManager.Attempt(() => FileSystemHelpers.AppendAllTextToFile(path, content)); } else { OperationManager.Attempt(() => FileSystemHelpers.WriteAllTextToFile(path, content)); } } catch (Exception ex) { TraceFactory.GetTracer().TraceError(ex); } }
private void WriteUnexpectedException(Exception ex) { try { var strb = new StringBuilder(); strb.AppendFormat("{0}_{1}_{2:000}_UnexpectedException.xml", DateTime.UtcNow.ToString("yyyy-MM-ddTHH-mm-ss"), InstanceIdUtility.GetShortInstanceId(), Interlocked.Increment(ref _salt) % 1000); FileSystemHelpers.AppendAllTextToFile( Path.Combine(_path, strb.ToString()), String.Format("<exception>{0}</exception>", XmlUtility.EscapeXmlText(ex.ToString()))); } catch { // no-op } }
protected void SafeLogToFile(string path, string content, bool isAppend = true) { try { // since we don't have proper lock on file, we are more forgiving in retry (10 times 250 ms interval). if (isAppend) { OperationManager.Attempt(() => FileSystemHelpers.AppendAllTextToFile(path, content), retries: 10); } else { OperationManager.Attempt(() => FileSystemHelpers.WriteAllTextToFile(path, content), retries: 10); } } catch (Exception ex) { Analytics.UnexpectedException(ex); } }
public async Task <ScanRequestResult> StartScan(String timeout, String mainScanDirPath, String id, String host) { using (_tracer.Step("Start scan in the background")) { String folderPath = Path.Combine(mainScanDirPath, Constants.ScanFolderName + id); String filePath = Path.Combine(folderPath, Constants.ScanStatusFile); Boolean hasFileModifcations = true; if (_deploymentLock.IsHeld) { return(ScanRequestResult.ScanAlreadyInProgress); } //Create unique scan folder and scan status file _deploymentLock.LockOperation(() => { //Check if files are modified if (CheckModifications(mainScanDirPath)) { //Create unique scan directory for current scan FileSystemHelpers.CreateDirectory(folderPath); _tracer.Trace("Unique scan directory created for scan {0}", id); //Create scan status file inside folder FileSystemHelpers.CreateFile(filePath).Close(); //Create temp file to check if scan is still running string tempScanFilePath = GetTempScanFilePath(mainScanDirPath); tempScanFilePath = Path.Combine(mainScanDirPath, Constants.TempScanFile); FileSystemHelpers.CreateFile(tempScanFilePath).Close(); UpdateScanStatus(folderPath, ScanStatus.Starting); } else { hasFileModifcations = false; } }, "Creating unique scan folder", TimeSpan.Zero); if (!hasFileModifcations) { return(ScanRequestResult.NoFileModifications); } //Start Backgorund Scan using (var timeoutCancellationTokenSource = new CancellationTokenSource()) { var successfullyScanned = PerformBackgroundScan(_tracer, folderPath, timeoutCancellationTokenSource.Token, id, mainScanDirPath); //Wait till scan task completes or the timeout goes off if (await Task.WhenAny(successfullyScanned, Task.Delay(Int32.Parse(timeout), timeoutCancellationTokenSource.Token)) == successfullyScanned) { //If scan task completes before timeout //Delete excess scan folders, just keep the maximum number allowed await DeletePastScans(mainScanDirPath, _tracer); //Create new Manifest file containing the modified timestamps String manifestPath = Path.Combine(mainScanDirPath, Constants.ScanManifest); if (FileSystemHelpers.FileExists(manifestPath)) { FileSystemHelpers.DeleteFileSafe(manifestPath); } JObject manifestObj = new JObject(); //Write to the manifest with new timestamps of the modified file ModifyManifestFile(manifestObj, Constants.ScanDir); File.WriteAllText(manifestPath, JsonConvert.SerializeObject(manifestObj)); //Path to common log file for azure monitor String aggrLogPath = Path.Combine(mainScanDirPath, Constants.AggregrateScanResults); //This checks if result scan log is formed //If yes, it will append necessary logs to the aggregrate log file //Current appended logs will be "Scanned files","Infected files", and details of infected files String currLogPath = Path.Combine(folderPath, Constants.ScanLogFile); if (FileSystemHelpers.FileExists(currLogPath)) { StreamReader file = new StreamReader(currLogPath); string line; while ((line = file.ReadLine()) != null) { if (line.Contains("FOUND") || line.Contains("Infected files") || line.Contains("Scanned files")) { //logType "Infected" means this log line represents details of infected files String logType = "Infected"; if (line.Contains("Infected files") || line.Contains("Scanned files")) { //logType "Info" means this log line represents total number of scanned or infected files logType = "Info"; } FileSystemHelpers.AppendAllTextToFile(aggrLogPath, DateTime.UtcNow.ToString(@"M/d/yyyy hh:mm:ss tt") + "," + id + "," + logType + "," + host + "," + line + '\n'); } } } return(successfullyScanned.Result ? ScanRequestResult.RunningAynschronously : ScanRequestResult.AsyncScanFailed); } else { //Timeout went off before scan task completion //Cancel scan task timeoutCancellationTokenSource.Cancel(); //Scan process will be cancelled //wait till scan status file is appropriately updated await successfullyScanned; //Delete excess scan folders, just keep the maximum number allowed await DeletePastScans(mainScanDirPath, _tracer); return(ScanRequestResult.AsyncScanFailed); } } } }
private void Log(string logEntry) { FileSystemHelpers.AppendAllTextToFile(_path, string.Concat(logEntry, System.Environment.NewLine)); }