예제 #1
0
        public static RealtimeLogEntry[] GetProcessLogs(uint pid)
        {
            List <RealtimeLogEntry> realtimeLogEntries = new List <RealtimeLogEntry>();

            IntPtr p_rg = Marshal.AllocHGlobal(100 * REALTIME_LOG_ENTRY_SIZE_BYTES);

            if (p_rg == null)
            {
                return(null);
            }

            uint count = DllGetProcessLogs(pid, (uint)100, p_rg);

            if (count == 0)
            {
                Marshal.FreeHGlobal(p_rg);
                return(null);
            }

            byte[] logEntriesAll = new byte[count * REALTIME_LOG_ENTRY_SIZE_BYTES];

            Marshal.Copy(p_rg, logEntriesAll, 0, (int)count * REALTIME_LOG_ENTRY_SIZE_BYTES);
            Marshal.FreeHGlobal(p_rg);

            for (uint i = 0; i < count; i++)
            {
                uint severity = (uint)BitConverter.ToInt32(logEntriesAll, (int)i * REALTIME_LOG_ENTRY_SIZE_BYTES);

                byte[] buf = new byte[REALTIME_LOG_ENTRY_SIZE_BYTES - 4];
                Array.Copy(logEntriesAll, (int)(i * REALTIME_LOG_ENTRY_SIZE_BYTES) + 4, buf, 0, REALTIME_LOG_ENTRY_SIZE_BYTES - 4);

                string message = ASCIIEncoding.Unicode.GetString(buf);
                message = message.Substring(0, message.IndexOf('\x00'));

                RealtimeLogEntry rle = new RealtimeLogEntry();
                rle.severity = severity;
                rle.message  = message;

                realtimeLogEntries.Add(rle);
            }

            return(realtimeLogEntries.ToArray());
        }
예제 #2
0
        private void DisplayRealtimeLogEvent(RealtimeLogEntry rle)
        {
            try
            {
                if (this.lvRealtime.InvokeRequired)
                {
                    this.lvRealtime.Invoke(new DisplayRealtimeLogEventDelegate(DisplayRealtimeLogEvent), new object[] { rle });
                }
                else
                {
                    lock (logDisplayLock)
                    {
                        ListViewItem lvi      = new ListViewItem(new string[] { rle.message });
                        Color        logColor = Color.LightGray;

                        switch (rle.severity)
                        {
                        case 1:
                            logColor = Color.Red;
                            break;

                        default:
                            break;
                        }

                        lvi.BackColor = logColor;

                        lvRealtime.Items.Insert(0, lvi);

                        if (lvRealtime.Items.Count > 250)
                        {
                            lvRealtime.Items.RemoveAt(250);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }