/// <summary> /// Loads Natvis files from file. /// </summary> /// <param name="filePath">File path.</param> /// <param name="typeVisualizers">Type visualizers to initialize.</param> public void LoadFile(string filePath, ICollection <NatvisVisualizerScanner.FileInfo> typeVisualizers) { try { if (!_fileSystem.File.Exists(filePath)) { TraceWriteLine(NatvisLoggingLevel.ERROR, "Unable to load Natvis file because it doesn't exist." + $" '{filePath}'"); return; } TraceWriteLine(NatvisLoggingLevel.VERBOSE, $"Loading Natvis file '{filePath}'."); var sw = new Stopwatch(); sw.Start(); using (Stream stream = _fileSystem.FileStream.Create(filePath, FileMode.Open, FileAccess.Read)) { LoadFromStream(stream, filePath, typeVisualizers); } NatvisValidator validator = _validatorFactory.Create(); validator.Validate(filePath); sw.Stop(); TraceWriteLine(NatvisLoggingLevel.VERBOSE, $"Loaded Natvis file '{filePath}' in {sw.Elapsed}."); } catch (InvalidOperationException ex) { // Handles invalid XML errors. // don't allow natvis failures to stop debugging TraceWriteLine(NatvisLoggingLevel.ERROR, $"Failed to load Natvis file '{filePath}'." + $" Reason: {ex.Message}"); } catch (Exception ex) { // TODO: Ensure 'unhandled' exceptions are logged at a higher level, such // as a global error handler. TraceWriteLine(NatvisLoggingLevel.ERROR, $"Failed to load Natvis file '{filePath}'. Reason: {ex.Message}" + $"{Environment.NewLine}Stacktrace:{ex.StackTrace}"); throw; } }
/// <summary> /// Loads Natvis files from string. This method is used in tests. /// </summary> /// <param name="natvisText">String with Natvis specification.</param> /// <param name="typeVisualizers">Type visualizers to initialize.</param> public void LoadFromString(string natvisText, ICollection <NatvisVisualizerScanner.FileInfo> typeVisualizers) { try { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(natvisText))) { LoadFromStream(stream, "<From String>", typeVisualizers); } using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(natvisText))) { NatvisValidator validator = _validatorFactory.Create(); validator.Validate(stream); } } catch (InvalidOperationException ex) { // Handles invalid XML errors. // Don't allow natvis failures to stop debugging. var reason = ex.InnerException != null ? $"{ex.Message}: {ex.InnerException.Message}" : $"{ex.Message}"; TraceWriteLine(NatvisLoggingLevel.ERROR, $"Failed to load Natvis text. Reason: {reason}" + $"{Environment.NewLine}Stacktrace:{ex.StackTrace}"); } catch (Exception ex) { // TODO: Ensure 'unhandled' exceptions are logged at a higher level, such // as a global error handler. _logger.Error( $"Failed to load Natvis text. Reason: {ex.Message}" + $"{Environment.NewLine}Text:{Environment.NewLine}{natvisText}" + $"{Environment.NewLine}Stacktrace:{Environment.NewLine}{ex.StackTrace}"); throw; } }