Beispiel #1
0
        /// <summary>
        /// Gets the bitmask of the PSChannel values to log.
        /// </summary>
        /// <returns>
        /// A bitmask of PSChannel.Operational and/or PSChannel.Analytic. The default value is PSChannel.Operational.
        /// </returns>
        internal PSChannel GetLogChannels()
        {
            string values = ReadValueFromFile <string>(ConfigScope.AllUsers, "LogChannels");

            PSChannel result = 0;

            if (!string.IsNullOrEmpty(values))
            {
                string[] names = values.Split(s_valueSeparators, StringSplitOptions.RemoveEmptyEntries);

                foreach (string name in names)
                {
                    if (name.Equals(LogDefaultValue, StringComparison.OrdinalIgnoreCase))
                    {
                        result = 0;
                        break;
                    }

                    PSChannel value;
                    if (Enum.TryParse <PSChannel>(name, true, out value))
                    {
                        result |= value;
                    }
                }
            }

            if (result == 0)
            {
                result = System.Management.Automation.Tracing.PSSysLogProvider.DefaultChannels;
            }

            return(result);
        }
Beispiel #2
0
 /// <summary>
 /// Writes a single event
 /// </summary>
 /// <param name="id">event id</param>
 /// <param name="channel"></param>
 /// <param name="opcode"></param>
 /// <param name="task"></param>
 /// <param name="logContext">log context</param>
 /// <param name="payLoad"></param>
 internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSTask task, LogContext logContext, string payLoad)
 {
     s_provider.Log(id, channel, task, opcode, GetPSLevelFromSeverity(logContext.Severity), DefaultKeywords,
                    LogContextToString(logContext),
                    GetPSLogUserData(logContext.ExecutionContext),
                    payLoad);
 }
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <param name="eventId">The event id of the log entry.</param>
        /// <param name="channel">The channel to log.</param>
        /// <param name="task">The task for the log entry.</param>
        /// <param name="opcode">The operation for the log entry.</param>
        /// <param name="level">The logging level.</param>
        /// <param name="keyword">The keyword(s) for the event.</param>
        /// <param name="args">The payload for the log message.</param>
        public void Log(PSEventId eventId, PSChannel channel, PSTask task, PSOpcode opcode, PSLevel level, PSKeyword keyword, params object[] args)
        {
            if (ShouldLog(level, keyword, channel))
            {
                int threadId = Thread.CurrentThread.ManagedThreadId;

                StringBuilder sb = MessageBuilder;
                sb.Clear();

                // add the message preamble
                sb.AppendFormat(CultureInfo.InvariantCulture,
                                "({0}:{1:X}:{2:X}) [{3:G}:{4:G}.{5:G}.{6:G}] ",
                                PSVersionInfo.GitCommitId, threadId, channel, eventId, task, opcode, level);

                // add the message
                GetEventMessage(sb, eventId, args);

                NativeMethods.SysLogPriority priority;
                if ((int)level <= _levels.Length)
                {
                    priority = _levels[(int)level];
                }
                else
                {
                    priority = NativeMethods.SysLogPriority.Info;
                }
                // log it.
                NativeMethods.SysLog(priority, sb.ToString());
            }
        }
Beispiel #4
0
        /// <summary>
        /// Gets the bitmask of the PSChannel values to log.
        /// </summary>
        /// <returns>
        /// A bitmask of PSChannel.Operational and/or PSChannel.Analytic.
        /// <para>
        /// The default value is PSChannel.Operational.
        /// </para>
        /// </returns>
        internal override PSChannel GetLogChannels()
        {
            string fileName = Path.Combine(psHomeConfigDirectory, configFileName);
            string values   = ReadValueFromFile <string>(fileName, "LogChannels");

            PSChannel result = 0;

            if (!string.IsNullOrEmpty(values))
            {
                string[] names = values.Split(s_valueSeparators, StringSplitOptions.RemoveEmptyEntries);

                foreach (string name in names)
                {
                    if (name.Equals(LogDefaultValue, StringComparison.OrdinalIgnoreCase))
                    {
                        result = 0;
                        break;
                    }

                    PSChannel value;
                    if (Enum.TryParse <PSChannel>(name, true, out value))
                    {
                        result |= value;
                    }
                }
            }

            if (result == 0)
            {
                result = DefaultChannels;
            }

            return(result);
        }
Beispiel #5
0
        internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSLevel level, PSTask task, PSKeyword keyword, params object[] args)
        {
            long keywords = 0L;

            if ((keyword == PSKeyword.UseAlwaysAnalytic) || (keyword == PSKeyword.UseAlwaysOperational))
            {
                keywords = 0L;
            }
            else
            {
                keywords = (long)keyword;
            }
            System.Diagnostics.Eventing.EventDescriptor eventDescriptor = new System.Diagnostics.Eventing.EventDescriptor((int)id, 1, (byte)channel, (byte)level, (byte)opcode, (int)task, keywords);
            etwProvider.WriteEvent(ref eventDescriptor, args);
        }
Beispiel #6
0
        /// <summary>
        /// Writes an event
        /// </summary>
        /// <param name="id"></param>
        /// <param name="channel"></param>
        /// <param name="opcode"></param>
        /// <param name="level"></param>
        /// <param name="task"></param>
        /// <param name="keyword"></param>
        /// <param name="args"></param>
        internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSLevel level, PSTask task, PSKeyword keyword, params object[] args)
        {
            long longKeyword = 0x00;

            if (keyword == PSKeyword.UseAlwaysAnalytic ||
                keyword == PSKeyword.UseAlwaysOperational)
            {
                longKeyword = 0x00;
            }
            else
            {
                longKeyword = (long)keyword;
            }

            EventDescriptor desc = new EventDescriptor((int)id, (byte)PSEventVersion.One, (byte)channel,
                                                       (byte)level, (byte)opcode, (int)task, longKeyword);

            etwProvider.WriteEvent(ref desc, args);
        }
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <param name="eventId">The event id of the log entry.</param>
        /// <param name="channel">The channel to log.</param>
        /// <param name="task">The task for the log entry.</param>
        /// <param name="opcode">The operation for the log entry.</param>
        /// <param name="level">The logging level.</param>
        /// <param name="keyword">The keyword(s) for the event.</param>
        /// <param name="args">The payload for the log message.</param>
        public void Log(PSEventId eventId, PSChannel channel, PSTask task, PSOpcode opcode, PSLevel level, PSKeyword keyword, params object[] args)
        {
            if (keyword == PSKeyword.UseAlwaysAnalytic)
            {
                // Use the 'DefaultKeywords' to work around the default keyword filter.
                // Note that the PSKeyword argument is not really used in writing SysLog.
                keyword = PSSysLogProvider.DefaultKeywords;
            }

            if (ShouldLog(level, keyword, channel))
            {
                int threadId = Thread.CurrentThread.ManagedThreadId;

                StringBuilder sb = MessageBuilder;
                sb.Clear();

                // add the message preamble
                sb.AppendFormat(CultureInfo.InvariantCulture,
                                "({0}:{1:X}:{2:X}) [{3:G}:{4:G}.{5:G}.{6:G}] ",
                                PSVersionInfo.GitCommitId, threadId, channel, eventId, task, opcode, level);

                // add the message
                GetEventMessage(sb, eventId, args);

                NativeMethods.SysLogPriority priority;
                if ((int)level <= _levels.Length)
                {
                    priority = _levels[(int)level];
                }
                else
                {
                    priority = NativeMethods.SysLogPriority.Info;
                }
                // log it.
                NativeMethods.SysLog(priority, sb.ToString());
            }
        }
Beispiel #8
0
 /// <summary>
 /// Writes a single event
 /// </summary>
 /// <param name="id">event id.</param>
 /// <param name="channel"></param>
 /// <param name="opcode"></param>
 /// <param name="task"></param>
 /// <param name="logContext">log context.</param>
 /// <param name="payLoad"></param>
 internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSTask task, LogContext logContext, string payLoad)
 {
     WriteEvent(id, channel, opcode, GetPSLevelFromSeverity(logContext.Severity), task, (PSKeyword)0x0,
                LogContextToString(logContext), GetPSLogUserData(logContext.ExecutionContext), payLoad);
 }
Beispiel #9
0
 /// <summary>
 /// Writes an event
 /// </summary>
 /// <param name="id"></param>
 /// <param name="channel"></param>
 /// <param name="opcode"></param>
 /// <param name="level"></param>
 /// <param name="task"></param>
 /// <param name="keyword"></param>
 /// <param name="args"></param>
 internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSLevel level, PSTask task, PSKeyword keyword, params object[] args)
 {
     s_provider.Log(id, channel, task, opcode, level, keyword, args);
 }
        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="applicationId">The log identity name used to identify the application in syslog.</param>
        /// <param name="level">The trace level to enable.</param>
        /// <param name="keywords">The keywords to enable.</param>
        /// <param name="channels">The output channels to enable.</param>
        public SysLogProvider(string applicationId, PSLevel level, PSKeyword keywords, PSChannel channels)
        {
            // NOTE: This string needs to remain valid for the life of the process since the underlying API keeps
            // a reference to it.
            // FUTURE: If logging is redesigned, make these details static or a singleton since there should only be one
            // instance active.
            _nativeSyslogIdent = Marshal.StringToHGlobalAnsi(applicationId);
            NativeMethods.OpenLog(_nativeSyslogIdent, _facility);
            _keywordFilter = (ulong)keywords;
            _levelFilter   = (byte)level;
            _channelFilter = (byte)channels;
            if ((_channelFilter & (ulong)PSChannel.Operational) != 0)
            {
                _keywordFilter |= (ulong)PSKeyword.UseAlwaysOperational;
            }

            if ((_channelFilter & (ulong)PSChannel.Analytic) != 0)
            {
                _keywordFilter |= (ulong)PSKeyword.UseAlwaysAnalytic;
            }
        }
 // NOTE: There are a number of places where PowerShell code sends analytic events
 // to the operational channel. This is a side-effect of the custom wrappers that
 // use flags that are not consistent with the event definition.
 // To ensure filtering of analytic events is consistent, both keyword and channel
 // filtering is performed to suppress analytic events.
 private bool ShouldLog(PSLevel level, PSKeyword keywords, PSChannel channel)
 {
     return((_channelFilter & (ulong)channel) != 0 &&
            IsEnabled(level, keywords));
 }
Beispiel #12
0
        /// <summary>
        /// Writes an event
        /// </summary>
        /// <param name="id"></param>
        /// <param name="channel"></param>
        /// <param name="opcode"></param>
        /// <param name="level"></param>
        /// <param name="task"></param>
        /// <param name="keyword"></param>
        /// <param name="args"></param>
        internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSLevel level, PSTask task, PSKeyword keyword, params object[] args)
        {
            long longKeyword = 0x00;

            if (keyword == PSKeyword.UseAlwaysAnalytic ||
                keyword == PSKeyword.UseAlwaysOperational)
            {
                longKeyword = 0x00;
            }
            else
            {
                longKeyword = (long)keyword;
            }

            EventDescriptor desc = new EventDescriptor((int)id, (byte)PSEventVersion.One, (byte)channel,
                (byte)level, (byte)opcode, (int)task, longKeyword);

            etwProvider.WriteEvent(ref desc, args);
        }
Beispiel #13
0
 /// <summary>
 /// Writes a single event
 /// </summary>
 /// <param name="id">event id</param>
 /// <param name="channel"></param>
 /// <param name="opcode"></param>
 /// <param name="task"></param>
 /// <param name="logContext">log context</param>
 /// <param name="payLoad"></param>
 internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSTask task, LogContext logContext, string payLoad)
 {
     WriteEvent(id, channel, opcode, GetPSLevelFromSeverity(logContext.Severity), task, (PSKeyword)0x0,
         LogContextToString(logContext), GetPSLogUserData(logContext.ExecutionContext), payLoad);
 }
Beispiel #14
0
 internal void WriteEvent(PSEventId id, PSChannel channel, PSOpcode opcode, PSLevel level, PSTask task, PSKeyword keyword, params object[] args)
 {
     long keywords = 0L;
     if ((keyword == PSKeyword.UseAlwaysAnalytic) || (keyword == PSKeyword.UseAlwaysOperational))
     {
         keywords = 0L;
     }
     else
     {
         keywords = (long) keyword;
     }
     System.Diagnostics.Eventing.EventDescriptor eventDescriptor = new System.Diagnostics.Eventing.EventDescriptor((int) id, 1, (byte) channel, (byte) level, (byte) opcode, (int) task, keywords);
     etwProvider.WriteEvent(ref eventDescriptor, args);
 }