Home
Discussion Forums
Tutorials & Source Code
Developer Blogs
Developer Events
Developer Jobs
Labs
We need you!
We're working hard on the next version of Developer Fusion -
Let us know
what you think we should be up to!
The Labs
\
Source Viewer
\
SSCLI
\
System.Diagnostics
\ DiagnosticsConfiguration
Object Browser
Solution Explorer
SSCLI
Expand
Microsoft.Win32
Expand
Microsoft.Win32.SafeHandles
Expand
System
Expand
System.Runtime.Hosting
Expand
System.Runtime.InteropServices
Expand
System.Runtime.CompilerServices
Expand
System.Collections
Expand
System.Collections.Generic
Expand
System.Collections.ObjectModel
Expand
System.Configuration.Assemblies
Expand
System.Diagnostics
Expand
Assert
Expand
AssertFilter
Expand
AssertFilters
Expand
AssertSection
Expand
AssertWrapper
Expand
AsyncStreamReader
Expand
BooleanSwitch
Expand
ConditionalAttribute
Expand
ConsoleTraceListener
Expand
CorrelationManager
Expand
CounterSampleCalculator
Expand
DataReceivedEventArgs
Expand
Debug
Expand
DebuggableAttribute
Expand
Debugger
Expand
DebuggerBrowsableAttribute
Expand
DebuggerBrowsableState
Expand
DebuggerDisplayAttribute
Expand
DebuggerHiddenAttribute
Expand
DebuggerNonUserCodeAttribute
Expand
DebuggerStepperBoundaryAttribute
Expand
DebuggerStepThroughAttribute
Expand
DebuggerTypeProxyAttribute
Expand
DebuggerVisualizerAttribute
Expand
DebuggingModes
Expand
DefaultFilter
Expand
DefaultTraceListener
Expand
DelimitedListTraceListener
Expand
DiagnosticsConfiguration
AssertUIEnabled()
AutoFlush()
CanInitialize()
ConfigFilePath()
GetConfigSection()
IndentSize()
Initialize()
IsInitialized()
IsInitializing()
LogFileName()
Refresh()
SharedListeners()
Sources()
SwitchSettings()
SystemDiagnosticsSection()
UseGlobalLock()
Expand
DiagnosticsConfigurationHandler
Expand
EnvironmentBlock
Expand
EventTypeFilter
Expand
FilterElement
Expand
fred
Expand
InitState
Expand
ListenerElement
Expand
ListenerElementsCollection
Expand
Log
Expand
LoggingLevels
Expand
LogSwitch
Expand
ModuleInfo
Expand
MonitoringDescriptionAttribute
Expand
OrdinalCaseInsensitiveComparer
Expand
PerfCounterSection
Expand
Process
Expand
ProcessInfo
Expand
ProcessPriorityClass
Expand
ProcessStartInfo
Expand
ProcessThreadTimes
Expand
ProcessWaitHandle
Expand
ProcessWindowStyle
Expand
SharedListenerElementsCollection
Expand
SourceElement
Expand
SourceElementsCollection
Expand
SourceFilter
Expand
SourceLevels
Expand
SourceSwitch
Expand
StackFrame
Expand
StackFrameHelper
Expand
StackTrace
Expand
State
Expand
Stopwatch
Expand
StreamReadMode
Expand
Switch
Expand
SwitchAttribute
Expand
SwitchElement
Expand
SwitchElementsCollection
Expand
SwitchesDictionarySectionHandler
Expand
SwitchLevelAttribute
Expand
SystemDiagnosticsSection
Expand
TextWriterTraceListener
Expand
ThreadInfo
Expand
Trace
Expand
TraceEventCache
Expand
TraceEventType
Expand
TraceFilter
Expand
TraceFormat
Expand
TraceInternal
Expand
TraceLevel
Expand
TraceListener
Expand
TraceListenerCollection
Expand
TraceOptions
Expand
TraceSection
Expand
TraceSource
Expand
TraceSwitch
Expand
TraceUtils
Expand
TypedElement
Expand
XmlWriterTraceListener
Expand
System.Diagnostics.CodeAnalysis
Expand
System.Diagnostics.SymbolStore
Expand
System.Globalization
Expand
System.IO
Expand
System.IO.IsolatedStorage
Expand
System.Reflection
Expand
System.Reflection.Cache
Expand
System.Reflection.Emit
Expand
System.Resources
Expand
System.Runtime
Expand
System.Runtime.InteropServices.Expando
Expand
System.Runtime.ConstrainedExecution
Expand
System.Runtime.Remoting.Activation
Expand
System.Runtime.Remoting.Messaging
Expand
System.Runtime.Remoting.Channels
Expand
System.Runtime.Remoting.Lifetime
Expand
System.Runtime.Remoting
Expand
System.Runtime.Remoting.Contexts
Expand
System.Runtime.Remoting.Proxies
Expand
System.Runtime.Remoting.Metadata
Expand
System.Runtime.Remoting.Metadata.W3cXsd2001
Expand
System.Runtime.Remoting.Services
Expand
System.Runtime.Serialization
Expand
System.Runtime.Serialization.Formatters
Expand
System.Runtime.Serialization.Formatters.Binary
Expand
System.Runtime.Versioning
Expand
System.Security
Expand
System.Security.Cryptography
Expand
System.Security.Cryptography.X509Certificates
Expand
System.Security.Permissions
Expand
System.Security.Policy
Expand
System.Security.Principal
Expand
System.Security.Util
Expand
System.Text
Expand
System.Threading
Expand
System.Runtime.Remoting.Channels.Http
Expand
System.Runtime.Remoting.Channels.Tcp
Expand
System.Runtime.Remoting.MetadataServices
Expand
System.Runtime.Serialization.Formatters.Soap.Xml
Expand
System.Runtime.Serialization.Formatters.Soap
Expand
Microsoft.Tools.Caspol
Expand
Microsoft.Tools.Build
Expand
Microsoft.CSharp
Expand
System.CodeDom
Expand
System.CodeDom.Compiler
Expand
System.Collections.Specialized
Expand
System.ComponentModel
Expand
System.ComponentModel.Design
Expand
System.ComponentModel.Design.Serialization
Expand
System.Configuration
Expand
System.Web
Expand
System.Configuration.Provider
Expand
System.Configuration.Internal
Expand
System.Internal
Expand
System.Windows.Forms
Expand
System.Net
Expand
System.Net.Sockets
Expand
System.Net.NetworkInformation
Expand
System.Net.ComImports
Expand
System.Net.Security
Expand
System.Net.Cache
Expand
System.Net.Configuration
Expand
System.Text.RegularExpressions
Expand
System.Tools
Expand
System.IO.Compression
Expand
System.IO.Ports
Expand
System.Security.AccessControl
Expand
System.Xml
Expand
System.Xml.XPath.DataBinding
Expand
System.Xml.Schema
Expand
System.Xml.Serialization
Expand
System.Xml.Serialization.Advanced
Expand
System.Xml.Serialization.Configuration
Expand
System.Xml.XPath
Expand
System.Xml.Xsl
Expand
System.Xml.Xsl.IlGen
Expand
System.Xml.Xsl.Qil
Expand
System.Xml.Xsl.Runtime
Expand
System.Xml.Xsl.XPath
Expand
System.Xml.Xsl.Xslt
Expand
System.Xml.Xsl.XsltOld
Expand
System.Xml.Xsl.XsltOld.Debugger
Expand
Microsoft.JScript
Expand
Microsoft.Vsa
Expand
Microsoft.JScript.Vsa
C#
VB.NET
//------------------------------------------------------------------------------
// <copyright file="DiagnosticsConfiguration.cs" company="Microsoft">
//
// Copyright (c) 2006 Microsoft Corporation. All rights reserved.
//
// The use and distribution terms for this software are contained in the file
// named license.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// You must not remove this notice, or any other, from this software.
//
// </copyright>
//------------------------------------------------------------------------------
namespace
System.Diagnostics
{
using
System
;
using
System.Reflection
;
using
System.Collections
;
using
System.Configuration
;
using
System.Threading
;
using
System.Runtime.Versioning
;
internal
enum
InitState
{
NotInitialized,
Initializing,
Initialized
}
static
internal
class
DiagnosticsConfiguration
{
private
static
SystemDiagnosticsSection
configSection;
private
static
InitState
initState = InitState.NotInitialized;
// setting for Switch.switchSetting
static
internal
SwitchElementsCollection
SwitchSettings {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
)
return
configSectionSav.Switches;
else
return
null
;
}
}
// setting for DefaultTraceListener.AssertUIEnabled
static
internal
bool
AssertUIEnabled {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Assert !=
null
)
return
configSectionSav.Assert.AssertUIEnabled;
else
return
true
;
// the default
}
}
static
internal
string
ConfigFilePath {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
)
return
configSectionSav.ElementInformation.Source;
else
return
string
.Empty;
// the default
}
}
// setting for DefaultTraceListener.LogFileName
static
internal
string
LogFileName {
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Assert !=
null
)
return
configSectionSav.Assert.LogFileName;
else
return
string
.Empty;
// the default
}
}
// setting for TraceInternal.AutoFlush
static
internal
bool
AutoFlush {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Trace !=
null
)
return
configSectionSav.Trace.AutoFlush;
else
return
false
;
// the default
}
}
// setting for TraceInternal.UseGlobalLock
static
internal
bool
UseGlobalLock {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Trace !=
null
)
return
configSectionSav.Trace.UseGlobalLock;
else
return
true
;
// the default
}
}
// setting for TraceInternal.IndentSize
static
internal
int
IndentSize {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Trace !=
null
)
return
configSectionSav.Trace.IndentSize;
else
return
4;
// the default
}
}
static
internal
ListenerElementsCollection
SharedListeners {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
)
return
configSectionSav.SharedListeners;
else
return
null
;
}
}
static
internal
SourceElementsCollection
Sources {
get {
Initialize();
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
&& configSectionSav.Sources !=
null
)
return
configSectionSav.Sources;
else
return
null
;
}
}
static
internal
SystemDiagnosticsSection
SystemDiagnosticsSection {
get {
Initialize();
return
configSection;
}
}
private
static
SystemDiagnosticsSection
GetConfigSection()
{
SystemDiagnosticsSection
configSection = (
SystemDiagnosticsSection
)PrivilegedConfigurationManager.GetSection("system.diagnostics");
return
configSection;
}
static
internal
bool
IsInitializing()
{
return
initState == InitState.Initializing;
}
static
internal
bool
IsInitialized()
{
return
initState == InitState.Initialized;
}
static
internal
bool
CanInitialize()
{
return
(initState != InitState.Initializing) && !ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress;
}
static
internal
void
Initialize()
{
// Initialize() is also called by other components outside of Trace (such as PerformanceCounter)
// as a result using one lock for this critical section and another for Trace API critical sections
// (such as Trace.WriteLine) could potentially lead to deadlock between 2 threads that are
// executing these critical sections (and consequently obtaining the 2 locks) in the reverse order.
// Using the same lock for DiagnosticsConfiguration as well as TraceInternal avoids this issue.
// Sequential locks on TraceInternal.critSec by the same thread is a non issue for this critical section.
lock
(TraceInternal.critSec) {
// because some of the code used to load config also uses diagnostics
// we can't block them while we initialize from config. Therefore we just
// return immediately and they just use the default values.
if
(initState != InitState.NotInitialized || ConfigurationManagerInternalFactory.Instance.SetConfigurationSystemInProgress) {
return
;
}
initState = InitState.Initializing;
// used for preventing recursion
try
{
configSection = GetConfigSection();
}
finally
{
initState = InitState.Initialized;
}
}
}
static
internal
void
Refresh()
{
ConfigurationManager.RefreshSection("system.diagnostics");
// There might still be some persistant state left behind for
// ConfigPropertyCollection (for ex, swtichelements), probably for perf.
// We need to explicitly cleanup any unrecognized attributes that we
// have added during last deserialization, so that they are re-added
// during the next Config.GetSection properly and we get a chance to
// populate the Attributes collection for re-deserialization.
// Another alternative could be to expose the properties collection
// directly as Attributes collection (currently we keep a local
// hashtable which we explicitly need to keep in sycn and hence the
// cleanup logic below) but the down side of that would be we need to
// explicitly compute what is recognized Vs unrecognized from that
// collection when we expose the unrecognized Attributes publically
SystemDiagnosticsSection
configSectionSav = configSection;
if
(configSectionSav !=
null
) {
if
(configSectionSav.Switches !=
null
) {
foreach
(
SwitchElement
swelem
in
configSectionSav.Switches)
swelem.ResetProperties();
}
if
(configSectionSav.SharedListeners !=
null
) {
foreach
(
ListenerElement
lnelem
in
configSectionSav.SharedListeners)
lnelem.ResetProperties();
}
if
(configSectionSav.Sources !=
null
) {
foreach
(
SourceElement
srelem
in
configSectionSav.Sources)
srelem.ResetProperties();
}
}
configSection =
null
;
initState = InitState.NotInitialized;
Initialize();
}
}
}
Developer Fusion