internal SessionSummary(SessionSummaryPacket packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } m_Packet = packet; m_SessionStatus = SessionStatus.Unknown; //it should be set for us in a minute... }
/// <summary> /// Create a new session summary as the live collection session for the current process /// </summary> /// <remarks>This constructor figures out all of the summary information when invoked, which can take a moment.</remarks> internal SessionSummary(AgentConfiguration configuration) { m_IsLive = true; m_Packet = new SessionSummaryPacket(); m_SessionStatus = SessionStatus.Running; m_PrivacyEnabled = configuration.Publisher.EnableAnonymousMode; try { m_Packet.ID = Guid.NewGuid(); m_Packet.Caption = null; //this stuff all tends to succeed if (m_PrivacyEnabled) { m_Packet.UserName = string.Empty; m_Packet.UserDomainName = string.Empty; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { m_Packet.UserName = System.Environment.GetEnvironmentVariable("USERNAME") ?? string.Empty; m_Packet.UserDomainName = System.Environment.GetEnvironmentVariable("USERDOMAIN") ?? string.Empty; } else { m_Packet.UserName = System.Environment.GetEnvironmentVariable("USER") ?? string.Empty; m_Packet.UserDomainName = System.Environment.GetEnvironmentVariable("HOSTNAME") ?? string.Empty; } m_Packet.TimeZoneCaption = TimeZoneInfo.Local.StandardName; m_Packet.EndDateTime = StartDateTime; //we want to ALWAYS have an end time, and since we just created our start time we need to move that over to end time //this stuff, on the other hand, doesn't always succeed //Lets see if the user has already picked some things for us... PublisherConfiguration publisherConfig = configuration.Publisher; string productName = null, applicationName = null, applicationDescription = null; Version applicationVersion = null; //what kind of process are we? if (publisherConfig.ApplicationType != ApplicationType.Unknown) { // They specified an application type, so just use that. m_AgentAppType = publisherConfig.ApplicationType; // Start with the type they specified. } m_Packet.ApplicationType = m_AgentAppType; // Finally, set the application type from our determined type. if (m_AgentAppType != ApplicationType.AspNet) { //we want to find our entry assembly and get default product/app info from it. GetApplicationNameSafe(out productName, out applicationName, out applicationVersion, out applicationDescription); } //OK, now apply configuration overrides or what we discovered... m_Packet.ProductName = string.IsNullOrEmpty(publisherConfig.ProductName) ? productName : publisherConfig.ProductName; m_Packet.ApplicationName = string.IsNullOrEmpty(publisherConfig.ApplicationName) ? applicationName : publisherConfig.ApplicationName; m_Packet.ApplicationVersion = publisherConfig.ApplicationVersion ?? applicationVersion; m_Packet.ApplicationDescription = string.IsNullOrEmpty(publisherConfig.ApplicationDescription) ? applicationDescription : publisherConfig.ApplicationDescription; m_Packet.EnvironmentName = publisherConfig.EnvironmentName; m_Packet.PromotionLevelName = publisherConfig.PromotionLevelName; //Finally, no nulls allowed! Fix any... m_Packet.ProductName = string.IsNullOrEmpty(m_Packet.ProductName) ? "Unknown" : m_Packet.ProductName; m_Packet.ApplicationName = string.IsNullOrEmpty(m_Packet.ApplicationName) ? "Unknown" : m_Packet.ApplicationName; m_Packet.ApplicationVersion = m_Packet.ApplicationVersion ?? new Version(0, 0); m_Packet.ApplicationDescription = m_Packet.ApplicationDescription ?? string.Empty; m_Packet.EnvironmentName = m_Packet.EnvironmentName ?? string.Empty; m_Packet.PromotionLevelName = m_Packet.PromotionLevelName ?? string.Empty; m_Packet.ComputerId = GetComputerIdSafe(m_Packet.ProductName, configuration); m_Packet.AgentVersion = GetAgentVersionSafe(); } catch (Exception ex) { //we really don't want an init error to fail us, not here! GC.KeepAlive(ex); } if (m_PrivacyEnabled == false) { try { IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); m_Packet.HostName = ipGlobalProperties.HostName; m_Packet.DnsDomainName = ipGlobalProperties.DomainName ?? string.Empty; } catch { //fallback to environment names try { m_Packet.HostName = System.Environment.MachineName; } catch (Exception ex) { //we really don't want an init error to fail us, not here! GC.KeepAlive(ex); m_Packet.HostName = "unknown"; } m_Packet.DnsDomainName = string.Empty; } } else { // Privacy mode. Don't store "personally-identifying information". m_Packet.HostName = "anonymous"; m_Packet.DnsDomainName = string.Empty; } var os = System.Environment.OSVersion; m_Packet.OSPlatformCode = (int)os.Platform; //we copied this enum for our value. m_Packet.OSVersion = os.Version; m_Packet.OSServicePack = os.ServicePack; try { m_Packet.OSArchitecture = RuntimeInformation.OSArchitecture.ToProcessorArchitecture(); m_Packet.RuntimeArchitecture = RuntimeInformation.ProcessArchitecture.ToProcessorArchitecture(); m_Packet.OSCultureName = CultureInfo.CurrentUICulture.ToString(); m_Packet.CurrentCultureName = CultureInfo.CurrentCulture.ToString(); m_Packet.CurrentUICultureName = CultureInfo.CurrentUICulture.ToString(); m_Packet.OSBootMode = OSBootMode.Normal; m_Packet.Processors = System.Environment.ProcessorCount; m_Packet.ProcessorCores = m_Packet.Processors; //BUG try { var frameworkDescription = RuntimeInformation.FrameworkDescription; //hopefully this has a version number in it... //Thanks to SO.. https://stackoverflow.com/questions/6618868/regular-expression-for-version-numbers var versionMatch = Regex.Match(frameworkDescription, @"\d+(?:\.\d+)+"); if (versionMatch.Success) { if (Version.TryParse(versionMatch.Value, out var version)) { m_Packet.RuntimeVersion = version; } else { m_Packet.RuntimeVersion = new Version(0, 0); } } } catch (Exception ex) { GC.KeepAlive(ex); m_Packet.RuntimeVersion = new Version(0, 0); } m_Packet.MemoryMB = 0; // BUG m_Packet.UserInteractive = System.Environment.UserInteractive; //find the active screen resolution if (m_AgentAppType == ApplicationType.Windows) { //We don't know if we can reliably get these on .NET Core. m_Packet.TerminalServer = false; m_Packet.ColorDepth = 0; m_Packet.ScreenHeight = 0; m_Packet.ScreenWidth = 0; } if (m_PrivacyEnabled) { m_Packet.CommandLine = string.Empty; } else { //.NET Core doesn't expose the command line because of concerns over how it is handled cross-platform. var commandArgs = System.Environment.GetCommandLineArgs(); m_Packet.CommandLine = string.Join(" ", commandArgs); //the first arg is the executable name } } catch (Exception ex) { //we really don't want an init error to fail us, not here! GC.KeepAlive(ex); } //now do user defined properties try { foreach (KeyValuePair <string, string> keyValuePair in configuration.Properties) { m_Packet.Properties.Add(keyValuePair.Key, keyValuePair.Value); } } catch (Exception ex) { //we aren't expecting any errors, but best be safe. GC.KeepAlive(ex); } m_Packet.Caption = m_Packet.ApplicationName; }