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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
//------------------------------------------------------------------------------
// <copyright file="TraceUtility.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Services.Diagnostics {
using System.Threading;
using System.Net;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Globalization;
using System.Xml.Serialization;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
internal static class Tracing {
private static bool tracingEnabled = true;
private static bool tracingInitialized;
private static bool appDomainShutdown;
private const string TraceSourceAsmx = "System.Web.Services.Asmx";
private static TraceSource asmxTraceSource;
private static object internalSyncObject;
private static object InternalSyncObject {
get {
if (internalSyncObject == null) {
object o = new Object();
Interlocked.CompareExchange(ref internalSyncObject, o, null);
}
return internalSyncObject;
}
}
internal static bool On {
get {
if (!tracingInitialized) {
InitializeLogging();
}
return tracingEnabled;
}
}
internal static bool IsVerbose {
get {
return ValidateSettings(Asmx, TraceEventType.Verbose);
}
}
internal static TraceSource Asmx {
get {
if (!tracingInitialized)
InitializeLogging();
if (!tracingEnabled)
return null;
return asmxTraceSource;
}
}
/// <devdoc>
/// <para>Sets up internal config settings for logging. (MUST be called under critsec) </para>
/// </devdoc>
private static void InitializeLogging() {
lock (InternalSyncObject) {
if (!tracingInitialized) {
bool loggingEnabled = false;
asmxTraceSource = new TraceSource(TraceSourceAsmx);
if (asmxTraceSource.Switch.ShouldTrace(TraceEventType.Critical)) {
loggingEnabled = true;
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
currentDomain.DomainUnload += new EventHandler(AppDomainUnloadEvent);
currentDomain.ProcessExit += new EventHandler(ProcessExitEvent);
}
tracingEnabled = loggingEnabled;
tracingInitialized = true;
}
}
}
private static void Close() {
if (asmxTraceSource != null) {
asmxTraceSource.Close();
}
}
/// <devdoc>
/// <para>Logs any unhandled exception through this event handler</para>
/// </devdoc>
private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) {
Exception e = (Exception)args.ExceptionObject;
ExceptionCatch(TraceEventType.Error, sender, "UnhandledExceptionHandler", e);
}
private static void ProcessExitEvent(object sender, EventArgs e) {
Close();
appDomainShutdown = true;
}
/// <devdoc>
/// <para>Called when the system is shutting down, used to prevent additional logging post-shutdown</para>
/// </devdoc>
private static void AppDomainUnloadEvent(object sender, EventArgs e) {
Close();
appDomainShutdown = true;
}
/// <devdoc>
/// <para>Confirms logging is enabled, given current logging settings</para>
/// </devdoc>
private static bool ValidateSettings(TraceSource traceSource, TraceEventType traceLevel) {
if (!tracingEnabled) {
return false;
}
if (!tracingInitialized) {
InitializeLogging();
}
if (traceSource == null || !traceSource.Switch.ShouldTrace(traceLevel)) {
return false;
}
if (appDomainShutdown) {
return false;
}
return true;
}
internal static void Information(string format, params object[] args) {
if (!ValidateSettings(Asmx, TraceEventType.Information))
return;
TraceEvent(TraceEventType.Information, Res.GetString( format, args));
}
static void TraceEvent(TraceEventType eventType, string format) {
Asmx.TraceEvent(eventType, 0, format);
}
internal static Exception ExceptionThrow(TraceMethod method, Exception e) {
return ExceptionThrow(TraceEventType.Error, method, e);
}
internal static Exception ExceptionThrow(TraceEventType eventType, TraceMethod method, Exception e) {
if (!ValidateSettings(Asmx, eventType))
return e;
TraceEvent(eventType, Res.GetString(Res.TraceExceptionThrown, method.ToString(), e.GetType(), e.Message));
StackTrace(eventType, e);
return e;
}
internal static Exception ExceptionCatch(TraceMethod method, Exception e) {
return ExceptionCatch(TraceEventType.Error, method, e);
}
internal static Exception ExceptionCatch(TraceEventType eventType, TraceMethod method, Exception e) {
if (!ValidateSettings(Asmx, eventType))
return e;
TraceEvent(eventType, Res.GetString(Res.TraceExceptionCought, method, e.GetType(), e.Message));
StackTrace(eventType, e);
return e;
}
internal static Exception ExceptionCatch(TraceEventType eventType, object target, string method, Exception e) {
if (!ValidateSettings(Asmx, eventType))
return e;
TraceEvent(eventType, Res.GetString(Res.TraceExceptionCought, TraceMethod.MethodId(target, method), e.GetType(), e.Message));
StackTrace(eventType, e);
return e;
}
internal static Exception ExceptionIgnore(TraceEventType eventType, TraceMethod method, Exception e) {
if (!ValidateSettings(Asmx, eventType))
return e;
TraceEvent(eventType, Res.GetString(Res.TraceExceptionIgnored, method, e.GetType(), e.Message));
StackTrace(eventType, e);
return e;
}
static void StackTrace(TraceEventType eventType, Exception e) {
if (IsVerbose && !string.IsNullOrEmpty(e.StackTrace)) {
TraceEvent(eventType, Res.GetString(Res.TraceExceptionDetails, e.ToString()));
}
}
internal static string TraceId(string id) {
return Res.GetString(id);
}
static string GetHostByAddress(string ipAddress) {
try {
return Dns.GetHostByAddress(ipAddress).HostName;
}
catch {
return null;
}
}
internal static List<string> Details(HttpRequest request) {
if (request == null)
return null;
List<string> requestDetails = null;
requestDetails = new List<string>();
requestDetails.Add(Res.GetString(Res.TraceUserHostAddress, request.UserHostAddress));
string hostName = request.UserHostAddress == request.UserHostName ? GetHostByAddress(request.UserHostAddress) : request.UserHostName;
if (!string.IsNullOrEmpty(hostName))
requestDetails.Add(Res.GetString(Res.TraceUserHostName, hostName));
requestDetails.Add(Res.GetString(Res.TraceUrl, request.HttpMethod, request.Url));
if (request.UrlReferrer != null)
requestDetails.Add(Res.GetString(Res.TraceUrlReferrer, request.UrlReferrer));
return requestDetails;
}
internal static void Enter(string callId, TraceMethod caller) {
Enter(callId, caller, null, null);
}
internal static void Enter(string callId, TraceMethod caller, List<string> details) {
Enter(callId, caller, null, details);
}
internal static void Enter(string callId, TraceMethod caller, TraceMethod callDetails) {
Enter(callId, caller, callDetails, null);
}
internal static void Enter(string callId, TraceMethod caller, TraceMethod callDetails, List<string> details) {
if (!ValidateSettings(Asmx, TraceEventType.Information))
return;
string trace = callDetails == null ? Res.GetString(Res.TraceCallEnter, callId, caller) : Res.GetString(Res.TraceCallEnterDetails, callId, caller, callDetails);
if (details != null && details.Count > 0) {
StringBuilder sb = new StringBuilder(trace);
foreach (string detail in details) {
sb.Append(Environment.NewLine);
sb.Append(" ");
sb.Append(detail);
}
trace = sb.ToString();
}
TraceEvent(TraceEventType.Information, trace);
}
internal static XmlDeserializationEvents GetDeserializationEvents() {
XmlDeserializationEvents events = new XmlDeserializationEvents();
events.OnUnknownElement = new XmlElementEventHandler(OnUnknownElement);
events.OnUnknownAttribute = new XmlAttributeEventHandler(OnUnknownAttribute);
return events;
}
internal static void Exit(string callId, TraceMethod caller) {
if (!ValidateSettings(Asmx, TraceEventType.Information))
return;
TraceEvent(TraceEventType.Information, Res.GetString(Res.TraceCallExit, callId, caller));
}
internal static void OnUnknownElement(object sender, XmlElementEventArgs e) {
if (!ValidateSettings(Asmx, TraceEventType.Warning))
return;
if (e.Element == null)
return;
string xml = RuntimeUtils.ElementString(e.Element);
string format = e.ExpectedElements == null ? Res.WebUnknownElement : e.ExpectedElements.Length == 0 ? Res.WebUnknownElement1 : Res.WebUnknownElement2;
TraceEvent(TraceEventType.Warning, Res.GetString(format, xml, e.ExpectedElements));
}
internal static void OnUnknownAttribute(object sender, XmlAttributeEventArgs e) {
if (!ValidateSettings(Asmx, TraceEventType.Warning))
return;
if (e.Attr == null)
return;
// ignore attributes from known namepsaces
if (RuntimeUtils.IsKnownNamespace(e.Attr.NamespaceURI))
return;
string format = e.ExpectedAttributes == null ? Res.WebUnknownAttribute : e.ExpectedAttributes.Length == 0 ? Res.WebUnknownAttribute2 : Res.WebUnknownAttribute3;
TraceEvent(TraceEventType.Warning, Res.GetString(format, e.Attr.Name, e.Attr.Value, e.ExpectedAttributes));
}
}
internal class TraceMethod {
object target;
string name;
object[] args;
string call;
internal TraceMethod(object target, string name, params object[] args) {
this.target = target;
this.name = name;
this.args = args;
}
public override string ToString() {
if (call == null)
call = CallString(this.target, this.name, this.args);
return call;
}
internal static string CallString(object target, string method, params object[] args) {
StringBuilder sb = new StringBuilder();
WriteObjectId(sb, target);
sb.Append(':');
sb.Append(':');
sb.Append(method);
sb.Append('(');
for (int i = 0; i < args.Length; i++) {
object o = args[i];
WriteObjectId(sb, o);
if (o != null) {
sb.Append('=');
WriteValue(sb, o);
}
if (i + 1 < args.Length) {
sb.Append(',');
sb.Append(' ');
}
}
sb.Append(')');
return sb.ToString();
}
internal static string MethodId(object target, string method) {
StringBuilder sb = new StringBuilder();
WriteObjectId(sb, target);
sb.Append(':');
sb.Append(':');
sb.Append(method);
return sb.ToString();
}
static void WriteObjectId(StringBuilder sb, object o) {
if (o == null) {
sb.Append("(null)");
}
else if (o is Type) {
Type type = (Type)o;
sb.Append(type.FullName);
if (!(type.IsAbstract && type.IsSealed)) {
sb.Append('#');
sb.Append(HashString(o));
}
}
else {
sb.Append(o.GetType().FullName);
sb.Append('#');
sb.Append(HashString(o));
}
}
static void WriteValue(StringBuilder sb, object o) {
if (o == null) {
return;
}
if (o is string) {
sb.Append('"');
sb.Append(o);
sb.Append('"');
}
else {
Type type = o.GetType();
if (type.IsArray) {
sb.Append('[');
sb.Append(((Array)o).Length);
sb.Append(']');
}
else {
string value = o.ToString();
if (type.FullName == value) {
sb.Append('.');
sb.Append('.');
}
else {
sb.Append(value);
}
}
}
}
static string HashString(object objectValue) {
if (objectValue == null) {
return "(null)";
}
else {
return objectValue.GetHashCode().ToString(NumberFormatInfo.InvariantInfo);
}
}
}
}
|