Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / System.Runtime.DurableInstancing / System / Runtime / ExceptionTrace.cs / 1305376 / ExceptionTrace.cs
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.Runtime
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Interop;
using System.Runtime.Versioning;
using System.Security;
using System.Runtime.Diagnostics;
class ExceptionTrace
{
const ushort FailFastEventLogCategory = 6;
string eventSourceName;
public ExceptionTrace(string eventSourceName)
{
this.eventSourceName = eventSourceName;
}
public void AsInformation(Exception exception)
{
//Traces an informational trace message
TraceCore.HandledException(Fx.Trace, exception);
}
public void AsWarning(Exception exception)
{
//Traces a warning trace message
TraceCore.HandledExceptionWarning(Fx.Trace, exception);
}
public Exception AsError(Exception exception)
{
return TraceException(exception);
}
public Exception AsError(Exception exception, string eventSource)
{
return TraceException(exception, eventSource);
}
public ArgumentException Argument(string paramName, string message)
{
return TraceException(new ArgumentException(message, paramName));
}
public ArgumentNullException ArgumentNull(string paramName)
{
return TraceException(new ArgumentNullException(paramName));
}
public ArgumentNullException ArgumentNull(string paramName, string message)
{
return TraceException(new ArgumentNullException(paramName, message));
}
public ArgumentException ArgumentNullOrEmpty(string paramName)
{
return this.Argument(paramName, SRCore.ArgumentNullOrEmpty(paramName));
}
public ArgumentOutOfRangeException ArgumentOutOfRange(string paramName, object actualValue, string message)
{
return TraceException(new ArgumentOutOfRangeException(paramName, actualValue, message));
}
// When throwing ObjectDisposedException, it is highly recommended that you use this ctor
// [C#]
// public ObjectDisposedException(string objectName, string message);
// And provide null for objectName but meaningful and relevant message for message.
// It is recommended because end user really does not care or can do anything on the disposed object, commonly an internal or private object.
public ObjectDisposedException ObjectDisposed(string message)
{
// pass in null, not disposedObject.GetType().FullName as per the above guideline
return TraceException(new ObjectDisposedException(null, message));
}
public void TraceUnhandledException(Exception exception)
{
TraceCore.UnhandledException(Fx.Trace, exception);
}
TException TraceException(TException exception)
where TException : Exception
{
return TraceException(exception, this.eventSourceName);
}
[ResourceConsumption(ResourceScope.Process)]
[Fx.Tag.SecurityNote(Critical = "Calls 'System.Runtime.Interop.UnsafeNativeMethods.IsDebuggerPresent()' which is a P/Invoke method",
Safe = "Does not leak any resource, needed for debugging")]
[SecuritySafeCritical]
TException TraceException(TException exception, string eventSource)
where TException : Exception
{
TraceCore.ThrowingException(Fx.Trace, eventSource, exception);
BreakOnException(exception);
return exception;
}
[Fx.Tag.SecurityNote(Critical = "Calls into critical method UnsafeNativeMethods.IsDebuggerPresent and UnsafeNativeMethods.DebugBreak",
Safe = "Safe because it's a no-op in retail builds.")]
[SecuritySafeCritical]
void BreakOnException(Exception exception)
{
#if DEBUG
if (Fx.BreakOnExceptionTypes != null)
{
foreach (Type breakType in Fx.BreakOnExceptionTypes)
{
if (breakType.IsAssignableFrom(exception.GetType()))
{
// This is intended to "crash" the process so that a debugger can be attached. If a managed
// debugger is already attached, it will already be able to hook these exceptions. We don't
// want to simulate an unmanaged crash (DebugBreak) in that case.
if (!Debugger.IsAttached && !UnsafeNativeMethods.IsDebuggerPresent())
{
UnsafeNativeMethods.DebugBreak();
}
}
}
}
#endif
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal void TraceFailFast(string message)
{
EventLogger logger = null;
#pragma warning disable 618
logger = new EventLogger(this.eventSourceName, Fx.Trace);
#pragma warning restore 618
TraceFailFast(message, logger);
}
// Generate an event Log entry for failfast purposes
// To force a Watson on a dev machine, do the following:
// 1. Set \HKLM\SOFTWARE\Microsoft\PCHealth\ErrorReporting ForceQueueMode = 0
// 2. In the command environment, set COMPLUS_DbgJitDebugLaunchSetting=0
[MethodImpl(MethodImplOptions.NoInlining)]
internal void TraceFailFast(string message, EventLogger logger)
{
if (logger != null)
{
try
{
string stackTrace = null;
try
{
stackTrace = new StackTrace().ToString();
}
catch (Exception exception)
{
stackTrace = exception.Message;
if (Fx.IsFatal(exception))
{
throw;
}
}
finally
{
logger.LogEvent(TraceEventType.Critical,
FailFastEventLogCategory,
(uint)EventLogEventId.FailFast,
message,
stackTrace);
}
}
catch (Exception ex)
{
logger.LogEvent(TraceEventType.Critical,
FailFastEventLogCategory,
(uint)EventLogEventId.FailFastException,
ex.ToString());
if (Fx.IsFatal(ex))
{
throw;
}
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------
namespace System.Runtime
{
using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Interop;
using System.Runtime.Versioning;
using System.Security;
using System.Runtime.Diagnostics;
class ExceptionTrace
{
const ushort FailFastEventLogCategory = 6;
string eventSourceName;
public ExceptionTrace(string eventSourceName)
{
this.eventSourceName = eventSourceName;
}
public void AsInformation(Exception exception)
{
//Traces an informational trace message
TraceCore.HandledException(Fx.Trace, exception);
}
public void AsWarning(Exception exception)
{
//Traces a warning trace message
TraceCore.HandledExceptionWarning(Fx.Trace, exception);
}
public Exception AsError(Exception exception)
{
return TraceException(exception);
}
public Exception AsError(Exception exception, string eventSource)
{
return TraceException(exception, eventSource);
}
public ArgumentException Argument(string paramName, string message)
{
return TraceException(new ArgumentException(message, paramName));
}
public ArgumentNullException ArgumentNull(string paramName)
{
return TraceException(new ArgumentNullException(paramName));
}
public ArgumentNullException ArgumentNull(string paramName, string message)
{
return TraceException(new ArgumentNullException(paramName, message));
}
public ArgumentException ArgumentNullOrEmpty(string paramName)
{
return this.Argument(paramName, SRCore.ArgumentNullOrEmpty(paramName));
}
public ArgumentOutOfRangeException ArgumentOutOfRange(string paramName, object actualValue, string message)
{
return TraceException(new ArgumentOutOfRangeException(paramName, actualValue, message));
}
// When throwing ObjectDisposedException, it is highly recommended that you use this ctor
// [C#]
// public ObjectDisposedException(string objectName, string message);
// And provide null for objectName but meaningful and relevant message for message.
// It is recommended because end user really does not care or can do anything on the disposed object, commonly an internal or private object.
public ObjectDisposedException ObjectDisposed(string message)
{
// pass in null, not disposedObject.GetType().FullName as per the above guideline
return TraceException(new ObjectDisposedException(null, message));
}
public void TraceUnhandledException(Exception exception)
{
TraceCore.UnhandledException(Fx.Trace, exception);
}
TException TraceException(TException exception)
where TException : Exception
{
return TraceException(exception, this.eventSourceName);
}
[ResourceConsumption(ResourceScope.Process)]
[Fx.Tag.SecurityNote(Critical = "Calls 'System.Runtime.Interop.UnsafeNativeMethods.IsDebuggerPresent()' which is a P/Invoke method",
Safe = "Does not leak any resource, needed for debugging")]
[SecuritySafeCritical]
TException TraceException(TException exception, string eventSource)
where TException : Exception
{
TraceCore.ThrowingException(Fx.Trace, eventSource, exception);
BreakOnException(exception);
return exception;
}
[Fx.Tag.SecurityNote(Critical = "Calls into critical method UnsafeNativeMethods.IsDebuggerPresent and UnsafeNativeMethods.DebugBreak",
Safe = "Safe because it's a no-op in retail builds.")]
[SecuritySafeCritical]
void BreakOnException(Exception exception)
{
#if DEBUG
if (Fx.BreakOnExceptionTypes != null)
{
foreach (Type breakType in Fx.BreakOnExceptionTypes)
{
if (breakType.IsAssignableFrom(exception.GetType()))
{
// This is intended to "crash" the process so that a debugger can be attached. If a managed
// debugger is already attached, it will already be able to hook these exceptions. We don't
// want to simulate an unmanaged crash (DebugBreak) in that case.
if (!Debugger.IsAttached && !UnsafeNativeMethods.IsDebuggerPresent())
{
UnsafeNativeMethods.DebugBreak();
}
}
}
}
#endif
}
[MethodImpl(MethodImplOptions.NoInlining)]
internal void TraceFailFast(string message)
{
EventLogger logger = null;
#pragma warning disable 618
logger = new EventLogger(this.eventSourceName, Fx.Trace);
#pragma warning restore 618
TraceFailFast(message, logger);
}
// Generate an event Log entry for failfast purposes
// To force a Watson on a dev machine, do the following:
// 1. Set \HKLM\SOFTWARE\Microsoft\PCHealth\ErrorReporting ForceQueueMode = 0
// 2. In the command environment, set COMPLUS_DbgJitDebugLaunchSetting=0
[MethodImpl(MethodImplOptions.NoInlining)]
internal void TraceFailFast(string message, EventLogger logger)
{
if (logger != null)
{
try
{
string stackTrace = null;
try
{
stackTrace = new StackTrace().ToString();
}
catch (Exception exception)
{
stackTrace = exception.Message;
if (Fx.IsFatal(exception))
{
throw;
}
}
finally
{
logger.LogEvent(TraceEventType.Critical,
FailFastEventLogCategory,
(uint)EventLogEventId.FailFast,
message,
stackTrace);
}
}
catch (Exception ex)
{
logger.LogEvent(TraceEventType.Critical,
FailFastEventLogCategory,
(uint)EventLogEventId.FailFastException,
ex.ToString());
if (Fx.IsFatal(ex))
{
throw;
}
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Mutex.cs
- StylusPointPropertyUnit.cs
- ExtensionSimplifierMarkupObject.cs
- SEHException.cs
- BaseTemplateParser.cs
- FixedSOMSemanticBox.cs
- Misc.cs
- TaskFormBase.cs
- TextFormatterImp.cs
- SystemTcpStatistics.cs
- XPathDocumentIterator.cs
- Documentation.cs
- AllMembershipCondition.cs
- GreaterThan.cs
- HandleRef.cs
- WebServiceHandler.cs
- GifBitmapDecoder.cs
- DisableDpiAwarenessAttribute.cs
- Vector3DAnimationBase.cs
- DocumentEventArgs.cs
- documentation.cs
- LogicalExpr.cs
- ColorIndependentAnimationStorage.cs
- codemethodreferenceexpression.cs
- BinaryObjectReader.cs
- ThousandthOfEmRealDoubles.cs
- ResourceDescriptionAttribute.cs
- UserPersonalizationStateInfo.cs
- XPathNodeList.cs
- UInt32Storage.cs
- SqlCommand.cs
- HandlerFactoryWrapper.cs
- MailHeaderInfo.cs
- CheckedPointers.cs
- ThreadStartException.cs
- ItemCheckEvent.cs
- ResourceDictionary.cs
- TypeConverterHelper.cs
- CharAnimationUsingKeyFrames.cs
- EntityClientCacheEntry.cs
- BooleanAnimationBase.cs
- StateRuntime.cs
- Int32Animation.cs
- CallbackValidatorAttribute.cs
- DbConnectionStringCommon.cs
- ReflectEventDescriptor.cs
- localization.cs
- ActiveDocumentEvent.cs
- Icon.cs
- EnvelopedSignatureTransform.cs
- XPathDescendantIterator.cs
- JournalEntry.cs
- Command.cs
- KeyInstance.cs
- HandleCollector.cs
- TextBoxAutoCompleteSourceConverter.cs
- HtmlButton.cs
- StickyNoteAnnotations.cs
- AppModelKnownContentFactory.cs
- BaseAsyncResult.cs
- PointF.cs
- QilCloneVisitor.cs
- HasCopySemanticsAttribute.cs
- CacheEntry.cs
- ActiveXSerializer.cs
- TagPrefixAttribute.cs
- SerializationFieldInfo.cs
- TraceContextEventArgs.cs
- PlanCompiler.cs
- translator.cs
- AdornerHitTestResult.cs
- FormsIdentity.cs
- LayoutEngine.cs
- unsafeIndexingFilterStream.cs
- Slider.cs
- FlowDocumentView.cs
- SynchronizationFilter.cs
- StructuralObject.cs
- MessageQueueInstaller.cs
- ApplyTemplatesAction.cs
- MailAddress.cs
- CodeValidator.cs
- OleAutBinder.cs
- ToolboxCategoryItems.cs
- SettingsSavedEventArgs.cs
- AsyncPostBackTrigger.cs
- SmtpDigestAuthenticationModule.cs
- AspNetRouteServiceHttpHandler.cs
- ComboBoxAutomationPeer.cs
- ParentQuery.cs
- DataGridItemEventArgs.cs
- LinkedResource.cs
- TemplateKeyConverter.cs
- CodeCompiler.cs
- WindowsSpinner.cs
- CursorConverter.cs
- COM2TypeInfoProcessor.cs
- CompoundFileReference.cs
- WindowManager.cs
- TargetControlTypeAttribute.cs