File size: 21,888 Bytes
8c763fb | 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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#define TRACE
using System.Reflection;
using System.Management.Automation.Internal;
namespace System.Management.Automation
{
/// <summary>
/// An PSTraceSource is a representation of a System.Diagnostics.TraceSource instance
/// that is used in the PowerShell components to produce trace output.
/// </summary>
/// <remarks>
/// It is permitted to subclass <see cref="PSTraceSource"/>
/// but there is no established scenario for doing this, nor has it been tested.
/// </remarks>
/// <!--
/// IF YOU ARE NOT PART OF THE PowerShell DEVELOPMENT TEAM PLEASE
/// DO NOT USE THIS CLASS!!!!!
///
/// The PSTraceSource class is derived from Switch to provide granular
/// control over the tracing in a program. An instance of PSTraceSource
/// is created for each category of tracing such that separate flags
/// (filters) can be set. Each flag enables one or more method for tracing.
///
/// For instance, the Exception flag will enable tracing on these methods:
/// TraceException.
/// </summary>
/// <remarks>
/// To get an instance of this class a user should define a public static
/// field of the type PSTraceSource, decorated it with an attribute of
/// PSTraceSourceAttribute, and assign the results of GetTracer to it.
/// <newpara/>
/// <example>
/// <code>
/// [PSTraceSourceAttribute("category", "description")]
/// public static PSTraceSource tracer = GetTracer("category", "description");
/// </code>
/// </example>
/// <newpara/>
/// Other than initial creation of this class through the GetTracer method,
/// this class should throw no exceptions. Any call to a PSTraceSource method
/// that results in an exception being thrown will be ignored.
/// -->
public partial class PSTraceSource
{
/// <summary>
/// Lock object for the GetTracer method.
/// </summary>
private static readonly object s_getTracerLock = new object();
/// <summary>
/// A helper to get an instance of the PSTraceSource class.
/// </summary>
/// <param name="name">
/// The name of the category that this class
/// will control the tracing for.
/// </param>
/// <param name="description">
/// The description to describe what the category
/// is used for.
/// </param>
/// <returns>
/// An instance of the PSTraceSource class which is initialized
/// to trace for the specified category. If multiple callers ask for the same category,
/// the same PSTraceSource will be returned.
/// </returns>
internal static PSTraceSource GetTracer(
string name,
string description)
{
return PSTraceSource.GetTracer(name, description, true);
}
/// <summary>
/// A helper to get an instance of the PSTraceSource class.
/// </summary>
/// <param name="name">
/// The name of the category that this class
/// will control the tracing for.
/// </param>
/// <param name="description">
/// The description to describe what the category
/// is used for.
/// </param>
/// <param name="traceHeaders">
/// If true, the line headers will be traced, if false, only the trace message will be traced.
/// </param>
/// <returns>
/// An instance of the PSTraceSource class which is initialized
/// to trace for the specified category. If multiple callers ask for the same category,
/// the same PSTraceSource will be returned.
/// </returns>
internal static PSTraceSource GetTracer(
string name,
string description,
bool traceHeaders)
{
ArgumentException.ThrowIfNullOrEmpty(name);
lock (PSTraceSource.s_getTracerLock)
{
PSTraceSource result = null;
// See if we can find an PSTraceSource for this category in the catalog.
PSTraceSource.TraceCatalog.TryGetValue(name, out result);
// If it's not already in the catalog, see if we can find it in the
// pre-configured trace source list
if (result == null)
{
string keyName = name;
if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(keyName))
{
if (keyName.Length > 16)
{
keyName = keyName.Substring(0, 16);
if (!PSTraceSource.PreConfiguredTraceSource.ContainsKey(keyName))
{
keyName = null;
}
}
else
{
keyName = null;
}
}
if (keyName != null)
{
// Get the pre-configured trace source from the catalog
PSTraceSource preconfiguredSource = PSTraceSource.PreConfiguredTraceSource[keyName];
result = PSTraceSource.GetNewTraceSource(keyName, description, traceHeaders);
result.Options = preconfiguredSource.Options;
result.Listeners.Clear();
result.Listeners.AddRange(preconfiguredSource.Listeners);
// Add it to the TraceCatalog
PSTraceSource.TraceCatalog.Add(keyName, result);
// Remove it from the pre-configured catalog
PSTraceSource.PreConfiguredTraceSource.Remove(keyName);
}
}
// Even if there was a PSTraceSource in the catalog, let's replace
// it with an PSTraceSource to get the added functionality. Anyone using
// a StructuredTraceSource should be able to do so even with the PSTraceSource
// instance.
if (result == null)
{
result = PSTraceSource.GetNewTraceSource(name, description, traceHeaders);
PSTraceSource.TraceCatalog[result.FullName] = result;
}
if (result.Options != PSTraceSourceOptions.None &&
traceHeaders)
{
result.TraceGlobalAppDomainHeader();
// Trace the object specific tracer information
result.TracerObjectHeader(Assembly.GetCallingAssembly());
}
return result;
}
}
internal static PSTraceSource GetNewTraceSource(
string name,
string description,
bool traceHeaders)
{
// Note, all callers should have already verified the name before calling this
// API, so this exception should never be exposed to an end-user.
ArgumentException.ThrowIfNullOrEmpty(name);
// Keep the fullName as it was passed, but truncate or pad
// the category name to 16 characters. This allows for
// uniform output
string fullName = name;
/*
// This is here to ensure all the trace category names are 16 characters,
// the problem is that the app-config file would need to contain the same
// trailing spaces if this actually does pad the name.
name =
string.Format(
System.Globalization.CultureInfo.InvariantCulture,
"{0,-16}",
name);
*/
PSTraceSource result =
new PSTraceSource(
fullName,
name,
description,
traceHeaders);
return result;
}
#region TraceFlags.New*Exception methods/helpers
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This is not allowed to call other
/// Throw*Exception variants, since they call this.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was null
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentNullException NewArgumentNullException(string paramName)
{
ArgumentException.ThrowIfNullOrEmpty(paramName);
string message = StringUtil.Format(AutomationExceptions.ArgumentNull, paramName);
var e = new PSArgumentNullException(paramName, message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant allows the caller to
/// specify alternate template text, but only in assembly S.M.A.Core.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was invalid
/// </param>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentNullException NewArgumentNullException(
string paramName, string resourceString, params object[] args)
{
if (string.IsNullOrEmpty(paramName))
{
throw NewArgumentNullException(nameof(paramName));
}
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
// Note that the paramName param comes first
var e = new PSArgumentNullException(paramName, message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant uses the default
/// ArgumentException template text. This is not allowed to call
/// other Throw*Exception variants, since they call this.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was invalid
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentException NewArgumentException(string paramName)
{
ArgumentException.ThrowIfNullOrEmpty(paramName);
string message = StringUtil.Format(AutomationExceptions.Argument, paramName);
// Note that the message param comes first
var e = new PSArgumentException(message, paramName);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant allows the caller to
/// specify alternate template text, but only in assembly S.M.A.Core.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was invalid
/// </param>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentException NewArgumentException(
string paramName, string resourceString, params object[] args)
{
if (string.IsNullOrEmpty(paramName))
{
throw NewArgumentNullException(nameof(paramName));
}
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
// Note that the message param comes first
var e = new PSArgumentException(message, paramName);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception.
/// </summary>
/// <returns>Exception instance ready to throw.</returns>
internal static PSInvalidOperationException NewInvalidOperationException()
{
string message = StringUtil.Format(AutomationExceptions.InvalidOperation,
new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name);
var e = new PSInvalidOperationException(message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant allows the caller to
/// specify alternate template text, but only in assembly S.M.A.Core.
/// </summary>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSInvalidOperationException NewInvalidOperationException(
string resourceString, params object[] args)
{
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
var e = new PSInvalidOperationException(message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant allows the caller to
/// specify alternate template text, but only in assembly S.M.A.Core.
/// </summary>
/// <param name="innerException">
/// This is the InnerException for the InvalidOperationException
/// </param>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSInvalidOperationException NewInvalidOperationException(
Exception innerException,
string resourceString, params object[] args)
{
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
var e = new PSInvalidOperationException(message, innerException);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This is not allowed to call other
/// Throw*Exception variants, since they call this.
/// </summary>
/// <returns>Exception instance ready to throw.</returns>
internal static PSNotSupportedException NewNotSupportedException()
{
string message = StringUtil.Format(AutomationExceptions.NotSupported,
new System.Diagnostics.StackTrace().GetFrame(0).ToString());
var e = new PSNotSupportedException(message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This is not allowed to call other
/// Throw*Exception variants, since they call this.
/// </summary>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSNotSupportedException NewNotSupportedException(
string resourceString,
params object[] args)
{
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
var e = new PSNotSupportedException(message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This is not allowed to call other
/// Throw*Exception variants, since they call this.
/// </summary>
/// <returns>Exception instance ready to throw.</returns>
internal static PSNotImplementedException NewNotImplementedException()
{
string message = StringUtil.Format(AutomationExceptions.NotImplemented,
new System.Diagnostics.StackTrace().GetFrame(0).ToString());
var e = new PSNotImplementedException(message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant uses the default
/// ArgumentOutOfRangeException template text. This is not allowed to call
/// other Throw*Exception variants, since they call this.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was out of range
/// </param>
/// <param name="actualValue">
/// The value of the argument causing the exception
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentOutOfRangeException NewArgumentOutOfRangeException(string paramName, object actualValue)
{
ArgumentException.ThrowIfNullOrEmpty(paramName);
string message = StringUtil.Format(AutomationExceptions.ArgumentOutOfRange, paramName);
var e = new PSArgumentOutOfRangeException(paramName, actualValue, message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant allows the caller to
/// specify alternate template text, but only in assembly S.M.A.Core.
/// </summary>
/// <param name="paramName">
/// The name of the parameter whose argument value was invalid
/// </param>
/// <param name="actualValue">
/// The value of the argument causing the exception
/// </param>
/// <param name="resourceString">
/// The template string for this error
/// </param>
/// <param name="args">
/// Objects corresponding to {0}, {1}, etc. in the resource string
/// </param>
/// <returns>Exception instance ready to throw.</returns>
internal static PSArgumentOutOfRangeException NewArgumentOutOfRangeException(
string paramName, object actualValue, string resourceString, params object[] args)
{
if (string.IsNullOrEmpty(paramName))
{
throw NewArgumentNullException(nameof(paramName));
}
if (string.IsNullOrEmpty(resourceString))
{
throw NewArgumentNullException(nameof(resourceString));
}
string message = StringUtil.Format(resourceString, args);
var e = new PSArgumentOutOfRangeException(paramName, actualValue, message);
return e;
}
/// <summary>
/// Traces the Message and StackTrace properties of the exception
/// and returns the new exception. This variant uses the default
/// ObjectDisposedException template text. This is not allowed to call
/// other Throw*Exception variants, since they call this.
/// </summary>
/// <param name="objectName">
/// The name of the disposed object
/// </param>
/// <returns>Exception instance ready to throw.</returns>
/// <remarks>
/// Note that the parameter is the object name and not the message.
/// </remarks>
internal static PSObjectDisposedException NewObjectDisposedException(string objectName)
{
if (string.IsNullOrEmpty(objectName))
{
throw NewArgumentNullException(nameof(objectName));
}
string message = StringUtil.Format(AutomationExceptions.ObjectDisposed, objectName);
var e = new PSObjectDisposedException(objectName, message);
return e;
}
#endregion TraceFlags.New*Exception methods/helpers
}
}
|