1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
//------------------------------------------------------------------------------
// <copyright file="Logging.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Net
{
using System.Collections;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Globalization;
using Microsoft.Win32;
using System.Text;
internal static class Logging
{
private static P2PTraceSource s_P2PTraceSource;
// <SecurityKernel Critical="True" Ring="0">
// <SatisfiesLinkDemand Name="AppDomain.add_ProcessExit(System.EventHandler):System.Void" />
// <SatisfiesLinkDemand Name="AppDomain.add_DomainUnload(System.EventHandler):System.Void" />
// <ReferencesCritical Name="Method: ProcessExitEventHandler(Object, EventArgs):Void" Ring="2" />
// <ReferencesCritical Name="Method: DomainUnloadEventHandler(Object, EventArgs):Void" Ring="2" />
// </SecurityKernel>
[System.Security.SecurityCritical]
static Logging()
{
s_P2PTraceSource = new P2PTraceSource();
if (s_P2PTraceSource.Switch.ShouldTrace(TraceEventType.Critical))
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.ProcessExit += new EventHandler(ProcessExitEventHandler);
//currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
currentDomain.DomainUnload += new EventHandler(DomainUnloadEventHandler);
}
}
// <SecurityKernel Critical="True" Ring="1">
// <ReferencesCritical Name="Method: Close():Void" Ring="1" />
// </SecurityKernel>
[System.Security.SecurityCritical]
private static void DomainUnloadEventHandler(object sender, EventArgs e)
{
Close();
}
/*
private static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)
{
s_P2PTraceSource.TraceEvent(TraceEventType.Critical, UNHANDLED_EXCEPTION_EVENT_ID, "Unhandled Exception {0}", e.ExceptionObject);
}
*/
// <SecurityKernel Critical="True" Ring="1">
// <ReferencesCritical Name="Method: Close():Void" Ring="1" />
// </SecurityKernel>
[System.Security.SecurityCritical]
private static void ProcessExitEventHandler(object sender, EventArgs e)
{
Close();
}
// <SecurityKernel Critical="True" Ring="0">
// <SatisfiesLinkDemand Name="TraceSource.Close():System.Void" />
// </SecurityKernel>
[System.Security.SecurityCritical]
private static void Close()
{
s_P2PTraceSource.Close();
}
//private Logging() {}
internal static P2PTraceSource P2PTraceSource
{
get
{
return s_P2PTraceSource;
}
}
internal static void Enter(TraceSource source, string method)
{
if(source.Switch.ShouldTrace(TraceEventType.Verbose))
{
source.TraceEvent(TraceEventType.Verbose, 0, "Entering --> " + method);
}
}
internal static void Leave(TraceSource source, string message)
{
if (source.Switch.ShouldTrace(TraceEventType.Verbose))
{
source.TraceEvent(TraceEventType.Verbose, 0, "Leaving <-- " + message);
}
}
/*
internal static void Enter(TraceSource source, string type, object obj, string method)
{
}
internal static void Enter(TraceSource source, string type, object obj, string method, params object[] args)
{
}
* */
internal static void DumpData(TraceSource source, TraceEventType eventType, int maxDataSize, byte[] buffer, int offset, int length)
{
if (buffer == null ||
buffer.Length == 0 ||
offset > buffer.Length)
{
return;
}
if (length > maxDataSize)
{
source.TraceEvent(eventType, 0, "dumping {0} of {1} bytes", maxDataSize, length);
length = maxDataSize;
}
if ((length < 0) || (length > buffer.Length - offset))
{
length = buffer.Length - offset;
}
do
{
int n = Math.Min(length, 16);
StringBuilder sb = new StringBuilder();
sb.Append(String.Format(CultureInfo.CurrentCulture, "{0:X8} : ", offset));
for (int i = 0; i < n; ++i)
{
sb.Append(String.Format(CultureInfo.CurrentCulture, "{0:X2}", buffer[offset + i]) + ((i == 7) ? '-' : ' '));
}
for (int i = n; i < 16; ++i)
{
sb.Append(" ");
}
sb.Append(": ");
for (int i = 0; i < n; ++i)
{
sb.Append(((buffer[offset + i] < 0x20) || (buffer[offset + i] > 0x7e))
? '.'
: (char)(buffer[offset + i]));
}
source.TraceEvent(eventType, 0, sb.ToString());
offset += n;
length -= n;
} while (length > 0);
}
}
internal class P2PTraceSource : TraceSource
{
private const string P2PTraceSourceName = "System.Net.PeerToPeer";
private const int DefaultMaxDataSize = 1024;
private const string AttributeNameMaxDataSize = "maxdatasize";
private static readonly string[] P2PTraceSourceSupportedAttributes = new string[] { AttributeNameMaxDataSize };
private readonly int m_maxDataSize = DefaultMaxDataSize;
internal P2PTraceSource() :base(P2PTraceSourceName)
{
if (Attributes.ContainsKey(AttributeNameMaxDataSize))
{
try{
m_maxDataSize = Int32.Parse(Attributes[AttributeNameMaxDataSize], NumberFormatInfo.InvariantInfo);
}
catch (Exception exception) {
if (exception is ThreadAbortException || exception is StackOverflowException || exception is OutOfMemoryException) {
throw;
}
}
}
}
protected override string[] GetSupportedAttributes()
{
return P2PTraceSourceSupportedAttributes;
}
internal int MaxDataSize
{
get
{
return m_maxDataSize;
}
}
}
}
|