public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { EventLogger logger = new EventLogger(); logger.SetLogSourceName(eventLogSourceName, diagnosticTrace); return(logger); }
protected static void AddExceptionToTraceString(XmlWriter xml, Exception exception) { xml.WriteElementString("ExceptionType", DiagnosticTraceBase.XmlEncode(exception.GetType().AssemblyQualifiedName)); xml.WriteElementString("Message", DiagnosticTraceBase.XmlEncode(exception.Message)); xml.WriteElementString("StackTrace", DiagnosticTraceBase.XmlEncode(DiagnosticTraceBase.StackTraceString(exception))); xml.WriteElementString("ExceptionString", DiagnosticTraceBase.XmlEncode(exception.ToString())); Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; xml.WriteElementString("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture)); } if (exception.Data != null && exception.Data.Count > 0) { xml.WriteStartElement("DataItems"); foreach (object key in exception.Data.Keys) { xml.WriteStartElement("Data"); xml.WriteElementString("Key", DiagnosticTraceBase.XmlEncode(key.ToString())); xml.WriteElementString("Value", DiagnosticTraceBase.XmlEncode(exception.Data[key].ToString())); xml.WriteEndElement(); } xml.WriteEndElement(); } if (exception.InnerException != null) { xml.WriteStartElement("InnerException"); DiagnosticTraceBase.AddExceptionToTraceString(xml, exception.InnerException); xml.WriteEndElement(); } }
private static string GetExceptionData(Exception exception) { string str; StringBuilder stringBuilder = EtwDiagnosticTrace.StringBuilderPool.Take(); try { using (StringWriter stringWriter = new StringWriter(stringBuilder, CultureInfo.CurrentCulture)) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter)) { xmlTextWriter.WriteStartElement("DataItems"); foreach (object key in exception.Data.Keys) { xmlTextWriter.WriteStartElement("Data"); xmlTextWriter.WriteElementString("Key", DiagnosticTraceBase.XmlEncode(key.ToString())); xmlTextWriter.WriteElementString("Value", DiagnosticTraceBase.XmlEncode(exception.Data[key].ToString())); xmlTextWriter.WriteEndElement(); } xmlTextWriter.WriteEndElement(); xmlTextWriter.Flush(); stringWriter.Flush(); str = stringBuilder.ToString(); } } } finally { EtwDiagnosticTrace.StringBuilderPool.Return(stringBuilder); } return(str); }
protected void SetTraceSource(TraceSource traceSource) { if (traceSource != null) { DiagnosticTraceBase.UnsafeRemoveDefaultTraceListener(traceSource); this.traceSource = traceSource; this.haveListeners = this.traceSource.Listeners.Count > 0; } }
public EventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { try { this.diagnosticTrace = diagnosticTrace; if (EventLogger.canLogEvent) { this.SafeSetLogSourceName(eventLogSourceName); } } catch (SecurityException securityException) { EventLogger.canLogEvent = false; } }
public TracePayload GetSerializedPayload(object source, TraceRecord traceRecord, Exception exception, bool getServiceReference) { string str = null; string str1 = null; string traceString = null; if (source != null) { str = DiagnosticTraceBase.CreateSourceString(source); } if (traceRecord != null) { StringBuilder stringBuilder = EtwDiagnosticTrace.StringBuilderPool.Take(); try { using (StringWriter stringWriter = new StringWriter(stringBuilder, CultureInfo.CurrentCulture)) { using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter)) { xmlTextWriter.WriteStartElement("ExtendedData"); traceRecord.WriteTo(xmlTextWriter); xmlTextWriter.WriteEndElement(); xmlTextWriter.Flush(); stringWriter.Flush(); str1 = stringBuilder.ToString(); } } } finally { EtwDiagnosticTrace.StringBuilderPool.Return(stringBuilder); } } if (exception != null) { traceString = EtwDiagnosticTrace.ExceptionToTraceString(exception, 0x7000); } if (!getServiceReference || EtwDiagnosticTrace.traceAnnotation == null) { return(new TracePayload(traceString, str, DiagnosticTraceBase.AppDomainFriendlyName, str1, string.Empty)); } else { return(new TracePayload(traceString, str, DiagnosticTraceBase.AppDomainFriendlyName, str1, EtwDiagnosticTrace.traceAnnotation())); } }
public EventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { try { this.diagnosticTrace = diagnosticTrace; //set diagnostics trace prior to calling SafeSetLogSourceName if (canLogEvent) { SafeSetLogSourceName(eventLogSourceName); } } catch (SecurityException) { // running in PT, do not try to log events anymore canLogEvent = false; // not throwing exception on purpose } }
private void SetLevel(SourceLevels level) { bool flag; SourceLevels sourceLevel = this.FixLevel(level); this.level = sourceLevel; if (this.TraceSource != null) { this.haveListeners = this.TraceSource.Listeners.Count > 0; this.OnSetLevel(level); DiagnosticTraceBase diagnosticTraceBase = this; if (!this.HaveListeners) { flag = false; } else { flag = level != SourceLevels.Off; } diagnosticTraceBase.tracingEnabled = flag; this.TraceSource.Switch.Level = level; } }
protected string XmlEncode(string text) { return(DiagnosticTraceBase.XmlEncode(text)); }
public static EventLogger UnsafeCreateEventLogger(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { EventLogger eventLogger = new EventLogger(); eventLogger.SetLogSourceName(eventLogSourceName, diagnosticTrace); return eventLogger; }
private void SetLogSourceName(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { this.eventLogSourceName = eventLogSourceName; this.diagnosticTrace = diagnosticTrace; }
private static void WriteExceptionToTraceString(XmlTextWriter xml, Exception exception, int remainingLength, int remainingAllowedRecursionDepth) { if (remainingAllowedRecursionDepth >= 1) { if (EtwDiagnosticTrace.WriteStartElement(xml, "Exception", ref remainingLength)) { try { List <Tuple <string, string> > tuples = new List <Tuple <string, string> >(); tuples.Add(new Tuple <string, string>("ExceptionType", DiagnosticTraceBase.XmlEncode(exception.GetType().AssemblyQualifiedName))); tuples.Add(new Tuple <string, string>("Message", DiagnosticTraceBase.XmlEncode(exception.Message))); tuples.Add(new Tuple <string, string>("StackTrace", DiagnosticTraceBase.XmlEncode(DiagnosticTraceBase.StackTraceString(exception)))); tuples.Add(new Tuple <string, string>("ExceptionString", DiagnosticTraceBase.XmlEncode(exception.ToString()))); IList <Tuple <string, string> > tuples1 = tuples; Win32Exception win32Exception = exception as Win32Exception; if (win32Exception != null) { int nativeErrorCode = win32Exception.NativeErrorCode; tuples1.Add(new Tuple <string, string>("NativeErrorCode", nativeErrorCode.ToString("X", CultureInfo.InvariantCulture))); } foreach (Tuple <string, string> tuple in tuples1) { if (EtwDiagnosticTrace.WriteXmlElementString(xml, tuple.Item1, tuple.Item2, ref remainingLength)) { continue; } return; } if (exception.Data != null && exception.Data.Count > 0) { string exceptionData = EtwDiagnosticTrace.GetExceptionData(exception); if (exceptionData.Length < remainingLength) { xml.WriteRaw(exceptionData); remainingLength = remainingLength - exceptionData.Length; } } if (exception.InnerException != null) { string innerException = EtwDiagnosticTrace.GetInnerException(exception, remainingLength, remainingAllowedRecursionDepth - 1); if (!string.IsNullOrEmpty(innerException) && innerException.Length < remainingLength) { xml.WriteRaw(innerException); } } } finally { xml.WriteEndElement(); } return; } else { return; } } else { return; } }
public void UnsafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values) { if (logCountForPT < MaxEventLogsInPT) { try { // Vista introduces a new limitation: a much smaller max // event log entry size that we need to track. All strings cannot // exceed 31839 characters in length when totalled together. // Choose a max length of 25600 characters (25k) to allow for // buffer since this max length may be reduced without warning. const int MaxEventLogEntryLength = 25600; int eventLogEntryLength = 0; string[] logValues = new string[values.Length + 2]; for (int i = 0; i < values.Length; ++i) { string stringValue = values[i]; if (!string.IsNullOrEmpty(stringValue)) { stringValue = NormalizeEventLogParameter(stringValue); } else { stringValue = String.Empty; } logValues[i] = stringValue; eventLogEntryLength += stringValue.Length + 1; } string normalizedProcessName = NormalizeEventLogParameter(UnsafeGetProcessName()); logValues[logValues.Length - 2] = normalizedProcessName; eventLogEntryLength += (normalizedProcessName.Length + 1); string invariantProcessId = UnsafeGetProcessId().ToString(CultureInfo.InvariantCulture); logValues[logValues.Length - 1] = invariantProcessId; eventLogEntryLength += (invariantProcessId.Length + 1); // If current event log entry length is greater than max length // need to truncate to max length. This probably means that we // have a very long exception and stack trace in our parameter // strings. Truncate each string by MaxEventLogEntryLength // divided by number of strings in the entry. // Truncation algorithm is overly aggressive by design to // simplify the code change due to Product Cycle timing. if (eventLogEntryLength > MaxEventLogEntryLength) { // logValues.Length is always > 0 (minimum value = 2) // Subtract one to insure string ends in '\0' int truncationLength = (MaxEventLogEntryLength / logValues.Length) - 1; for (int i = 0; i < logValues.Length; i++) { if (logValues[i].Length > truncationLength) { logValues[i] = logValues[i].Substring(0, truncationLength); } } } SecurityIdentifier sid = WindowsIdentity.GetCurrent().User; byte[] sidBA = new byte[sid.BinaryLength]; sid.GetBinaryForm(sidBA, 0); IntPtr[] stringRoots = new IntPtr[logValues.Length]; GCHandle stringsRootHandle = new GCHandle(); GCHandle[] stringHandles = null; try { stringsRootHandle = GCHandle.Alloc(stringRoots, GCHandleType.Pinned); stringHandles = new GCHandle[logValues.Length]; for (int strIndex = 0; strIndex < logValues.Length; strIndex++) { stringHandles[strIndex] = GCHandle.Alloc(logValues[strIndex], GCHandleType.Pinned); stringRoots[strIndex] = stringHandles[strIndex].AddrOfPinnedObject(); } UnsafeWriteEventLog(type, eventLogCategory, eventId, logValues, sidBA, stringsRootHandle); } finally { if (stringsRootHandle.AddrOfPinnedObject() != IntPtr.Zero) { stringsRootHandle.Free(); } if (stringHandles != null) { foreach (GCHandle gcHandle in stringHandles) { if (gcHandle != null) { gcHandle.Free(); } } } } if (shouldTrace && this.diagnosticTrace != null && this.diagnosticTrace.IsEnabled()) { const int RequiredValueCount = 4; Dictionary <string, string> eventValues = new Dictionary <string, string>(logValues.Length + RequiredValueCount); eventValues["CategoryID.Name"] = "EventLogCategory"; eventValues["CategoryID.Value"] = eventLogCategory.ToString(CultureInfo.InvariantCulture); eventValues["InstanceID.Name"] = "EventId"; eventValues["InstanceID.Value"] = eventId.ToString(CultureInfo.InvariantCulture); for (int i = 0; i < values.Length; ++i) { eventValues.Add("Value" + i.ToString(CultureInfo.InvariantCulture), values[i] == null ? string.Empty : DiagnosticTraceBase.XmlEncode(values[i])); } this.diagnosticTrace.TraceEventLogEvent(type, new DictionaryTraceRecord((eventValues))); } } catch (Exception e) { if (Fx.IsFatal(e)) { throw; } // If not fatal, just eat the exception } // In PT, we only limit 5 event logging per session if (this.isInPartialTrust) { logCountForPT++; } } }
void SetLogSourceName(string eventLogSourceName, DiagnosticTraceBase diagnosticTrace) { this.eventLogSourceName = eventLogSourceName; this.diagnosticTrace = diagnosticTrace; }
public void UnsafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, string[] values) { string empty; 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]; if (string.IsNullOrEmpty(str)) { str = string.Empty; } else { str = EventLogger.NormalizeEventLogParameter(str); } strArrays[i] = str; length = length + str.Length + 1; } string str1 = EventLogger.NormalizeEventLogParameter(this.UnsafeGetProcessName()); strArrays[(int)strArrays.Length - 2] = str1; length = length + str1.Length + 1; int num = this.UnsafeGetProcessId(); string str2 = num.ToString(CultureInfo.InvariantCulture); strArrays[(int)strArrays.Length - 1] = str2; length = length + str2.Length + 1; if (length > 0x6400) { int length1 = 0x6400 / (int)strArrays.Length - 1; for (int j = 0; j < (int)strArrays.Length; j++) { if (strArrays[j].Length > length1) { strArrays[j] = strArrays[j].Substring(0, length1); } } } 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++) { GCHandle gCHandle1 = gCHandleArray1[l]; gCHandle1.Free(); } } } if (shouldTrace && this.diagnosticTrace != null && this.diagnosticTrace.IsEnabled()) { 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++) { Dictionary <string, string> strs1 = strs; string str3 = string.Concat("Value", m.ToString(CultureInfo.InvariantCulture)); if (values[m] == null) { empty = string.Empty; } else { empty = DiagnosticTraceBase.XmlEncode(values[m]); } strs1.Add(str3, empty); } this.diagnosticTrace.TraceEventLogEvent(type, new DictionaryTraceRecord(strs)); } } catch (Exception exception1) { Exception exception = exception1; if (Fx.IsFatal(exception)) { throw; } } if (this.isInPartialTrust) { EventLogger.logCountForPT = EventLogger.logCountForPT + 1; } } }