Esempio n. 1
0
        //
        // FindLogNamesMatchingWildcards helper.
        // Finds all logs whose names match wildcard patterns in the 'logPatterns' argument.   
        // For each non-matched pattern, a non-terminating error is written.
        // The results are added to _logNamesMatchingWildcard array.  
        //
        private void FindLogNamesMatchingWildcards(EventLogSession eventLogSession, IEnumerable<string> logPatterns)
        {
            if (_logNamesMatchingWildcard == null)
            {
                _logNamesMatchingWildcard = new StringCollection();
            }
            else
            {
                _logNamesMatchingWildcard.Clear();
            }

            foreach (string logPattern in logPatterns)
            {
                bool bMatched = false;
                foreach (string actualLogName in eventLogSession.GetLogNames())
                {
                    WildcardPattern wildLogPattern = new WildcardPattern(logPattern, WildcardOptions.IgnoreCase);

                    if (((!WildcardPattern.ContainsWildcardCharacters(logPattern))
                        && (logPattern.Equals(actualLogName, StringComparison.CurrentCultureIgnoreCase)))
                        ||
                        (wildLogPattern.IsMatch(actualLogName)))
                    {
                        //
                        // Skip direct ETW channels matching wildcards unless -force is present.
                        // Error out for direct channels unless -oldest is present.
                        //
                        EventLogConfiguration logObj;
                        try
                        {
                            logObj = new EventLogConfiguration(actualLogName, eventLogSession);
                        }
                        catch (Exception exc)
                        {
                            string msg = string.Format(CultureInfo.InvariantCulture,
                                                     _resourceMgr.GetString("LogInfoUnavailable"),
                                                     actualLogName, exc.Message);
                            Exception outerExc = new Exception(msg, exc);
                            WriteError(new ErrorRecord(outerExc, "LogInfoUnavailable", ErrorCategory.NotSpecified, null));
                            continue;
                        }

                        if (logObj.LogType == EventLogType.Debug || logObj.LogType == EventLogType.Analytical)
                        {
                            if (WildcardPattern.ContainsWildcardCharacters(logPattern) && !Force.IsPresent)
                            {
                                continue;
                            }

                            ValidateLogName(actualLogName, eventLogSession);
                        }

                        if (!_logNamesMatchingWildcard.Contains(actualLogName.ToLowerInvariant()))
                        {
                            _logNamesMatchingWildcard.Add(actualLogName.ToLowerInvariant());
                        }
                        bMatched = true;
                    }
                }
                if (!bMatched)
                {
                    string msg = _resourceMgr.GetString("NoMatchingLogsFound");
                    Exception exc = new Exception(string.Format(CultureInfo.InvariantCulture, msg, _computerName, logPattern));
                    WriteError(new ErrorRecord(exc, "NoMatchingLogsFound", ErrorCategory.ObjectNotFound, logPattern));
                }
            }
        }
        public string[] GetAllLogNames()
        {
            System.Collections.Generic.IList<string> allLogs = new System.Collections.Generic.List<string>();
            using (EventLogSession logSession = new EventLogSession())
            {
                IEnumerable<string> logNames = logSession.GetLogNames();
                foreach (string logName in logNames)
                {
                    using (EventLogConfiguration logConfig = new EventLogConfiguration(logName))
                    {
                        if (logConfig.IsEnabled)
                        {
                            allLogs.Add(logName);
                        }
                    }
                }
            }

            return allLogs.Cast<string>().ToArray();
        }
Esempio n. 3
0
 private void FindLogNamesMatchingWildcards(EventLogSession eventLogSession, IEnumerable<string> logPatterns)
 {
     if (this._logNamesMatchingWildcard == null)
     {
         this._logNamesMatchingWildcard = new StringCollection();
     }
     else
     {
         this._logNamesMatchingWildcard.Clear();
     }
     foreach (string str in logPatterns)
     {
         bool flag = false;
         foreach (string str2 in eventLogSession.GetLogNames())
         {
             WildcardPattern pattern = new WildcardPattern(str, WildcardOptions.IgnoreCase);
             if ((!WildcardPattern.ContainsWildcardCharacters(str) && str.Equals(str2, StringComparison.CurrentCultureIgnoreCase)) || pattern.IsMatch(str2))
             {
                 EventLogConfiguration configuration;
                 try
                 {
                     configuration = new EventLogConfiguration(str2, eventLogSession);
                 }
                 catch (Exception exception)
                 {
                     Exception exception2 = new Exception(string.Format(CultureInfo.InvariantCulture, this._resourceMgr.GetString("LogInfoUnavailable"), new object[] { str2, exception.Message }), exception);
                     base.WriteError(new ErrorRecord(exception2, "LogInfoUnavailable", ErrorCategory.NotSpecified, null));
                     continue;
                 }
                 if ((configuration.LogType == EventLogType.Debug) || (configuration.LogType == EventLogType.Analytical))
                 {
                     if (WildcardPattern.ContainsWildcardCharacters(str) && !this.Force.IsPresent)
                     {
                         continue;
                     }
                     this.ValidateLogName(str2, eventLogSession);
                 }
                 if (!this._logNamesMatchingWildcard.Contains(str2.ToLower(CultureInfo.InvariantCulture)))
                 {
                     this._logNamesMatchingWildcard.Add(str2.ToLower(CultureInfo.InvariantCulture));
                 }
                 flag = true;
             }
         }
         if (!flag)
         {
             string format = this._resourceMgr.GetString("NoMatchingLogsFound");
             Exception exception3 = new Exception(string.Format(CultureInfo.InvariantCulture, format, new object[] { this._computerName, str }));
             base.WriteError(new ErrorRecord(exception3, "NoMatchingLogsFound", ErrorCategory.ObjectNotFound, str));
         }
     }
 }