Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / CompMod / System / Diagnostics / DefaultTraceListener.cs / 1 / DefaultTraceListener.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#define DEBUG
#define TRACE
namespace System.Diagnostics {
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using Microsoft.Win32;
using System.Globalization;
using System.Runtime.Versioning;
///
/// Provides
/// the default output methods and behavior for tracing.
///
[HostProtection(Synchronization=true)]
public class DefaultTraceListener : TraceListener {
bool assertUIEnabled;
string logFileName;
bool settingsInitialized;
const int internalWriteSize = 16384;
///
/// Initializes a new instance of the class with
/// Default as its .
///
public DefaultTraceListener()
: base("Default") {
}
///
/// [To be supplied.]
///
public bool AssertUiEnabled {
get {
if (!settingsInitialized) InitializeSettings();
return assertUIEnabled;
}
set {
if (!settingsInitialized) InitializeSettings();
assertUIEnabled = value;
}
}
///
/// [To be supplied.]
///
public string LogFileName {
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get {
if (!settingsInitialized) InitializeSettings();
return logFileName;
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
set {
if (!settingsInitialized) InitializeSettings();
logFileName = value;
}
}
///
///
/// Emits or displays a message
/// and a stack trace for an assertion that
/// always fails.
///
///
public override void Fail(string message) {
Fail(message, null);
}
///
///
/// Emits or displays messages and a stack trace for an assertion that
/// always fails.
///
///
public override void Fail(string message, string detailMessage) {
StackTrace stack = new StackTrace(true);
int userStackFrameIndex = 0;
string stackTrace;
bool uiPermission = UiPermission;
try {
stackTrace = StackTraceToString(stack, userStackFrameIndex, stack.FrameCount - 1);
}
catch {
stackTrace = "";
}
WriteAssert(stackTrace, message, detailMessage);
if (AssertUiEnabled && uiPermission) {
AssertWrapper.ShowAssert(stackTrace, stack.GetFrame(userStackFrameIndex), message, detailMessage);
}
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
private void InitializeSettings() {
// don't use the property setters here to avoid infinite recursion.
assertUIEnabled = DiagnosticsConfiguration.AssertUIEnabled;
logFileName = DiagnosticsConfiguration.LogFileName;
settingsInitialized = true;
}
private void WriteAssert(string stackTrace, string message, string detailMessage) {
string assertMessage = SR.GetString(SR.DebugAssertBanner) + "\r\n"
+ SR.GetString(SR.DebugAssertShortMessage) + "\r\n"
+ message + "\r\n"
+ SR.GetString(SR.DebugAssertLongMessage) + "\r\n" +
detailMessage + "\r\n"
+ stackTrace;
WriteLine(assertMessage);
}
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
private void WriteToLogFile(string message, bool useWriteLine) {
try {
FileInfo file = new FileInfo(LogFileName);
using (Stream stream = file.Open(FileMode.OpenOrCreate)) {
using (StreamWriter writer = new StreamWriter(stream)) {
stream.Position = stream.Length;
if (useWriteLine)
writer.WriteLine(message);
else
writer.Write(message);
}
}
}
catch (Exception e) {
WriteLine(SR.GetString(SR.ExceptionOccurred, LogFileName, e.ToString()), false);
}
catch {
WriteLine(SR.GetString(SR.ExceptionOccurred, LogFileName, ""), false);
}
}
// Given a stack trace and start and end frame indexes, construct a
// callstack that contains method, file and line number information.
private string StackTraceToString(StackTrace trace, int startFrameIndex, int endFrameIndex) {
StringBuilder sb = new StringBuilder(512);
for (int i = startFrameIndex; i <= endFrameIndex; i++) {
StackFrame frame = trace.GetFrame(i);
MethodBase method = frame.GetMethod();
sb.Append("\r\n at ");
if (method.ReflectedType != null) {
sb.Append(method.ReflectedType.Name);
}
else {
// This is for global methods and this is what shows up in windbg.
sb.Append("");
}
sb.Append(".");
sb.Append(method.Name);
sb.Append("(");
ParameterInfo[] parameters = method.GetParameters();
for (int j = 0; j < parameters.Length; j++) {
ParameterInfo parameter = parameters[j];
if (j > 0)
sb.Append(", ");
sb.Append(parameter.ParameterType.Name);
sb.Append(" ");
sb.Append(parameter.Name);
}
sb.Append(") ");
sb.Append(frame.GetFileName());
int line = frame.GetFileLineNumber();
if (line > 0) {
sb.Append("(");
sb.Append(line.ToString(CultureInfo.InvariantCulture));
sb.Append(")");
}
}
sb.Append("\r\n");
return sb.ToString();
}
///
///
/// Writes the output to the OutputDebugString
/// API and
/// to System.Diagnostics.Debugger.Log.
///
///
public override void Write(string message) {
Write(message, true);
}
private void Write(string message, bool useLogFile) {
if (NeedIndent) WriteIndent();
// really huge messages mess up both VS and dbmon, so we chop it up into
// reasonable chunks if it's too big
if (message == null || message.Length <= internalWriteSize) {
internalWrite(message);
}
else {
int offset;
for (offset = 0; offset < message.Length - internalWriteSize; offset += internalWriteSize) {
internalWrite(message.Substring(offset, internalWriteSize));
}
internalWrite(message.Substring(offset));
}
if (useLogFile && LogFileName.Length != 0)
WriteToLogFile(message, false);
}
void internalWrite(string message) {
if (Debugger.IsLogging()) {
Debugger.Log(0, null, message);
} else {
if (message == null)
SafeNativeMethods.OutputDebugString(String.Empty);
else
SafeNativeMethods.OutputDebugString(message);
}
}
///
///
/// Writes the output to the OutputDebugString
/// API and to System.Diagnostics.Debugger.Log
/// followed by a line terminator. The default line terminator is a carriage return
/// followed by a line feed (\r\n).
///
///
public override void WriteLine(string message) {
WriteLine(message, true);
}
private void WriteLine(string message, bool useLogFile) {
if (NeedIndent) WriteIndent();
// I do the concat here to make sure it goes as one call to the output.
// we would save a stringbuilder operation by calling Write twice.
Write(message + "\r\n", useLogFile);
NeedIndent = true;
}
///
/// It returns true if the current permission set allows an assert dialog to be displayed.
///
private static bool UiPermission {
get {
bool uiPermission = false;
try {
new UIPermission(UIPermissionWindow.SafeSubWindows).Demand();
uiPermission = true;
}
catch {
}
return uiPermission;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#define DEBUG
#define TRACE
namespace System.Diagnostics {
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using Microsoft.Win32;
using System.Globalization;
using System.Runtime.Versioning;
///
/// Provides
/// the default output methods and behavior for tracing.
///
[HostProtection(Synchronization=true)]
public class DefaultTraceListener : TraceListener {
bool assertUIEnabled;
string logFileName;
bool settingsInitialized;
const int internalWriteSize = 16384;
///
/// Initializes a new instance of the class with
/// Default as its .
///
public DefaultTraceListener()
: base("Default") {
}
///
/// [To be supplied.]
///
public bool AssertUiEnabled {
get {
if (!settingsInitialized) InitializeSettings();
return assertUIEnabled;
}
set {
if (!settingsInitialized) InitializeSettings();
assertUIEnabled = value;
}
}
///
/// [To be supplied.]
///
public string LogFileName {
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get {
if (!settingsInitialized) InitializeSettings();
return logFileName;
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
set {
if (!settingsInitialized) InitializeSettings();
logFileName = value;
}
}
///
///
/// Emits or displays a message
/// and a stack trace for an assertion that
/// always fails.
///
///
public override void Fail(string message) {
Fail(message, null);
}
///
///
/// Emits or displays messages and a stack trace for an assertion that
/// always fails.
///
///
public override void Fail(string message, string detailMessage) {
StackTrace stack = new StackTrace(true);
int userStackFrameIndex = 0;
string stackTrace;
bool uiPermission = UiPermission;
try {
stackTrace = StackTraceToString(stack, userStackFrameIndex, stack.FrameCount - 1);
}
catch {
stackTrace = "";
}
WriteAssert(stackTrace, message, detailMessage);
if (AssertUiEnabled && uiPermission) {
AssertWrapper.ShowAssert(stackTrace, stack.GetFrame(userStackFrameIndex), message, detailMessage);
}
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
private void InitializeSettings() {
// don't use the property setters here to avoid infinite recursion.
assertUIEnabled = DiagnosticsConfiguration.AssertUIEnabled;
logFileName = DiagnosticsConfiguration.LogFileName;
settingsInitialized = true;
}
private void WriteAssert(string stackTrace, string message, string detailMessage) {
string assertMessage = SR.GetString(SR.DebugAssertBanner) + "\r\n"
+ SR.GetString(SR.DebugAssertShortMessage) + "\r\n"
+ message + "\r\n"
+ SR.GetString(SR.DebugAssertLongMessage) + "\r\n" +
detailMessage + "\r\n"
+ stackTrace;
WriteLine(assertMessage);
}
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
private void WriteToLogFile(string message, bool useWriteLine) {
try {
FileInfo file = new FileInfo(LogFileName);
using (Stream stream = file.Open(FileMode.OpenOrCreate)) {
using (StreamWriter writer = new StreamWriter(stream)) {
stream.Position = stream.Length;
if (useWriteLine)
writer.WriteLine(message);
else
writer.Write(message);
}
}
}
catch (Exception e) {
WriteLine(SR.GetString(SR.ExceptionOccurred, LogFileName, e.ToString()), false);
}
catch {
WriteLine(SR.GetString(SR.ExceptionOccurred, LogFileName, ""), false);
}
}
// Given a stack trace and start and end frame indexes, construct a
// callstack that contains method, file and line number information.
private string StackTraceToString(StackTrace trace, int startFrameIndex, int endFrameIndex) {
StringBuilder sb = new StringBuilder(512);
for (int i = startFrameIndex; i <= endFrameIndex; i++) {
StackFrame frame = trace.GetFrame(i);
MethodBase method = frame.GetMethod();
sb.Append("\r\n at ");
if (method.ReflectedType != null) {
sb.Append(method.ReflectedType.Name);
}
else {
// This is for global methods and this is what shows up in windbg.
sb.Append("");
}
sb.Append(".");
sb.Append(method.Name);
sb.Append("(");
ParameterInfo[] parameters = method.GetParameters();
for (int j = 0; j < parameters.Length; j++) {
ParameterInfo parameter = parameters[j];
if (j > 0)
sb.Append(", ");
sb.Append(parameter.ParameterType.Name);
sb.Append(" ");
sb.Append(parameter.Name);
}
sb.Append(") ");
sb.Append(frame.GetFileName());
int line = frame.GetFileLineNumber();
if (line > 0) {
sb.Append("(");
sb.Append(line.ToString(CultureInfo.InvariantCulture));
sb.Append(")");
}
}
sb.Append("\r\n");
return sb.ToString();
}
///
///
/// Writes the output to the OutputDebugString
/// API and
/// to System.Diagnostics.Debugger.Log.
///
///
public override void Write(string message) {
Write(message, true);
}
private void Write(string message, bool useLogFile) {
if (NeedIndent) WriteIndent();
// really huge messages mess up both VS and dbmon, so we chop it up into
// reasonable chunks if it's too big
if (message == null || message.Length <= internalWriteSize) {
internalWrite(message);
}
else {
int offset;
for (offset = 0; offset < message.Length - internalWriteSize; offset += internalWriteSize) {
internalWrite(message.Substring(offset, internalWriteSize));
}
internalWrite(message.Substring(offset));
}
if (useLogFile && LogFileName.Length != 0)
WriteToLogFile(message, false);
}
void internalWrite(string message) {
if (Debugger.IsLogging()) {
Debugger.Log(0, null, message);
} else {
if (message == null)
SafeNativeMethods.OutputDebugString(String.Empty);
else
SafeNativeMethods.OutputDebugString(message);
}
}
///
///
/// Writes the output to the OutputDebugString
/// API and to System.Diagnostics.Debugger.Log
/// followed by a line terminator. The default line terminator is a carriage return
/// followed by a line feed (\r\n).
///
///
public override void WriteLine(string message) {
WriteLine(message, true);
}
private void WriteLine(string message, bool useLogFile) {
if (NeedIndent) WriteIndent();
// I do the concat here to make sure it goes as one call to the output.
// we would save a stringbuilder operation by calling Write twice.
Write(message + "\r\n", useLogFile);
NeedIndent = true;
}
///
/// It returns true if the current permission set allows an assert dialog to be displayed.
///
private static bool UiPermission {
get {
bool uiPermission = false;
try {
new UIPermission(UIPermissionWindow.SafeSubWindows).Demand();
uiPermission = true;
}
catch {
}
return uiPermission;
}
}
}
}
// 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
- TdsParameterSetter.cs
- MimeTypeMapper.cs
- QueryContinueDragEvent.cs
- KeyToListMap.cs
- EndPoint.cs
- ControlParser.cs
- ResourceDescriptionAttribute.cs
- ToolStripGripRenderEventArgs.cs
- RTLAwareMessageBox.cs
- CodeAttachEventStatement.cs
- SqlDataSourceFilteringEventArgs.cs
- GenericTextProperties.cs
- metadatamappinghashervisitor.hashsourcebuilder.cs
- Compress.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- MessageQueue.cs
- MemberPath.cs
- TypeUtil.cs
- KeyValueSerializer.cs
- ExpressionEvaluator.cs
- HttpModulesSection.cs
- UpdateExpressionVisitor.cs
- HttpApplicationFactory.cs
- PropertyGeneratedEventArgs.cs
- DbMetaDataCollectionNames.cs
- TextAutomationPeer.cs
- CultureTableRecord.cs
- SplitterEvent.cs
- InvokeDelegate.cs
- QilTargetType.cs
- sqlcontext.cs
- SpeechRecognizer.cs
- MouseWheelEventArgs.cs
- CqlParser.cs
- UiaCoreTypesApi.cs
- EntityCommandCompilationException.cs
- ListViewGroupConverter.cs
- StylusPointPropertyUnit.cs
- HttpWebRequestElement.cs
- FlagsAttribute.cs
- Script.cs
- ProviderBase.cs
- FormViewDeleteEventArgs.cs
- Trigger.cs
- GridPatternIdentifiers.cs
- RawTextInputReport.cs
- HotCommands.cs
- TraceUtility.cs
- XmlElement.cs
- _RequestCacheProtocol.cs
- TextContainerChangeEventArgs.cs
- oledbmetadatacolumnnames.cs
- DerivedKeySecurityToken.cs
- EntityException.cs
- ImageMapEventArgs.cs
- WebPartEditorCancelVerb.cs
- Win32.cs
- EventLogPermissionEntry.cs
- ToolStripScrollButton.cs
- PolyQuadraticBezierSegment.cs
- Point4D.cs
- MemberInfoSerializationHolder.cs
- ExtenderControl.cs
- HtmlTableCellCollection.cs
- ToolStripControlHost.cs
- RNGCryptoServiceProvider.cs
- DocumentsTrace.cs
- CodeArgumentReferenceExpression.cs
- CroppedBitmap.cs
- DocumentViewerConstants.cs
- HScrollBar.cs
- PageContentCollection.cs
- PartialCachingAttribute.cs
- OleDbReferenceCollection.cs
- SetState.cs
- InputScope.cs
- Camera.cs
- InkCanvasInnerCanvas.cs
- TraceSection.cs
- coordinatorscratchpad.cs
- MinMaxParagraphWidth.cs
- ObjectViewQueryResultData.cs
- HTTPNotFoundHandler.cs
- FragmentNavigationEventArgs.cs
- DataGridViewTextBoxCell.cs
- ListBindingConverter.cs
- ArrayElementGridEntry.cs
- Point3DCollection.cs
- HandlerBase.cs
- PropertyEmitter.cs
- NativeCppClassAttribute.cs
- TypedTableBase.cs
- WebPart.cs
- ItemsControlAutomationPeer.cs
- CultureInfoConverter.cs
- PartBasedPackageProperties.cs
- SafeThemeHandle.cs
- DataKey.cs
- BaseCodePageEncoding.cs
- XmlNamespaceDeclarationsAttribute.cs