コード例 #1
0
        void TraceEventLogEvent(EventLogTraceEvent traceEvent)
        {
            string formatMessage = "";

            using (StringWriter writer = new StringWriter())
                using (XmlTextWriter xmlwriter = new XmlTextWriter(writer))
                {
                    xmlwriter.Formatting = Formatting.Indented;
                    XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
                    xmlnsEmpty.Add("", "");
                    XmlSerializer serializer = new XmlSerializer(typeof(EventLogTraceEvent));
                    serializer.Serialize(xmlwriter, traceEvent, xmlnsEmpty);
                    formatMessage = writer.ToString();
                }

            TraceEvent(traceEvent.EventType, traceEvent.EventId, formatMessage);
        }
コード例 #2
0
        EventLogTraceEvent CreateEventLogEntry(EventLogEntry entry, TraceEventType eventType, int eventId)
        {
            EventLogTraceEvent traceEvent = new EventLogTraceEvent();

            traceEvent.Source         = _traceSource.Name;
            traceEvent.Timestamp      = entry.TimeWritten.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss");
            traceEvent.MachineName    = Environment.MachineName;
            traceEvent.EventId        = eventId;
            traceEvent.EventType      = eventType;
            traceEvent.EventLogSource = entry.Source;
            traceEvent.EventLogId     = (int)(entry.InstanceId & 0x3fff);
            traceEvent.EventLogType   = entry.EntryType;

            if (traceEvent.EventId == -1)
            {
                traceEvent.EventId = traceEvent.EventLogId;
            }

            if (traceEvent.EventType == 0)
            {
                traceEvent.EventType = TraceEventType.Information;
                switch (entry.EntryType)
                {
                case EventLogEntryType.Error: traceEvent.EventType = TraceEventType.Error; break;

                case EventLogEntryType.Warning: traceEvent.EventType = TraceEventType.Warning; break;
                }
            }
            if (System.Environment.OSVersion.Version.Major >= 6)
            {
                if (entry.Message.IndexOf(" Event ID '" + entry.InstanceId.ToString() + "' ") == -1)
                {
                    traceEvent.Message = entry.Message;
                }
                else
                {
                    traceEvent.Message = GetEventLogItemMessage(_eventLog.MachineName, _eventLog.LogDisplayName, (uint)(int)entry.Index);
                }
            }
            else
            {
                traceEvent.Message = entry.Message;
            }

            return(traceEvent);
        }
コード例 #3
0
        void TraceCriticalEvent(string message)
        {
            EventLogTraceEvent traceEvent = new EventLogTraceEvent();

            traceEvent.Message     = message;
            traceEvent.Source      = _traceSource.Name;
            traceEvent.Timestamp   = DateTime.Now.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss");
            traceEvent.MachineName = Environment.MachineName;
            traceEvent.EventId     = -1;
            traceEvent.EventType   = TraceEventType.Critical;
            if (_eventLog != null)
            {
                traceEvent.EventLogSource = _eventLog.Log;
            }
            traceEvent.EventLogId   = -1;
            traceEvent.EventLogType = EventLogEntryType.Error;
            TraceEventLogEvent(traceEvent);
        }
コード例 #4
0
        public TraceSource ConfigureProbe(string configName)
        {
            _traceSource = new TraceSource(configName);
            StringDictionary    attributes = _traceSource.Attributes; // Initialize TraceSource
            NameValueCollection config     = (NameValueCollection)ConfigurationManager.GetSection(configName);

            if (config != null)
            {
                EventLogName = config["EventLogName"];
                if (config["ProbeFrequency"] != null)
                {
                    ProbeFrequency = TimeSpan.FromSeconds(Int32.Parse(config["ProbeFrequency"]));
                }

                foreach (string eventFilter in config.AllKeys)
                {
                    if (!eventFilter.StartsWith("Filter"))
                    {
                        continue;
                    }

                    int filterIndex;
                    if (!int.TryParse(eventFilter.Substring(6), out filterIndex) || eventFilter != ("Filter" + filterIndex.ToString()))
                    {
                        continue;
                    }

                    string[] filterItems = config[eventFilter].Split('=').Select(f => f.Trim()).Where(f => !string.IsNullOrEmpty(f)).ToArray();

                    int            eventId   = -1;
                    TraceEventType eventType = 0;
                    if (filterItems.Length > 1)
                    {
                        foreach (string filterItem in filterItems[1].Split(','))
                        {
                            string traceResultItem = filterItem.Trim();
                            if (String.IsNullOrEmpty(traceResultItem))
                            {
                                continue;
                            }

                            try
                            {
                                eventId = Int32.Parse(traceResultItem);
                            }
                            catch (Exception exId)
                            {
                                try
                                {
                                    eventType = (TraceEventType)Enum.Parse(typeof(TraceEventType), traceResultItem);
                                }
                                catch (Exception exType)
                                {
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " failed to parse " + eventFilter + "=" + config[eventFilter]);
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " - " + exId.Message);
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " - " + exType.Message);
                                    break;
                                }
                            }
                        }
                    }

                    string            eventLogSource = null;
                    int               eventLogId     = -1;
                    EventLogEntryType eventLogType   = 0;
                    if (filterItems.Length >= 1)
                    {
                        foreach (string filterItem in filterItems[0].Split(','))
                        {
                            string eventFilterItem = filterItem.Trim();
                            if (eventLogSource == null)
                            {
                                eventLogSource = eventFilterItem;
                                continue;
                            }

                            if (String.IsNullOrEmpty(eventFilterItem))
                            {
                                continue;
                            }

                            try
                            {
                                eventLogId = Int32.Parse(eventFilterItem);
                            }
                            catch (Exception exId)
                            {
                                try
                                {
                                    eventLogType = (EventLogEntryType)Enum.Parse(typeof(EventLogEntryType), eventFilterItem);
                                }
                                catch (Exception exType)
                                {
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " failed to parse " + eventFilter + "=" + config[eventFilter]);
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " - " + exId.Message);
                                    System.Diagnostics.Trace.WriteLine(_traceSource.Name + " - " + exType.Message);
                                    eventLogSource = null;
                                    break;
                                }
                            }
                        }
                    }

                    if (eventLogSource == null)
                    {
                        continue;
                    }

                    if (!_eventFilter.ContainsKey(eventLogSource))
                    {
                        _eventFilter.Add(eventLogSource, new List <EventLogTraceEvent>());
                    }
                    EventLogTraceEvent eventLogFilter = new EventLogTraceEvent();
                    eventLogFilter.EventId        = eventId;
                    eventLogFilter.EventType      = eventType;
                    eventLogFilter.EventLogSource = eventLogSource;
                    eventLogFilter.EventLogType   = eventLogType;
                    eventLogFilter.EventLogId     = eventLogId;
                    _eventFilter[eventLogSource].Add(eventLogFilter);
                }
            }
            StartProbe(false);
            return(_traceSource);
        }