Support > Samples > Style Sheet Publisher

Style Sheet Publisher

Download source code

 

This sample application demonstrates how to trace messages into a simple text file formated with a Cascading Style Sheet file.

Step 1:  Configure your application

To enable tracing in your application it's important to add the “traceManagement” section into your application configuration file. The following sample shows the necessary configuration to trace into a text file and format the message.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section
                name="traceManagement"
                type="Css.Diagnostics.TraceManagementSection, 
                Css.Diagnostics.TraceComLib" />
    </configSections>
    <traceManagement mode="On">
        <publishers>
        <!--
        TraceStylesheetPublisher applies the style sheet for each message 
        before writing it to a text file.
                     
        filename:   Name of the trace file to write the formatted message to.
        stylesheet: Name of the xslt stylesheet to use for formatting the trace message.
        -->
            <publisher key="TraceStylesheetPublisher1" 
                 mode="On" 
                 assembly="Css.Diagnostics.TraceComLib" 
                 type="Css.Diagnostics.Publishers.TraceStylesheetPublisher">
                <attributes>
                   <attribute name="filename" value="..\..\MyTraceFile.txt"/>
                   <attribute name="stylesheet" value="..\..\FormatTraceMessage.xslt" />
                </attributes>
                <filters>
                    <filter type="*" level="4" />
                </filters>
            </publisher>
        </publishers>
        <filters>
            <filter type="*" level="4"></filter>
        </filters>
    </traceManagement>
</configuration>
Step 2: Reference Css.Diagnostics.TraceComLib

The whole tracing technology is located in the “Css.Diagnostics.TraceComLib” Library. Referencing this Library in your application offers you the possibility to create and trace messages.

Step 3: Tracing

The following snippet shows a possible implementation to trace messages in your application.

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using Css.Diagnostics;

namespace Css.Coyote.Samples.StyleSheetPublisher
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure
                (System.IO.Path.Combine(
                      AppDomain.CurrentDomain.BaseDirectory
                    , AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
                    , true);

            Exception ex = new Exception("If you read this, everything works fine ;-)");

            try
            {
                throw ex;
            }
            catch (Exception)
            {
                TraceAgent.Write(ex, TraceLevel.Error);
            }

            Console.WriteLine("Look in the MyTraceFile.txt!");
            Console.ReadKey();
        }
    }
}
Step 4: Open Trace File

After tracing with the above configuration the trace message appears in the text file.