/// <summary> /// Reconstructs an instance of a <see cref="TraceEventRecord"/> object based on its <see cref="Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry"/> counterpart from Enterprise Library. /// </summary> /// <param name="logEntry">A log entry used by the Logging Application Block in Enterprise Library.</param> /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns> public static TraceEventRecord Create(LogEntry logEntry) { Guard.ArgumentNotNull(logEntry, "logEntry"); TraceEventRecord eventRecord = new TraceEventRecord { DateTime = logEntry.TimeStamp, ProcessId = Convert.ToInt32(logEntry.ProcessId), ThreadId = Convert.ToInt32(logEntry.Win32ThreadId), MachineName = logEntry.MachineName, ProcessName = logEntry.ProcessName, Timestamp = logEntry.TimeStamp.Ticks, EventId = logEntry.EventId, EventSource = logEntry.Title, EventType = logEntry.Severity, Message = logEntry.Message }; // Ensure that provider GUID is copied from the extended property collection. object propValue = logEntry.ExtendedProperties[InstrumentationUtility.Resources.TracingProviderGuidPropertyName]; if (propValue != null) { Guid guidValue; if (Guid.TryParse(Convert.ToString(propValue), out guidValue)) { eventRecord.EventSourceId = guidValue; } } return(eventRecord); }
public void TraceEvent(TraceEventRecord eventRecord) { if (IsEnabled) { LogEntry logEntry = new LogEntry() { TimeStamp = eventRecord.DateTime, ProcessId = eventRecord.ProcessId.ToString(), Win32ThreadId = eventRecord.ThreadId.ToString(), MachineName = eventRecord.MachineName, ProcessName = eventRecord.ProcessName, EventId = eventRecord.EventId, Message = eventRecord.Message, Severity = eventRecord.EventType, Title = eventRecord.EventSource }; logEntry.Categories.Add(WellKnownTraceCategory.CreateFrom(eventRecord)); logEntry.ExtendedProperties[InstrumentationUtility.Resources.TracingProviderGuidPropertyName] = this.providerGuid; if (TraceWriter.ShouldLog(logEntry)) { TraceWriter.Write(logEntry); } } }
/// <summary> /// Returns an instance of a <see cref="TraceEventRecord"/> object initialized with the given parameters. /// </summary> /// <param name="eventSource">The name of the source generated the event.</param> /// <param name="eventSourceId">The unique identifier of the source generated this event.</param> /// <param name="eventType">One of the <see cref="System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param> /// <param name="eventId">The ID of the trace event.</param> /// <param name="message">The text of the trace message.</param> /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns> public static TraceEventRecord Create(string eventSource, Guid eventSourceId, TraceEventType eventType, int eventId, string message) { TraceEventRecord eventRecord = Create(eventSource, eventType, eventId, message); eventRecord.EventSourceId = eventSourceId; return(eventRecord); }
/// <summary> /// Reconstructs an instance of a <see cref="TraceEventRecord"/> object from its XML representation generated by <see cref="System.Runtime.Serialization.DataContractSerializer"/>. /// </summary> /// <param name="reader">The XML reader supplying the serialized representation of a <see cref="TraceEventRecord"/> object.</param> /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns> public static TraceEventRecord Create(XmlReader reader) { Guard.ArgumentNotNull(reader, "reader"); DataContractAttribute dataContractAttr = FrameworkUtility.GetDeclarativeAttribute <DataContractAttribute>(typeof(TraceEventRecord)); if (dataContractAttr != null) { XElement eventRecordXml = XElement.Load(reader, LoadOptions.None); if (eventRecordXml.Name.LocalName == dataContractAttr.Name && eventRecordXml.Name.Namespace == dataContractAttr.Namespace) { XElement childElement; TraceEventRecord eventRecord = new TraceEventRecord { DateTime = (childElement = (from child in eventRecordXml.Descendants(XName.Get(DateTimePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?DateTime.Parse(childElement.Value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) : default(DateTime), ProcessId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ProcessIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0, ThreadId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ThreadIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0, MachineName = (childElement = (from child in eventRecordXml.Descendants(XName.Get(MachineNamePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null, ProcessName = (childElement = (from child in eventRecordXml.Descendants(XName.Get(ProcessNamePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null, Timestamp = (childElement = (from child in eventRecordXml.Descendants(XName.Get(TimestampPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int64.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0, EventId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Int32.Parse(childElement.Value, CultureInfo.InvariantCulture) : 0, EventSource = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventSourcePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null, EventSourceId = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventSourceIdPropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null?Guid.Parse(childElement.Value) : Guid.Empty, EventType = (childElement = (from child in eventRecordXml.Descendants(XName.Get(EventTypePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? (TraceEventType)Enum.Parse(typeof(TraceEventType), childElement.Value, true) : default(TraceEventType), Message = (childElement = (from child in eventRecordXml.Descendants(XName.Get(MessagePropertyName, dataContractAttr.Namespace)) select child).FirstOrDefault()) != null ? childElement.Value : null }; return(eventRecord); } else { throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, ExceptionMessages.CannotCreateInstanceFromXmlReader, typeof(TraceEventRecord).Name, dataContractAttr.Namespace, eventRecordXml.Name.LocalName, eventRecordXml.Name.Namespace), "reader"); } } else { throw new ArgumentNullException(typeof(DataContractAttribute).FullName); } }
/// <summary> /// Returns an instance of a <see cref="TraceEventRecord"/> object initialized with the given parameters. /// </summary> /// <param name="eventCache">Provides trace event data specific to a thread and a process.</param> /// <param name="eventSource">The name of the source generated the event.</param> /// <param name="eventType">One of the <see cref="System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param> /// <param name="eventId">The ID of the trace event.</param> /// <param name="message">The text of the trace message.</param> /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns> public static TraceEventRecord Create(TraceEventCache eventCache, string eventSource, TraceEventType eventType, int eventId, string message) { Guard.ArgumentNotNull(eventCache, "eventCache"); Guard.ArgumentNotNullOrEmptyString(eventSource, "eventSource"); TraceEventRecord eventRecord = new TraceEventRecord { DateTime = DateTime.UtcNow, ProcessId = eventCache.ProcessId, ThreadId = Convert.ToInt32(eventCache.ThreadId), MachineName = Environment.MachineName, ProcessName = Process.GetCurrentProcess().ProcessName, Timestamp = eventCache.Timestamp, EventId = eventId, EventSource = eventSource, EventType = eventType, Message = message }; return(eventRecord); }
/// <summary> /// Returns an instance of a <see cref="TraceEventRecord"/> object initialized with the given parameters. /// </summary> /// <param name="eventSource">The name of the source generated the event.</param> /// <param name="eventType">One of the <see cref="System.Diagnostics.TraceEventType"/> values specifying the type of event that has caused the trace.</param> /// <param name="eventId">The ID of the trace event.</param> /// <param name="message">The text of the trace message.</param> /// <returns>The initialized instance of a <see cref="TraceEventRecord"/> object containing trace event details.</returns> public static TraceEventRecord Create(string eventSource, TraceEventType eventType, int eventId, string message) { Guard.ArgumentNotNullOrEmptyString(eventSource, "eventSource"); Process currentProcess = Process.GetCurrentProcess(); TraceEventRecord eventRecord = new TraceEventRecord { DateTime = DateTime.UtcNow, ProcessId = currentProcess.Id, ThreadId = Thread.CurrentThread.ManagedThreadId, MachineName = Environment.MachineName, ProcessName = currentProcess.ProcessName, Timestamp = HighResolutionTimer.CurrentTickCount, EventId = eventId, EventSource = eventSource, EventType = eventType, Message = message }; return(eventRecord); }
public void TraceEvent(TraceEventRecord eventRecord) { TraceEvent(eventRecord.EventType, eventRecord.EventCategory, eventRecord.EventId, eventRecord.Message); }