Represents a source that you can use to generate trace output without associating it with a specific component or assembly. This is useful for generating trace output from non-managed code or when you want to trace events anonymously.
Syntax
// C#
public class TraceAnonymousSource : TraceSource
Inheritance Hierarchy
System.Object
System.Diagnostics.TraceSource
System.Diagnostics.TraceAnonymousSource
Remarks
The TraceAnonymousSource
class inherits from TraceSource
and provides a way to create trace output that is not directly linked to a specific source name or assembly. When you create an instance of TraceAnonymousSource
, it does not have a default name. You can assign a name to it by providing a string to the constructor.
This class is particularly useful when:
- You need to emit trace messages from code that is not part of your managed application, such as unmanaged code calling into managed code.
- You want to group trace messages from various, disparate sources under a single, identifiable trace listener, without having to configure individual trace sources for each.
- You are building a library and want to provide a simple way for consumers to trace events without forcing them to configure a specific
TraceSource
by name.
TraceAnonymousSource
without providing a name, it will use a default name that can be configured in the application's configuration file.
Constructors
TraceAnonymousSource()
Initializes a new instance of the TraceAnonymousSource
class with a default name.
public TraceAnonymousSource()
TraceAnonymousSource(string name)
Initializes a new instance of the TraceAnonymousSource
class with the specified name.
public TraceAnonymousSource(string name)
Parameters
Name | Description |
---|---|
name |
The name of the trace source. |
Methods
The TraceAnonymousSource
class inherits all methods from the TraceSource
class. You can use methods like TraceData
, TraceInformation
, TraceWarning
, etc., to emit trace output.
// Example Usage:
// Assuming 'anonymousSource' is an instance of TraceAnonymousSource
// In C#:
// TraceAnonymousSource anonymousSource = new TraceAnonymousSource("MyAnonymousTraces");
// anonymousSource.TraceInformation("This is an informational message.");
// anonymousSource.TraceEvent(TraceEventType.Warning, 1, "A potential issue occurred.");
// In XML configuration:
/*
<configuration>
<system.diagnostics>
<sources>
<add name="MyAnonymousTraces" switchValue="All" />
</sources>
<trace>
<listeners>
<add name="ConsoleListener" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
*/
Requirements
Feature | Description |
---|---|
Namespace | System.Diagnostics |
Assembly | System.dll |
.NET Framework Versions | Available in .NET Framework 4.5 and later versions. |