Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions src/System.Management.Automation/engine/InformationRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Dbg = System.Management.Automation.Diagnostics;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Management.Automation.Language;

#if CORECLR
using Environment = System.Management.Automation.Environment;
Expand Down Expand Up @@ -41,15 +42,6 @@ public InformationRecord(Object messageData, string source)
this.Source = source;

this.TimeGenerated = DateTime.Now;
this.Tags = new List<string>();
// domain\user on Windows, just user on Unix
#if UNIX
this.User = Platform.Unix.UserName;
#else
this.User = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
#endif
this.Computer = PsUtils.GetHostName();
this.ProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
this.NativeThreadId = PsUtils.GetNativeThreadId();
this.ManagedThreadId = (uint)System.Threading.Thread.CurrentThread.ManagedThreadId;
}
Expand Down Expand Up @@ -104,25 +96,64 @@ internal InformationRecord(InformationRecord baseRecord)
/// </summary>
[DataMember]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public List<string> Tags { get; internal set; }
public List<string> Tags
{
get { return _tags ?? (_tags = new List<string>()); }
internal set { _tags = value; }
}
private List<string> _tags;

/// <summary>
/// The user that generated this informational record
/// </summary>
[DataMember]
public string User { get; set; }
public string User
{
get
{
if (this._user == null)
{
// domain\user on Windows, just user on Unix
#if UNIX
this._user = Platform.Unix.UserName;
#else
this._user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
#endif
}
return _user;
}
set { _user = value; }
}
private string _user;

/// <summary>
/// The computer that generated this informational record
/// </summary>
[DataMember]
public string Computer { get; set; }
public string Computer
{
get { return this._computerName ?? (this._computerName = PsUtils.GetHostName()); }
set { this._computerName = value; }
}
private string _computerName;

/// <summary>
/// The process that generated this informational record
/// </summary>
[DataMember]
public uint ProcessId { get; set; }
public uint ProcessId
{
get
{
if (!this._processId.HasValue)
{
this._processId = (uint) System.Diagnostics.Process.GetCurrentProcess().Id;
}
return this._processId.Value;
}
set { _processId = value; }
}
private uint? _processId;

/// <summary>
/// The native thread that generated this informational record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ Describe "Stream writer tests" -Tags "CI" {
$result = Write-Information "Test Message" *>&1
$result.GetType().Fullname | Should be "System.Management.Automation.InformationRecord"
$result.NativeThreadId | Should Not Be 0
$result.ProcessId | Should Be $pid
if ($IsWindows)
{
# Use Match instead of Be so we can avoid dealing with a potential domain name
$result.Computer | Should Match ".*${env:COMPUTERNAME}"
$result.User | Should Match ".*${env:USERNAME}"
}
else
{
$result.Computer | Should Be $(uname -n)
$result.User | Should Be $(whoami)
}
"$result" | Should be "Test Message"
}
}
Expand Down