Esempio n. 1
0
 protected string XmlEncode(string text)
 {
     return(DiagnosticTrace.XmlEncode(text));
 }
Esempio n. 2
0
        public void UnsafeLogEvent(TraceEventType type, ushort eventLogCategory, uint eventId, bool shouldTrace, params string[] values)
        {
            if (logCountForPT >= 5)
            {
                return;
            }
            try
            {
                int      num       = 0;
                string[] logValues = new string[values.Length + 2];
                for (int i = 0; i < values.Length; i++)
                {
                    string str = values[i];
                    if (!string.IsNullOrEmpty(str))
                    {
                        str = NormalizeEventLogParameter(str);
                    }
                    else
                    {
                        str = string.Empty;
                    }
                    logValues[i] = str;
                    num         += str.Length + 1;
                }
                string str2 = NormalizeEventLogParameter(this.UnsafeGetProcessName());
                logValues[logValues.Length - 2] = str2;
                num += str2.Length + 1;
                string str3 = this.UnsafeGetProcessId().ToString(CultureInfo.InvariantCulture);
                logValues[logValues.Length - 1] = str3;
                num += str3.Length + 1;
                if (num > 0x6400)
                {
                    int length = (0x6400 / logValues.Length) - 1;
                    for (int j = 0; j < logValues.Length; j++)
                    {
                        if (logValues[j].Length > length)
                        {
                            logValues[j] = logValues[j].Substring(0, length);
                        }
                    }
                }
                SecurityIdentifier user       = WindowsIdentity.GetCurrent().User;
                byte[]             binaryForm = new byte[user.BinaryLength];
                user.GetBinaryForm(binaryForm, 0);
                IntPtr[]   ptrArray          = new IntPtr[logValues.Length];
                GCHandle   stringsRootHandle = new GCHandle();
                GCHandle[] handleArray       = null;
                try
                {
                    stringsRootHandle = GCHandle.Alloc(ptrArray, GCHandleType.Pinned);
                    handleArray       = new GCHandle[logValues.Length];
                    for (int k = 0; k < logValues.Length; k++)
                    {
                        handleArray[k] = GCHandle.Alloc(logValues[k], GCHandleType.Pinned);
                        ptrArray[k]    = handleArray[k].AddrOfPinnedObject();
                    }
                    this.UnsafeWriteEventLog(type, eventLogCategory, eventId, logValues, binaryForm, stringsRootHandle);
                }
                finally
                {
                    if (stringsRootHandle.AddrOfPinnedObject() != IntPtr.Zero)
                    {
                        stringsRootHandle.Free();
                    }
                    if (handleArray != null)
                    {
                        foreach (GCHandle handle2 in handleArray)
                        {
                            handle2.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> dictionary = new Dictionary <string, string>(logValues.Length + 4);
                    dictionary["CategoryID.Name"]  = "EventLogCategory";
                    dictionary["CategoryID.Value"] = eventLogCategory.ToString(CultureInfo.InvariantCulture);
                    dictionary["InstanceID.Name"]  = "EventId";
                    dictionary["InstanceID.Value"] = eventId.ToString(CultureInfo.InvariantCulture);
                    for (int m = 0; m < values.Length; m++)
                    {
                        dictionary.Add("Value" + m.ToString(CultureInfo.InvariantCulture), (values[m] == null) ? string.Empty : DiagnosticTrace.XmlEncode(values[m]));
                    }
                    TraceRecord traceRecord = new DictionaryTraceRecord(dictionary);
                    switch (type)
                    {
                    case TraceEventType.Critical:
                        TraceCore.TraceCodeEventLogCritical(this.diagnosticTrace, traceRecord);
                        goto Label_035C;

                    case TraceEventType.Error:
                        TraceCore.TraceCodeEventLogError(this.diagnosticTrace, traceRecord);
                        goto Label_035C;

                    case (TraceEventType.Error | TraceEventType.Critical):
                        goto Label_035C;

                    case TraceEventType.Warning:
                        TraceCore.TraceCodeEventLogWarning(this.diagnosticTrace, traceRecord);
                        goto Label_035C;

                    case TraceEventType.Information:
                        TraceCore.TraceCodeEventLogInfo(this.diagnosticTrace, traceRecord);
                        goto Label_035C;

                    case TraceEventType.Verbose:
                        TraceCore.TraceCodeEventLogVerbose(this.diagnosticTrace, traceRecord);
                        goto Label_035C;
                    }
                }
            }
            catch (Exception exception)
            {
                if (Fx.IsFatal(exception))
                {
                    throw;
                }
            }
Label_035C:
            if (this.isInPartialTrust)
            {
                logCountForPT++;
            }
        }