public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception, bool getServiceReference) { string str = null; string str1 = null; string traceString = null; if (source != null) { str = DiagnosticTrace.CreateSourceString(source); } if (traceRecord != null) { StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)); xmlTextWriter.WriteStartElement("ExtendedData"); traceRecord.WriteTo(xmlTextWriter); xmlTextWriter.WriteEndElement(); str1 = stringBuilder.ToString(); } if (exception != null) { traceString = DiagnosticTrace.ExceptionToTraceString(exception); } return(new TracePayload(traceString, str, DiagnosticTrace.appDomainFriendlyName, str1, string.Empty)); }
public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTrace diagnosticTrace) { EventLogger eventLogger = new EventLogger(); eventLogger.SetLogSourceName(eventLogSourceName, diagnosticTrace); return(eventLogger); }
private static string BuildTrace(ref System.Diagnostics.Eventing.EventDescriptor eventDescriptor, string description, TracePayload payload) { StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)); xmlTextWriter.WriteStartElement("TraceRecord"); xmlTextWriter.WriteAttributeString("xmlns", "http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"); xmlTextWriter.WriteAttributeString("Severity", TraceLevelHelper.LookupSeverity((TraceEventLevel)eventDescriptor.Level, (TraceEventOpcode)eventDescriptor.Opcode)); xmlTextWriter.WriteAttributeString("Channel", DiagnosticTrace.LookupChannel((TraceChannel)eventDescriptor.Channel)); xmlTextWriter.WriteElementString("TraceIdentifier", DiagnosticTrace.GenerateTraceCode(ref eventDescriptor)); xmlTextWriter.WriteElementString("Description", description); xmlTextWriter.WriteElementString("AppDomain", payload.AppDomainFriendlyName); if (!string.IsNullOrEmpty(payload.EventSource)) { xmlTextWriter.WriteElementString("Source", payload.EventSource); } if (!string.IsNullOrEmpty(payload.ExtendedData)) { xmlTextWriter.WriteRaw(payload.ExtendedData); } if (!string.IsNullOrEmpty(payload.SerializedException)) { xmlTextWriter.WriteRaw(payload.SerializedException); } xmlTextWriter.WriteEndElement(); return(stringBuilder.ToString()); }
public void Event(ref System.Diagnostics.Eventing.EventDescriptor eventDescriptor, string description) { if (this.TracingEnabled) { TracePayload serializedPayload = DiagnosticTrace.GetSerializedPayload(null, null, null); this.WriteTraceSource(ref eventDescriptor, description, serializedPayload); } }
public void Event(int eventId, TraceEventLevel traceEventLevel, TraceChannel channel, string description) { if (this.TracingEnabled) { System.Diagnostics.Eventing.EventDescriptor eventDescriptor = DiagnosticTrace.GetEventDescriptor(eventId, channel, traceEventLevel); this.Event(ref eventDescriptor, description); } }
public EventLogger(string eventLogSourceName, DiagnosticTrace diagnosticTrace) { try { this.diagnosticTrace = diagnosticTrace; if (EventLogger.canLogEvent) { this.SafeSetLogSourceName(eventLogSourceName); } } catch (SecurityException securityException) { EventLogger.canLogEvent = false; } }
private static string ExceptionToTraceString(Exception exception) { StringBuilder stringBuilder = new StringBuilder(); XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder, CultureInfo.CurrentCulture)); xmlTextWriter.WriteStartElement("Exception"); xmlTextWriter.WriteElementString("ExceptionType", DiagnosticTrace.XmlEncode(exception.GetType().AssemblyQualifiedName)); xmlTextWriter.WriteElementString("Message", DiagnosticTrace.XmlEncode(exception.Message)); xmlTextWriter.WriteElementString("StackTrace", DiagnosticTrace.XmlEncode(DiagnosticTrace.StackTraceString(exception))); xmlTextWriter.WriteElementString("ExceptionString", DiagnosticTrace.XmlEncode(exception.ToString())); Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; xmlTextWriter.WriteElementString("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture)); } if (exception.Data != null && exception.Data.Count > 0) { xmlTextWriter.WriteStartElement("DataItems"); foreach (object key in exception.Data.Keys) { xmlTextWriter.WriteStartElement("Data"); xmlTextWriter.WriteElementString("Key", DiagnosticTrace.XmlEncode(key.ToString())); xmlTextWriter.WriteElementString("Value", DiagnosticTrace.XmlEncode(exception.Data[key].ToString())); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); } if (exception.InnerException != null) { xmlTextWriter.WriteStartElement("InnerException"); xmlTextWriter.WriteRaw(DiagnosticTrace.ExceptionToTraceString(exception.InnerException)); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); return(stringBuilder.ToString()); }
public void WriteTraceSource(ref System.Diagnostics.Eventing.EventDescriptor eventDescriptor, string description, TracePayload payload) { if (this.TracingEnabled) { XPathNavigator xPathNavigator = null; try { string str = DiagnosticTrace.BuildTrace(ref eventDescriptor, description, payload); XmlDocument xmlDocument = new XmlDocument(); StringReader stringReader = new StringReader(str); XmlReaderSettings xmlReaderSetting = new XmlReaderSettings() { DtdProcessing = DtdProcessing.Prohibit }; using (XmlReader xmlReader = XmlReader.Create(stringReader, xmlReaderSetting)) { xmlDocument.Load(xmlReader); } xPathNavigator = xmlDocument.CreateNavigator(); this.TraceSource.TraceData(TraceLevelHelper.GetTraceEventType(eventDescriptor.Level, eventDescriptor.Opcode), eventDescriptor.EventId, xPathNavigator); if (this.calledShutdown) { this.TraceSource.Flush(); } } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } this.LogTraceFailure((xPathNavigator == null ? string.Empty : xPathNavigator.ToString()), exception); } } }
public void UnsafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values) { if (EventLogger.logCountForPT < 5) { try { int length = 0; string[] strArrays = new string[(int)values.Length + 2]; for (int i = 0; i < (int)values.Length; i++) { string str = values[i]; str = (string.IsNullOrEmpty(str) ? string.Empty : EventLogger.NormalizeEventLogParameter(str)); strArrays[i] = str; length = length + str.Length + 1; } string str1 = EventLogger.NormalizeEventLogParameter(EventLogger.UnsafeGetProcessName()); strArrays[(int)strArrays.Length - 2] = str1; length = length + str1.Length + 1; string str2 = EventLogger.UnsafeGetProcessId().ToString(CultureInfo.InvariantCulture); strArrays[(int)strArrays.Length - 1] = str2; length = length + str2.Length + 1; if (length > 25600) { int num = 25600 / (int)strArrays.Length - 1; for (int j = 0; j < (int)strArrays.Length; j++) { if (strArrays[j].Length > num) { strArrays[j] = strArrays[j].Substring(0, num); } } } SecurityIdentifier user = WindowsIdentity.GetCurrent().User; byte[] numArray = new byte[user.BinaryLength]; user.GetBinaryForm(numArray, 0); IntPtr[] intPtrArray = new IntPtr[(int)strArrays.Length]; GCHandle gCHandle = new GCHandle(); GCHandle[] gCHandleArray = null; try { gCHandle = GCHandle.Alloc(intPtrArray, GCHandleType.Pinned); gCHandleArray = new GCHandle[(int)strArrays.Length]; for (int k = 0; k < (int)strArrays.Length; k++) { gCHandleArray[k] = GCHandle.Alloc(strArrays[k], GCHandleType.Pinned); intPtrArray[k] = gCHandleArray[k].AddrOfPinnedObject(); } this.UnsafeWriteEventLog(type, eventLogCategory, eventId, strArrays, numArray, gCHandle); } finally { if (gCHandle.AddrOfPinnedObject() != IntPtr.Zero) { gCHandle.Free(); } if (gCHandleArray != null) { GCHandle[] gCHandleArray1 = gCHandleArray; for (int l = 0; l < (int)gCHandleArray1.Length; l++) { gCHandleArray1[l].Free(); } } } if (shouldTrace && this.diagnosticTrace != null && (TraceCore.TraceCodeEventLogCriticalIsEnabled(this.diagnosticTrace) || TraceCore.TraceCodeEventLogVerboseIsEnabled(this.diagnosticTrace) || TraceCore.TraceCodeEventLogInfoIsEnabled(this.diagnosticTrace) || TraceCore.TraceCodeEventLogWarningIsEnabled(this.diagnosticTrace) || TraceCore.TraceCodeEventLogErrorIsEnabled(this.diagnosticTrace))) { Dictionary <string, string> strs = new Dictionary <string, string>((int)strArrays.Length + 4); strs["CategoryID.Name"] = "EventLogCategory"; strs["CategoryID.Value"] = eventLogCategory.ToString(CultureInfo.InvariantCulture); strs["InstanceID.Name"] = "EventId"; strs["InstanceID.Value"] = eventId.ToString(CultureInfo.InvariantCulture); for (int m = 0; m < (int)values.Length; m++) { strs.Add(string.Concat("Value", m.ToString(CultureInfo.InvariantCulture)), (values[m] == null ? string.Empty : DiagnosticTrace.XmlEncode(values[m]))); } TraceRecord dictionaryTraceRecord = new DictionaryTraceRecord(strs); TraceEventType traceEventType = type; switch (traceEventType) { case TraceEventType.Critical: { TraceCore.TraceCodeEventLogCritical(this.diagnosticTrace, dictionaryTraceRecord); break; } case TraceEventType.Error: { TraceCore.TraceCodeEventLogError(this.diagnosticTrace, dictionaryTraceRecord); break; } case TraceEventType.Critical | TraceEventType.Error: { break; } case TraceEventType.Warning: { TraceCore.TraceCodeEventLogWarning(this.diagnosticTrace, dictionaryTraceRecord); break; } default: { if (traceEventType == TraceEventType.Information) { TraceCore.TraceCodeEventLogInfo(this.diagnosticTrace, dictionaryTraceRecord); break; } else if (traceEventType == TraceEventType.Verbose) { TraceCore.TraceCodeEventLogVerbose(this.diagnosticTrace, dictionaryTraceRecord); break; } else { break; } } } } } catch (Exception exception) { if (Fx.IsFatal(exception)) { throw; } } if (this.isInPartialTrust) { EventLogger.logCountForPT = EventLogger.logCountForPT + 1; } } }
private void SetLogSourceName(string updatedEventLogSourceName, DiagnosticTrace updatedDiagnosticTrace) { this.eventLogSourceName = updatedEventLogSourceName; this.diagnosticTrace = updatedDiagnosticTrace; }
public static TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception) { return(DiagnosticTrace.GetSerializedPayload(source, traceRecord, exception, false)); }