How to output TraceOutputOptions values

Topics: Logging Application Block
Feb 24, 2012 at 5:09 AM

Hi all,

I am trying to use different types of listeners to output TraceOutputOptions values. I specified TraceOutputOptions using the attribute of each listener (i.e., traceOutputOptions="ThreadId"). However, these values can only be output by System Diagnostics Trace Listener. Other listeners don't output these values. Does anyone know the reason?

Actually I am just interested in the managed ThreadId value. Does anyone know how to acquire and output this value using enterprise library? 

Thanks in advance.

Feb 24, 2012 at 3:30 PM
Edited Feb 24, 2012 at 3:31 PM

You are correct -- the TraceOutputOptions is not supported by all Trace Listeners.  See the documentation at MSDN:

The TraceOptions enumeration is not used by the following classes and methods:

If you want to capture the ManagedThreadId (or any other additional data) for all Trace Listeners then you can use the ExtendedProperties property.  ExtendedProperties is a Dictionary that can store any additional data required.

To format the ExtendedProperties you can use the format string "{dictionary({key} - {value}{newline})".  This will dump all of the extended properties (with each on a new line).  Alternately you can just output the one property you are interested in: "ThreadId: {keyvalue(ThreadId)}".

Once you have a formatter set up to output the value then you need to add the information to the ExtendedProperties.  The code to do that would look something like this:


    LogEntry logEntry = new LogEntry()
        Message = "My message"

    var extendedProperties = new Dictionary<string, object>() 
        { "ManagedThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId }
    logEntry.ExtendedProperties = extendedProperties;

    LogWriter logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

The configuration using the Fluent interface would look like this (to output just the ManagedThreadId):

    var builder = new ConfigurationSourceBuilder();

                .SendTo.EventLog("Event Log Trace Listener")
                    .UsingEventLogSource("Enterprise Library Logging")
                    .FormatWith(new FormatterBuilder()
                    .TextFormatterNamed("Text Formatter")
                    .UsingTemplate("Timestamp: {timestamp}...{newline}ManagedThreadId: {keyvalue(ManagedThreadId)}"));
    var configSource = new DictionaryConfigurationSource();
        = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

Or if you are using a configuration file like this (to output all ExtendedProperties):

<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="Enterprise Library Logging" formatter="Text Formatter"
                log="" machineName="." traceOutputOptions="None" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
            <add switchValue="All" name="General">
                    <add name="Event Log Listener" />

Randy Levy
Enterprise Library support engineer