From ae9a8a80c0a8396d290eddaf6a5c06e32f91b3b6 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Wed, 16 Dec 2020 17:03:14 +0000 Subject: [PATCH 1/3] Fix IDE0032: UseAutoProperty part 3 `src\Microsoft.PowerShell.Commands.Diagnostics\` https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0032 --- .../CounterSample.cs | 154 +++------------ .../CounterSet.cs | 82 +++----- .../GetCounterCommand.cs | 39 +--- .../GetEventCommand.cs | 176 +++++------------- .../NewWinEventCommand.cs | 56 ++---- 5 files changed, 119 insertions(+), 388 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs index 0717be5ff33..14560794a5a 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs @@ -27,127 +27,43 @@ internal PerformanceCounterSample(string path, UInt64 timeStamp100nSec, UInt32 status) { - _path = path; - _instanceName = instanceName; - _cookedValue = cookedValue; - _rawValue = rawValue; - _secondValue = secondValue; - _multiCount = multiCount; - _counterType = counterType; - _defaultScale = defaultScale; - _timeBase = timeBase; - _timeStamp = timeStamp; - _timeStamp100nSec = timeStamp100nSec; - _status = status; + Path = path; + InstanceName = instanceName; + CookedValue = cookedValue; + RawValue = rawValue; + SecondValue = secondValue; + MultipleCount = multiCount; + CounterType = counterType; + DefaultScale = defaultScale; + TimeBase = timeBase; + Timestamp = timeStamp; + Timestamp100NSec = timeStamp100nSec; + Status = status; } - public string Path - { - get { return _path; } - - set { _path = value; } - } - - private string _path = string.Empty; - - public string InstanceName - { - get { return _instanceName; } - - set { _instanceName = value; } - } - - private string _instanceName = string.Empty; - - public double CookedValue - { - get { return _cookedValue; } - - set { _cookedValue = value; } - } - - private double _cookedValue = 0; - - public UInt64 RawValue - { - get { return _rawValue; } - - set { _rawValue = value; } - } - - private UInt64 _rawValue = 0; - - public UInt64 SecondValue - { - get { return _secondValue; } - - set { _secondValue = value; } - } - - private UInt64 _secondValue = 0; - - public uint MultipleCount - { - get { return _multiCount; } - - set { _multiCount = value; } - } + public string Path { get; set; } = string.Empty; - private uint _multiCount = 0; + public string InstanceName { get; set; } = string.Empty; - public PerformanceCounterType CounterType - { - get { return _counterType; } - - set { _counterType = value; } - } - - private PerformanceCounterType _counterType = 0; - - public DateTime Timestamp - { - get { return _timeStamp; } - - set { _timeStamp = value; } - } - - private DateTime _timeStamp = DateTime.MinValue; - - public UInt64 Timestamp100NSec - { - get { return _timeStamp100nSec; } - - set { _timeStamp100nSec = value; } - } - - private UInt64 _timeStamp100nSec = 0; + public double CookedValue { get; set; } = 0; - public UInt32 Status - { - get { return _status; } + public UInt64 RawValue { get; set; } = 0; - set { _status = value; } - } + public UInt64 SecondValue { get; set; } = 0; - private UInt32 _status = 0; + public uint MultipleCount { get; set; } = 0; - public UInt32 DefaultScale - { - get { return _defaultScale; } + public PerformanceCounterType CounterType { get; set; } = 0; - set { _defaultScale = value; } - } + public DateTime Timestamp { get; set; } = DateTime.MinValue; - private UInt32 _defaultScale = 0; + public UInt64 Timestamp100NSec { get; set; } = 0; - public UInt64 TimeBase - { - get { return _timeBase; } + public UInt32 Status { get; set; } = 0; - set { _timeBase = value; } - } + public UInt32 DefaultScale { get; set; } = 0; - private UInt64 _timeBase = 0; + public UInt64 TimeBase { get; set; } = 0; } public class PerformanceCounterSampleSet @@ -161,31 +77,17 @@ internal PerformanceCounterSampleSet(DateTime timeStamp, PerformanceCounterSample[] counterSamples, bool firstSet) : this() { - _timeStamp = timeStamp; - _counterSamples = counterSamples; - } - - public DateTime Timestamp - { - get { return _timeStamp; } - - set { _timeStamp = value; } + Timestamp = timeStamp; + CounterSamples = counterSamples; } - private DateTime _timeStamp = DateTime.MinValue; + public DateTime Timestamp { get; set; } = DateTime.MinValue; [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample.CounterSamples", Justification = "A string[] is required here because that is the type Powershell supports")] - public PerformanceCounterSample[] CounterSamples - { - get { return _counterSamples; } - - set { _counterSamples = value; } - } - - private PerformanceCounterSample[] _counterSamples = null; + public PerformanceCounterSample[] CounterSamples { get; set; } = null; private readonly ResourceManager _resourceMgr = null; } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSet.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSet.cs index 2eb6b1dd7ce..dfc175b604c 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSet.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSet.cs @@ -16,74 +16,34 @@ internal CounterSet(string setName, string setHelp, ref Dictionary counterInstanceMapping) { - _counterSetName = setName; + CounterSetName = setName; if (machineName == null || machineName.Length == 0) { machineName = "."; } else { - _machineName = machineName; - if (!_machineName.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)) + MachineName = machineName; + if (!MachineName.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase)) { - _machineName = @"\\" + _machineName; + MachineName = @"\\" + MachineName; } } - _counterSetType = categoryType; - _description = setHelp; - _counterInstanceMapping = counterInstanceMapping; + CounterSetType = categoryType; + Description = setHelp; + CounterInstanceMapping = counterInstanceMapping; } - public string CounterSetName - { - get - { - return _counterSetName; - } - } - - private readonly string _counterSetName = string.Empty; - - public string MachineName - { - get - { - return _machineName; - } - } - - private readonly string _machineName = "."; - - public PerformanceCounterCategoryType CounterSetType - { - get - { - return _counterSetType; - } - } + public string CounterSetName { get; } = string.Empty; - private readonly PerformanceCounterCategoryType _counterSetType; + public string MachineName { get; } = "."; - public string Description - { - get - { - return _description; - } - } - - private readonly string _description = string.Empty; + public PerformanceCounterCategoryType CounterSetType { get; } - internal Dictionary CounterInstanceMapping - { - get - { - return _counterInstanceMapping; - } - } + public string Description { get; } = string.Empty; - private readonly Dictionary _counterInstanceMapping; + internal Dictionary CounterInstanceMapping { get; } public StringCollection Paths { @@ -95,15 +55,15 @@ public StringCollection Paths string path; if (CounterInstanceMapping[counterName].Length != 0) { - path = (_machineName == ".") ? - ("\\" + _counterSetName + "(*)\\" + counterName) : - (_machineName + "\\" + _counterSetName + "(*)\\" + counterName); + path = (MachineName == ".") ? + ("\\" + CounterSetName + "(*)\\" + counterName) : + (MachineName + "\\" + CounterSetName + "(*)\\" + counterName); } else { - path = (_machineName == ".") ? - ("\\" + _counterSetName + "\\" + counterName) : - (_machineName + "\\" + _counterSetName + "\\" + counterName); + path = (MachineName == ".") ? + ("\\" + CounterSetName + "\\" + counterName) : + (MachineName + "\\" + CounterSetName + "\\" + counterName); } retColl.Add(path); @@ -122,9 +82,9 @@ public StringCollection PathsWithInstances { foreach (string instanceName in CounterInstanceMapping[counterName]) { - string path = (_machineName == ".") ? - ("\\" + _counterSetName + "(" + instanceName + ")\\" + counterName) : - (_machineName + "\\" + _counterSetName + "(" + instanceName + ")\\" + counterName); + string path = (MachineName == ".") ? + ("\\" + CounterSetName + "(" + instanceName + ")\\" + counterName) : + (MachineName + "\\" + CounterSetName + "(" + instanceName + ")\\" + counterName); retColl.Add(path); } } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs index b3f98a9364e..4a775e14fde 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs @@ -38,14 +38,7 @@ public sealed class GetCounterCommand : PSCmdlet Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ListSet", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListSet - { - get { return _listSet; } - - set { _listSet = value; } - } - - private string[] _listSet = { "*" }; + public string[] ListSet { get; set; } = { "*" }; // // Counter parameter @@ -93,14 +86,7 @@ public string[] Counter ValueFromPipelineByPropertyName = false, HelpMessageBaseName = "GetEventResources")] [ValidateRange((int)1, int.MaxValue)] - public int SampleInterval - { - get { return _sampleInterval; } - - set { _sampleInterval = value; } - } - - private int _sampleInterval = 1; + public int SampleInterval { get; set; } = 1; // // MaxSamples parameter @@ -155,14 +141,7 @@ public SwitchParameter Continuous Scope = "member", Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ComputerName", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ComputerName - { - get { return _computerName; } - - set { _computerName = value; } - } - - private string[] _computerName = Array.Empty(); + public string[] ComputerName { get; set; } = Array.Empty(); private ResourceManager _resourceMgr = null; @@ -288,12 +267,12 @@ private void AccumulatePipelineCounters() // private void ProcessListSet() { - if (_computerName.Length == 0) + if (ComputerName.Length == 0) { ProcessListSetPerMachine(null); } else - foreach (string machine in _computerName) + foreach (string machine in ComputerName) { ProcessListSetPerMachine(machine); } @@ -322,7 +301,7 @@ private void ProcessListSetPerMachine(string machine) _cultureAndSpecialCharacterMap.TryGetValue(culture.Name, out characterReplacementList); - foreach (string pattern in _listSet) + foreach (string pattern in ListSet) { bool bMatched = false; string normalizedPattern = pattern; @@ -550,7 +529,7 @@ private void ProcessGetCounter() break; } - bool cancelled = _cancelEventArrived.WaitOne((int)_sampleInterval * 1000, true); + bool cancelled = _cancelEventArrived.WaitOne((int)SampleInterval * 1000, true); if (cancelled) { break; @@ -587,7 +566,7 @@ private List CombineMachinesAndCounterPaths() { List retColl = new(); - if (_computerName.Length == 0) + if (ComputerName.Length == 0) { retColl.AddRange(_accumulatedCounters); return retColl; @@ -601,7 +580,7 @@ private List CombineMachinesAndCounterPaths() } else { - foreach (string machine in _computerName) + foreach (string machine in ComputerName) { if (machine.StartsWith("\\\\", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs index ae362bcfb0e..b0ab5dd6a91 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs @@ -43,14 +43,7 @@ public sealed class GetWinEventCommand : PSCmdlet Target = "Microsoft.PowerShell.Commands.GetEvent.ListLog", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListLog - { - get { return _listLog; } - - set { _listLog = value; } - } - - private string[] _listLog = { "*" }; + public string[] ListLog { get; set; } = { "*" }; /// /// GetLog parameter. @@ -66,14 +59,7 @@ public string[] ListLog Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.LogName", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] LogName - { - get { return _logName; } - - set { _logName = value; } - } - - private string[] _logName = { "*" }; + public string[] LogName { get; set; } = { "*" }; /// /// ListProvider parameter. @@ -93,14 +79,7 @@ public string[] LogName Target = "Microsoft.PowerShell.Commands.GetEvent.ListProvider", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListProvider - { - get { return _listProvider; } - - set { _listProvider = value; } - } - - private string[] _listProvider = { "*" }; + public string[] ListProvider { get; set; } = { "*" }; /// /// ProviderName parameter. @@ -118,14 +97,7 @@ public string[] ListProvider Target = "Microsoft.PowerShell.Commands.GetEvent.ProviderName", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ProviderName - { - get { return _providerName; } - - set { _providerName = value; } - } - - private string[] _providerName; + public string[] ProviderName { get; set; } /// /// Path parameter. @@ -143,14 +115,7 @@ public string[] ProviderName Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.Path", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] Path - { - get { return _path; } - - set { _path = value; } - } - - private string[] _path; + public string[] Path { get; set; } /// /// MaxEvents parameter. @@ -186,14 +151,7 @@ public string[] Path HelpMessageBaseName = "GetEventResources", HelpMessageResourceId = "MaxEventsParamHelp")] [ValidateRange((Int64)1, Int64.MaxValue)] - public Int64 MaxEvents - { - get { return _maxEvents; } - - set { _maxEvents = value; } - } - - private Int64 _maxEvents = -1; + public Int64 MaxEvents { get; set; } = -1; /// /// ComputerName parameter. @@ -225,14 +183,7 @@ public Int64 MaxEvents [ValidateNotNull] [Alias("Cn")] - public string ComputerName - { - get { return _computerName; } - - set { _computerName = value; } - } - - private string _computerName = string.Empty; + public string ComputerName { get; set; } = string.Empty; /// /// Credential parameter. @@ -245,14 +196,7 @@ public string ComputerName [Parameter(ParameterSetName = "XmlQuerySet")] [Parameter(ParameterSetName = "FileSet")] [Credential] - public PSCredential Credential - { - get { return _credential; } - - set { _credential = value; } - } - - private PSCredential _credential = PSCredential.Empty; + public PSCredential Credential { get; set; } = PSCredential.Empty; /// /// FilterXPath parameter. @@ -273,14 +217,7 @@ public PSCredential Credential ValueFromPipelineByPropertyName = false, HelpMessageBaseName = "GetEventResources")] [ValidateNotNull] - public string FilterXPath - { - get { return _filter; } - - set { _filter = value; } - } - - private string _filter = "*"; + public string FilterXPath { get; set; } = "*"; /// /// FilterXml parameter. @@ -298,14 +235,7 @@ public string FilterXPath Target = "Microsoft.PowerShell.Commands.GetEvent.FilterXml", Justification = "An XmlDocument is required here because that is the type Powershell supports")] - public XmlDocument FilterXml - { - get { return _xmlQuery; } - - set { _xmlQuery = value; } - } - - private XmlDocument _xmlQuery = null; + public XmlDocument FilterXml { get; set; } = null; /// /// FilterHashtable parameter. @@ -323,14 +253,7 @@ public XmlDocument FilterXml Target = "Microsoft.PowerShell.Commands.GetEvent.FilterHashtable", Justification = "A string[] is required here because that is the type Powershell supports")] - public Hashtable[] FilterHashtable - { - get { return _selector; } - - set { _selector = value; } - } - - private Hashtable[] _selector; + public Hashtable[] FilterHashtable { get; set; } /// /// Force switch. @@ -340,14 +263,7 @@ public Hashtable[] FilterHashtable [Parameter(ParameterSetName = "GetLogSet")] [Parameter(ParameterSetName = "HashQuerySet")] - public SwitchParameter Force - { - get { return _force; } - - set { _force = value; } - } - - private SwitchParameter _force; + public SwitchParameter Force { get; set; } /// /// Oldest switch. @@ -501,7 +417,7 @@ protected override void ProcessRecord() // private void AccumulatePipelineLogNames() { - _accumulatedLogNames.AddRange(_logName); + _accumulatedLogNames.AddRange(LogName); } // @@ -510,7 +426,7 @@ private void AccumulatePipelineLogNames() // private void AccumulatePipelineProviderNames() { - _accumulatedProviderNames.AddRange(_logName); + _accumulatedProviderNames.AddRange(LogName); } // @@ -519,7 +435,7 @@ private void AccumulatePipelineProviderNames() // private void AccumulatePipelineFileNames() { - _accumulatedFileNames.AddRange(_logName); + _accumulatedFileNames.AddRange(LogName); } // @@ -544,7 +460,7 @@ private void ProcessGetLog() } else { - logQuery = new EventLogQuery(_logNamesMatchingWildcard[0], PathType.LogName, _filter); + logQuery = new EventLogQuery(_logNamesMatchingWildcard[0], PathType.LogName, FilterXPath); } logQuery.Session = eventLogSession; @@ -561,7 +477,7 @@ private void ProcessGetProvider() { using (EventLogSession eventLogSession = CreateSession()) { - FindProvidersByLogForWildcardPatterns(eventLogSession, _providerName); + FindProvidersByLogForWildcardPatterns(eventLogSession, ProviderName); if (_providersByLogMap.Count == 0) { @@ -604,7 +520,7 @@ private void ProcessListLog() { using (EventLogSession eventLogSession = CreateSession()) { - foreach (string logPattern in _listLog) + foreach (string logPattern in ListLog) { bool bMatchFound = false; WildcardPattern wildLogPattern = new(logPattern, WildcardOptions.IgnoreCase); @@ -660,7 +576,7 @@ private void ProcessListLog() if (!bMatchFound) { string msg = _resourceMgr.GetString("NoMatchingLogsFound"); - Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, _computerName, logPattern)); + Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, ComputerName, logPattern)); WriteError(new ErrorRecord(exc, "NoMatchingLogsFound", ErrorCategory.ObjectNotFound, null)); } } @@ -674,7 +590,7 @@ private void ProcessListProvider() { using (EventLogSession eventLogSession = CreateSession()) { - foreach (string provPattern in _listProvider) + foreach (string provPattern in ListProvider) { bool bMatchFound = false; WildcardPattern wildProvPattern = new(provPattern, WildcardOptions.IgnoreCase); @@ -707,7 +623,7 @@ private void ProcessListProvider() if (!bMatchFound) { string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("NoMatchingProvidersFound"), - _computerName, provPattern); + ComputerName, provPattern); Exception exc = new(msg); WriteError(new ErrorRecord(exc, "NoMatchingProvidersFound", ErrorCategory.ObjectNotFound, null)); } @@ -727,7 +643,7 @@ private void ProcessFilterXml() // // Do minimal parsing of xmlQuery to determine if any direct channels or ETL files are in it. // - XmlElement root = _xmlQuery.DocumentElement; + XmlElement root = FilterXml.DocumentElement; XmlNodeList queryNodes = root.SelectNodes("//Query//Select"); foreach (XmlNode queryNode in queryNodes) { @@ -749,7 +665,7 @@ private void ProcessFilterXml() } } - EventLogQuery logQuery = new(null, PathType.LogName, _xmlQuery.InnerXml); + EventLogQuery logQuery = new(null, PathType.LogName, FilterXml.InnerXml); logQuery.Session = eventLogSession; logQuery.ReverseDirection = !_oldest; @@ -768,9 +684,9 @@ private void ProcessFile() // At this point, _path array contains paths that might have wildcards, // environment variables or PS drives. Let's resolve those. // - for (int i = 0; i < _path.Length; i++) + for (int i = 0; i < Path.Length; i++) { - StringCollection resolvedPaths = ValidateAndResolveFilePath(_path[i]); + StringCollection resolvedPaths = ValidateAndResolveFilePath(Path[i]); foreach (string resolvedPath in resolvedPaths) { _resolvedPaths.Add(resolvedPath); @@ -791,7 +707,7 @@ private void ProcessFile() } else { - logQuery = new EventLogQuery(_resolvedPaths[0], PathType.FilePath, _filter); + logQuery = new EventLogQuery(_resolvedPaths[0], PathType.FilePath, FilterXPath); } logQuery.Session = eventLogSession; @@ -833,30 +749,30 @@ private EventLogSession CreateSession() { EventLogSession eventLogSession = null; - if (_computerName == string.Empty) + if (ComputerName == string.Empty) { // Set _computerName to "localhost" for future error messages, // but do not use it for the connection to avoid RPC overhead. - _computerName = "localhost"; + ComputerName = "localhost"; - if (_credential == PSCredential.Empty) + if (Credential == PSCredential.Empty) { return new EventLogSession(); } } - else if (_credential == PSCredential.Empty) + else if (Credential == PSCredential.Empty) { - return new EventLogSession(_computerName); + return new EventLogSession(ComputerName); } // If we are here, either both computer name and credential were passed initially, // or credential only - we will use it with "localhost" - NetworkCredential netCred = (NetworkCredential)_credential; - eventLogSession = new EventLogSession(_computerName, + NetworkCredential netCred = (NetworkCredential)Credential; + eventLogSession = new EventLogSession(ComputerName, netCred.Domain, netCred.UserName, - _credential.Password, + Credential.Password, SessionAuthentication.Default ); // @@ -894,7 +810,7 @@ private void ReadEvents(EventLogQuery logQuery) break; } - if (_maxEvents != -1 && numEvents >= _maxEvents) + if (MaxEvents != -1 && numEvents >= MaxEvents) { break; } @@ -974,7 +890,7 @@ private string BuildStructuredQuery(EventLogSession eventLogSession) uint queryId = 0; foreach (string log in _logNamesMatchingWildcard) { - result.AppendFormat(CultureInfo.InvariantCulture, queryTemplate, new object[] { queryId++, log, _filter }); + result.AppendFormat(CultureInfo.InvariantCulture, queryTemplate, new object[] { queryId++, log, FilterXPath }); } result.Append(queryListClose); @@ -989,7 +905,7 @@ private string BuildStructuredQuery(EventLogSession eventLogSession) foreach (string filePath in _resolvedPaths) { string properFilePath = filePrefix + filePath; - result.AppendFormat(CultureInfo.InvariantCulture, queryTemplate, new object[] { queryId++, properFilePath, _filter }); + result.AppendFormat(CultureInfo.InvariantCulture, queryTemplate, new object[] { queryId++, properFilePath, FilterXPath }); } result.Append(queryListClose); @@ -1111,7 +1027,7 @@ private string BuildStructuredQueryFromHashTable(EventLogSession eventLogSession uint queryId = 0; - foreach (Hashtable hash in _selector) + foreach (Hashtable hash in FilterHashtable) { string xpathString = string.Empty; string xpathStringSuppress = string.Empty; @@ -1674,7 +1590,7 @@ private bool ValidateLogName(string logName, EventLogSession eventLogSession) catch (EventLogNotFoundException) { string msg = _resourceMgr.GetString("NoMatchingLogsFound"); - Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, _computerName, logName)); + Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, ComputerName, logName)); WriteError(new ErrorRecord(exc, "NoMatchingLogsFound", ErrorCategory.ObjectNotFound, logName)); return false; } @@ -1833,7 +1749,7 @@ private StringCollection ValidateAndResolveFilePath(string path) // private void CheckHashTablesForNullValues() { - foreach (Hashtable hash in _selector) + foreach (Hashtable hash in FilterHashtable) { foreach (string key in hash.Keys) { @@ -1875,13 +1791,13 @@ private string AddProviderPredicatesToFilter(StringCollection providers) { if (providers.Count == 0) { - return _filter; + return FilterXPath; } - string ret = _filter; + string ret = FilterXPath; string predicate = BuildProvidersPredicate(providers); - if (_filter.Equals("*", StringComparison.OrdinalIgnoreCase)) + if (FilterXPath.Equals("*", StringComparison.OrdinalIgnoreCase)) { ret += "[" + predicate + "]"; } @@ -1890,7 +1806,7 @@ private string AddProviderPredicatesToFilter(StringCollection providers) // // Extend the XPath provided in the _filter // - int lastPredClose = _filter.LastIndexOf(']'); + int lastPredClose = FilterXPath.LastIndexOf(']'); if (lastPredClose == -1) { ret += "[" + predicate + "]"; @@ -2113,7 +2029,7 @@ private void FindLogNamesMatchingWildcards(EventLogSession eventLogSession, IEnu if (!bMatched) { string msg = _resourceMgr.GetString("NoMatchingLogsFound"); - Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, _computerName, logPattern)); + Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, ComputerName, logPattern)); WriteError(new ErrorRecord(exc, "NoMatchingLogsFound", ErrorCategory.ObjectNotFound, logPattern)); } } @@ -2150,7 +2066,7 @@ private void FindProvidersByLogForWildcardPatterns(EventLogSession eventLogSessi if (!bMatched) { string msg = _resourceMgr.GetString("NoMatchingProvidersFound"); - Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, _computerName, provPattern)); + Exception exc = new(string.Format(CultureInfo.InvariantCulture, msg, ComputerName, provPattern)); WriteError(new ErrorRecord(exc, "NoMatchingProvidersFound", ErrorCategory.ObjectNotFound, provPattern)); } } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/NewWinEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/NewWinEventCommand.cs index 5fceca05b47..b3fb4bf0a79 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/NewWinEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/NewWinEventCommand.cs @@ -36,20 +36,7 @@ public sealed class NewWinEventCommand : PSCmdlet Position = 0, Mandatory = true, ParameterSetName = ParameterAttribute.AllParameterSets)] - public string ProviderName - { - get - { - return _providerName; - } - - set - { - _providerName = value; - } - } - - private string _providerName; + public string ProviderName { get; set; } /// /// Id (EventId defined in manifest file) @@ -109,20 +96,7 @@ public byte Version SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Target = "Microsoft.PowerShell.Commands", Justification = "A string[] is required here because that is the type Powershell supports")] - public object[] Payload - { - get - { - return _payload; - } - - set - { - _payload = value; - } - } - - private object[] _payload; + public object[] Payload { get; set; } /// /// BeginProcessing. @@ -137,7 +111,7 @@ protected override void BeginProcessing() private void LoadProvider() { - if (string.IsNullOrEmpty(_providerName)) + if (string.IsNullOrEmpty(ProviderName)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("ProviderNotSpecified")), "ProviderName"); } @@ -146,7 +120,7 @@ private void LoadProvider() { foreach (string providerName in session.GetProviderNames()) { - if (string.Equals(providerName, _providerName, StringComparison.OrdinalIgnoreCase)) + if (string.Equals(providerName, ProviderName, StringComparison.OrdinalIgnoreCase)) { try { @@ -165,7 +139,7 @@ private void LoadProvider() if (_providerMetadata == null) { - string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("NoProviderFound"), _providerName); + string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("NoProviderFound"), ProviderName); throw new ArgumentException(msg); } } @@ -188,7 +162,7 @@ private void LoadEventDescriptor() string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("IncorrectEventId"), _id, - _providerName); + ProviderName); throw new EventWriteException(msg); } @@ -216,7 +190,7 @@ private void LoadEventDescriptor() _resourceMgr.GetString("IncorrectEventVersion"), _version, _id, - _providerName); + ProviderName); throw new EventWriteException(msg); } @@ -226,7 +200,7 @@ private void LoadEventDescriptor() string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("VersionNotSpecified"), _id, - _providerName); + ProviderName); throw new EventWriteException(msg); } @@ -269,8 +243,8 @@ private bool VerifyTemplate(EventMetadata emd) } } - if ((_payload == null && definedParameterCount != 0) - || ((_payload != null) && _payload.Length != definedParameterCount)) + if ((Payload == null && definedParameterCount != 0) + || ((Payload != null) && Payload.Length != definedParameterCount)) { string warning = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("PayloadMismatch"), _id, emd.Template); WriteWarning(warning); @@ -317,17 +291,17 @@ protected override void ProcessRecord() { EventDescriptor ed = _eventDescriptor.Value; - if (_payload != null && _payload.Length > 0) + if (Payload != null && Payload.Length > 0) { - for (int i = 0; i < _payload.Length; i++) + for (int i = 0; i < Payload.Length; i++) { - if (_payload[i] == null) + if (Payload[i] == null) { - _payload[i] = string.Empty; + Payload[i] = string.Empty; } } - provider.WriteEvent(in ed, _payload); + provider.WriteEvent(in ed, Payload); } else { From 56c887de7b0df119b0066eb639435a587c2f6096 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Wed, 16 Dec 2020 17:08:49 +0000 Subject: [PATCH 2/3] Remove blank lines --- .../GetCounterCommand.cs | 1 - .../GetEventCommand.cs | 13 ------------- 2 files changed, 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs index 4a775e14fde..89afe3a3837 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetCounterCommand.cs @@ -37,7 +37,6 @@ public sealed class GetCounterCommand : PSCmdlet Scope = "member", Target = "Microsoft.PowerShell.Commands.GetCounterCommand.ListSet", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListSet { get; set; } = { "*" }; // diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs index b0ab5dd6a91..a61d25e1b8b 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs @@ -42,7 +42,6 @@ public sealed class GetWinEventCommand : PSCmdlet Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.ListLog", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListLog { get; set; } = { "*" }; /// @@ -73,12 +72,10 @@ public sealed class GetWinEventCommand : PSCmdlet HelpMessageBaseName = "GetEventResources", HelpMessageResourceId = "ListProviderParamHelp")] [AllowEmptyCollection] - [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.ListProvider", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ListProvider { get; set; } = { "*" }; /// @@ -91,12 +88,10 @@ public sealed class GetWinEventCommand : PSCmdlet ValueFromPipelineByPropertyName = true, HelpMessageBaseName = "GetEventResources", HelpMessageResourceId = "GetProviderParamHelp")] - [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.ProviderName", Justification = "A string[] is required here because that is the type Powershell supports")] - public string[] ProviderName { get; set; } /// @@ -109,7 +104,6 @@ public sealed class GetWinEventCommand : PSCmdlet ValueFromPipelineByPropertyName = true, HelpMessageBaseName = "GetEventResources", HelpMessageResourceId = "PathParamHelp")] - [Alias("PSPath")] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", @@ -180,7 +174,6 @@ public sealed class GetWinEventCommand : PSCmdlet ParameterSetName = "XmlQuerySet", HelpMessageBaseName = "GetEventResources", HelpMessageResourceId = "ComputerNameParamHelp")] - [ValidateNotNull] [Alias("Cn")] public string ComputerName { get; set; } = string.Empty; @@ -229,12 +222,10 @@ public sealed class GetWinEventCommand : PSCmdlet ValueFromPipelineByPropertyName = false, ParameterSetName = "XmlQuerySet", HelpMessageBaseName = "GetEventResources")] - [SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.FilterXml", Justification = "An XmlDocument is required here because that is the type Powershell supports")] - public XmlDocument FilterXml { get; set; } = null; /// @@ -247,12 +238,10 @@ public sealed class GetWinEventCommand : PSCmdlet ValueFromPipelineByPropertyName = false, ParameterSetName = "HashQuerySet", HelpMessageBaseName = "GetEventResources")] - [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.FilterHashtable", Justification = "A string[] is required here because that is the type Powershell supports")] - public Hashtable[] FilterHashtable { get; set; } /// @@ -261,7 +250,6 @@ public sealed class GetWinEventCommand : PSCmdlet [Parameter(ParameterSetName = "ListLogSet")] [Parameter(ParameterSetName = "GetProviderSet")] [Parameter(ParameterSetName = "GetLogSet")] - [Parameter(ParameterSetName = "HashQuerySet")] public SwitchParameter Force { get; set; } @@ -271,7 +259,6 @@ public sealed class GetWinEventCommand : PSCmdlet [Parameter(ParameterSetName = "FileSet")] [Parameter(ParameterSetName = "GetProviderSet")] [Parameter(ParameterSetName = "GetLogSet")] - [Parameter(ParameterSetName = "HashQuerySet")] [Parameter(ParameterSetName = "XmlQuerySet")] public SwitchParameter Oldest From 0347d19a9f0be2beed53dd4ad9c28373ee5135e1 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Wed, 16 Dec 2020 17:13:44 +0000 Subject: [PATCH 3/3] Fix RCS1188: Remove redundant auto-property initialization https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1188.md --- .../CounterSample.cs | 20 +++++++++---------- .../GetEventCommand.cs | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs index 14560794a5a..9089216a485 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/CounterSample.cs @@ -45,25 +45,25 @@ internal PerformanceCounterSample(string path, public string InstanceName { get; set; } = string.Empty; - public double CookedValue { get; set; } = 0; + public double CookedValue { get; set; } - public UInt64 RawValue { get; set; } = 0; + public UInt64 RawValue { get; set; } - public UInt64 SecondValue { get; set; } = 0; + public UInt64 SecondValue { get; set; } - public uint MultipleCount { get; set; } = 0; + public uint MultipleCount { get; set; } - public PerformanceCounterType CounterType { get; set; } = 0; + public PerformanceCounterType CounterType { get; set; } public DateTime Timestamp { get; set; } = DateTime.MinValue; - public UInt64 Timestamp100NSec { get; set; } = 0; + public UInt64 Timestamp100NSec { get; set; } - public UInt32 Status { get; set; } = 0; + public UInt32 Status { get; set; } - public UInt32 DefaultScale { get; set; } = 0; + public UInt32 DefaultScale { get; set; } - public UInt64 TimeBase { get; set; } = 0; + public UInt64 TimeBase { get; set; } } public class PerformanceCounterSampleSet @@ -87,7 +87,7 @@ internal PerformanceCounterSampleSet(DateTime timeStamp, Scope = "member", Target = "Microsoft.PowerShell.Commands.GetCounter.PerformanceCounterSample.CounterSamples", Justification = "A string[] is required here because that is the type Powershell supports")] - public PerformanceCounterSample[] CounterSamples { get; set; } = null; + public PerformanceCounterSample[] CounterSamples { get; set; } private readonly ResourceManager _resourceMgr = null; } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs index a61d25e1b8b..8c94f34dbe5 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs @@ -226,7 +226,7 @@ public sealed class GetWinEventCommand : PSCmdlet Scope = "member", Target = "Microsoft.PowerShell.Commands.GetEvent.FilterXml", Justification = "An XmlDocument is required here because that is the type Powershell supports")] - public XmlDocument FilterXml { get; set; } = null; + public XmlDocument FilterXml { get; set; } /// /// FilterHashtable parameter.