/// <summary> /// Appends an <see cref="object"/> to this instance in /// a formatted way with the specified <see cref="LoggingFlags"/>. /// </summary> /// <param name="obj">Object to append.</param> /// <param name="flags"><see cref="LoggingFlags"/> to instruct the <see cref="LogBuilder"/>.</param> /// <returns>A reference to this instance.</returns> /// <exception cref="NotSupportedException"></exception> public LogBuilder AppendObject(object obj, LoggingFlags flags) { if (obj == null) { throw new NotSupportedException("Does not support logging of 'null' root objects yet."); } var dump = DumpObject(obj, flags); m_StringBuilder.Append(dump); m_BlockString += dump; //var debug = m_StringBuilder.ToString(); return(this); }
public static void WriteProfile(ProfileData profile) { using (Profile driverProfile = new Profile()) { driverProfile.DeviceType = "Telescope"; driverProfile.WriteValue(driverID, traceStateProfileName, profile.TraceState.ToString()); driverProfile.WriteValue(driverID, traceFlagsProfileName, profile.TraceFlags.ToString()); driverProfile.WriteValue(driverID, comPortProfileName, profile.ComPort.ToString()); driverProfile.WriteValue(driverID, baudRateProfileName, profile.BaudRate.ToString()); driverProfile.WriteValue(driverID, latitudeProfileName, profile.Latitude.ToString()); driverProfile.WriteValue(driverID, longitudeProfileName, profile.Longitude.ToString()); driverProfile.WriteValue(driverID, elevationProfileName, profile.Elevation.ToString()); currentLogFlags = profile.TraceFlags; } }
public void SetFlags(LoggingFlags flags) { _logger.SetFlag(flags); }
private string DumpObject(object obj, LoggingFlags flags) { var objType = obj.GetType(); var objFields = objType.GetFields(); var objProperties = objType.GetProperties(); var objLog = string.Empty; if (flags.HasFlag(LoggingFlags.Properties)) { //TODO: Remove this dirty code repetition. for (int i = 0; i < objProperties.Length; i++) { var property = objProperties[i]; var propertyName = property.Name; if (!flags.HasFlag(LoggingFlags.Unknowns)) // check if we loggin unknowns { if (propertyName.StartsWith("Unknown")) { continue; } } var propertyValue = property.GetValue(obj); propertyValue = DumpObjectValue(propertyValue); objLog += m_CurrentIndent + propertyName + ": " + propertyValue; if (i != objProperties.Length - 1) { objLog += "\r\n"; } } } if (flags.HasFlag(LoggingFlags.Fields)) { //TODO: Remove this dirty code repetition. for (int i = 0; i < objFields.Length; i++) { var field = objFields[i]; var fieldName = field.Name; if (!flags.HasFlag(LoggingFlags.Unknowns)) // check if we loggin unknowns { if (fieldName.StartsWith("Unknown")) { continue; } } var fieldValue = field.GetValue(obj); fieldValue = DumpObjectValue(fieldValue); objLog += m_CurrentIndent + fieldName + ": " + fieldValue; if (i != objFields.Length - 1) { objLog += "\r\n"; } } } //var debug = m_StringBuilder.ToString(); return(objLog); }
public void SetFlag(LoggingFlags flag) { _flags = flag; }
/// <summary> /// Initializes a new instance of the <see cref="LoggableAttribute"/> /// class with the specified <see cref="LoggingFlags"/>. /// </summary> /// <param name="flags"></param> public LoggableAttribute(LoggingFlags flags) { Flags = flags; }
private static int Attach(int processId, FileInfo[] configs, LoggingFlags logLevel, LoggingFlags logFileLevel, FileSystemInfo?logFilePath) { #region Target Process Process process; try { process = Process.GetProcessById(processId); } catch (Exception ex) { WriteError(ex.Message); return(ExitCodeFailure); } #endregion #region Configuration Sources var sourceInfos = new List <ConfigurationSourceInfo>(); // Schema file embedded name is the same that the full type name that was generated from the schema file string schemaResourceName = typeof(InstrumentationConfigurationSources).FullName + ".xsd"; foreach (var configFile in configs) { var sourceInfo = new ConfigurationSourceInfo() { ConfigSourceFilePath = configFile.FullName }; // Attempt to resolve fullpath string?configFileDirectory = Path.GetDirectoryName(sourceInfo.ConfigSourceFilePath); if (null == configFileDirectory || !Directory.Exists(configFileDirectory)) { WriteError(Invariant($"Could not find directory '{configFileDirectory}'.")); return(ExitCodeFailure); } sourceInfo.ConfigSourceDirectory = configFileDirectory; #region Parse Configuration Sources // Read schema file using (Stream? schemaStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(schemaResourceName)) using (XmlReader schemaReader = XmlReader.Create(schemaStream, new XmlReaderSettings() { XmlResolver = null, DtdProcessing = DtdProcessing.Prohibit })) { // Create reader settings that conducts validation XmlReaderSettings readerSettings = new XmlReaderSettings() { XmlResolver = null, DtdProcessing = DtdProcessing.Prohibit }; readerSettings.Schemas.Add(string.Empty, schemaReader); readerSettings.ValidationType = ValidationType.Schema; // Read configuration source file using (XmlReader reader = XmlReader.Create(sourceInfo.ConfigSourceFilePath, readerSettings)) { XmlSerializer serializer = new XmlSerializer(typeof(InstrumentationConfigurationSources)); try { sourceInfo.Sources = (InstrumentationConfigurationSources)serializer.Deserialize(reader); } // InvalidOperationException is thrown when XML does not conform to the schema catch (InvalidOperationException ex) { // Log the location of the error WriteError($"Error: {ex.Message}"); if (null != ex.InnerException) { // Log the detailed information of why the XML does not conform to the schema WriteError(ex.InnerException.Message); } return(ExitCodeFailure); } } } #endregion sourceInfos.Add(sourceInfo); } #endregion #region Engine RootPath // This executable should be in the [Root]\Tools\Attach directory. Get the root directory // and build back to the instrumentation engine file path. // CONSIDER: Is there a better way to get the engine path instead of building the path // relative to the current executable? For example, requiring the root path of the engine // to be passed as a parameter (which increases the difficulty of using this executable). string?rootDirectory = Environment.GetEnvironmentVariable("MicrosoftInstrumentationEngine_InstallationRoot"); if (string.IsNullOrEmpty(rootDirectory)) { string?attachDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); if (null == attachDirectory || !Directory.Exists(attachDirectory)) { WriteError(Invariant($"Directory '{attachDirectory}' does not exist.")); return(ExitCodeFailure); } string?toolsDirectory = Path.GetDirectoryName(attachDirectory); if (null == toolsDirectory || !Directory.Exists(toolsDirectory)) { WriteError(Invariant($"Directory '{toolsDirectory}' does not exist.")); return(ExitCodeFailure); } rootDirectory = Path.GetDirectoryName(toolsDirectory); } else { rootDirectory = Environment.ExpandEnvironmentVariables(rootDirectory); } if (null == rootDirectory || !Directory.Exists(rootDirectory)) { WriteError(Invariant($"Directory '{rootDirectory}' does not exist.")); return(ExitCodeFailure); } #endregion #region Target Process Architecture bool isTargetProcess32Bit = true; if (RuntimeInformation.OSArchitecture == Architecture.X64) { isTargetProcess32Bit = false; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { try { // Returns true for 32-bit applications running on a 64-bit OS if (!NativeMethods.IsWow64Process(process.Handle, out isTargetProcess32Bit)) { Exception?ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); if (null != ex) { WriteError(Invariant($"Failed to check process bitness: {ex.Message}")); } else { WriteError(Invariant($"Failed to check process bitness.")); } return(ExitCodeFailure); } } catch (Win32Exception ex) { WriteError(Invariant($"Failed to check process bitness (code: {ex.NativeErrorCode}): {ex.Message}")); return(ExitCodeFailure); } } } #endregion #region Generate Engine Configuration // Create CLRIE configuration object for XML serialization var configuration = new InstrumentationEngineConfiguration(); configuration.InstrumentationEngine = GenerateEngineConfiguration( logLevel, logFileLevel, logFilePath); if (TryParseInstrumentationMethodConfiguration( sourceInfos, isTargetProcess32Bit ? ChipType.x86 : ChipType.x64, out InstrumentationMethodsTypeAddInstrumentationMethod[]? methods) &&
private string DumpObject(object obj, LoggingFlags flags) { var objType = obj.GetType(); var objFields = objType.GetFields(); var objProperties = objType.GetProperties(); var objLog = string.Empty; if (flags.HasFlag(LoggingFlags.Properties)) { //TODO: Remove this dirty code repetition. for (int i = 0; i < objProperties.Length; i++) { var property = objProperties[i]; var propertyName = property.Name; if (!flags.HasFlag(LoggingFlags.Unknowns)) // check if we loggin unknowns if (propertyName.StartsWith("Unknown")) continue; var propertyValue = property.GetValue(obj); propertyValue = DumpObjectValue(propertyValue); objLog += m_CurrentIndent + propertyName + ": " + propertyValue; if (i != objProperties.Length - 1) objLog += "\r\n"; } } if (flags.HasFlag(LoggingFlags.Fields)) { //TODO: Remove this dirty code repetition. for (int i = 0; i < objFields.Length; i++) { var field = objFields[i]; var fieldName = field.Name; if (!flags.HasFlag(LoggingFlags.Unknowns)) // check if we loggin unknowns if (fieldName.StartsWith("Unknown")) continue; var fieldValue = field.GetValue(obj); fieldValue = DumpObjectValue(fieldValue); objLog += m_CurrentIndent + fieldName + ": " + fieldValue; if (i != objFields.Length - 1) objLog += "\r\n"; } } //var debug = m_StringBuilder.ToString(); return objLog; }
/// <summary> /// Appends an <see cref="object"/> to this instance in /// a formatted way with the specified <see cref="LoggingFlags"/>. /// </summary> /// <param name="obj">Object to append.</param> /// <param name="flags"><see cref="LoggingFlags"/> to instruct the <see cref="LogBuilder"/>.</param> /// <returns>A reference to this instance.</returns> /// <exception cref="NotSupportedException"></exception> public LogBuilder AppendObject(object obj, LoggingFlags flags) { if (obj == null) throw new NotSupportedException("Does not support logging of 'null' root objects yet."); var dump = DumpObject(obj, flags); m_StringBuilder.Append(dump); m_BlockString += dump; //var debug = m_StringBuilder.ToString(); return this; }