How to use Logging Application Block to write to output window?

Mar 30, 2012 at 1:13 AM

I downloaded Enterprise Library 5.0 and want to use Logging App Block to write trace messages and debug messages to output window or DebugView. Can I do it without creating a Custom Trace Listener? Can I use System Diagnostics Trace Listener to fulfill my purpose?

Thanks,

Cindy

Mar 30, 2012 at 1:47 AM

Yes.  Since Enterprise Library is built on the System.Diagnostics Trace Listeners you can use existing system trace listeners with Enterprise Library.  

The trace listener you want to use is the System.Diagnostics.DefaultTraceLisener.  To use it you will wrap it in the System Diagnostics Trace Listener.  You can use the configuration tool to do this.  The resulting configuration looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                type="System.Diagnostics.DefaultTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="System Diagnostics Trace Listener" />
        </listeners>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="System Diagnostics Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="System Diagnostics Trace Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Mar 30, 2012 at 8:36 PM

Then my next question is how to use it to log trace messages and debug messages. Sometimes I want only trace messages to write out; other times I want both trace and debug messages to write out. Which method I should use to write trace message, which to use to write debug messages?

Thanks,

Cindy

Mar 30, 2012 at 10:21 PM

I tried to use System Diagnostics Trace Listener to write messages to output window or DebugView. It doesn't work. My messages didn't write to output Windows. 

<!--l version="1.0" encoding="utf-8"-->

    
        
    
    
        
            
            
        
        
            
        
        
            
                
                    
                
            
        
        
            
            
            
                
                    
                
            
        
    
Mar 30, 2012 at 10:25 PM
Here is my app.config. I have 2 logging listeners - Event Log Listenser with severity filter as Warning and System Diagnostics trace Listener with severity filter as ALL:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                source="GRFS.Fulfillment.Shipping" log="Application" machineName="."
                traceOutputOptions="None" filter="Warning" />
            <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                type="System.Diagnostics.DefaultTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                traceOutputOptions="DateTime" name="System Diagnostics Trace Listener" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="Event Log Listener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>
Mar 30, 2012 at 10:36 PM

System Diagnostics trace Listener works. I made mistake. I only want to know how to use it to log debug messages and trace messages.

Thanks

Mar 31, 2012 at 2:40 AM
Edited Mar 31, 2012 at 4:21 AM

Enterprise Library does not have a built-in concept of Debug or Trace messages (excluding the Tracer class) -- you are free to design it however you see fit.  Instead of Debug or Trace Enterprise Library uses a Severity Filter of All, Off, Critical, Error, Warning, Information, Verbose, and ActivityTracing.

So I would use Verbose to represent "Debug" messages and "Information" to represent Trace messages.  You can then apply different Severity Filters to each trace listener (EventLog/warning and Debug/Trace) which will let you turn debug/trace on or off.

        <listeners>
            <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" filter="Warning" />
            <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                type="LoggingOutputDebugTraceListener.MyDefaultTraceListener, LoggingOutputDebugTraceListener"
                filter="Verbose" name="System Diagnostics Trace Listener" />
        </listeners>

There are other ways to do this as well.  You could also define separate categories for Warning, Debug, Trace, etc.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Apr 3, 2012 at 9:38 PM

Thanks for the information.

Cindy