Support > Samples > Event Log Publisher

Event Log Publisher

Download source code

 

This sample application demonstrates how to trace messages into the Windows Application Event Log.


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 the Windows Application Event Log.

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section
                name="traceManagement"
                type="Css.Diagnostics.TraceManagementSection, 
                Css.Diagnostics.TraceComLib" />
    </configSections>
    <traceManagement mode="On">
        <publishers>
            <!--
            TraceEventLogPublisher allways writes to the trace messages to the 
            application event log log.
            source: written to the eventlog as source of the event log event.
            -->
            <publisher key="EventLogPublisher1"  mode="On" 
                 assembly="Css.Diagnostics.TraceComLib" 
                 type="Css.Diagnostics.Publishers.TraceEventLogPublisher">
                <attributes>
                    <attribute name="source" value="TraceAgentTester"/>
                </attributes>
                <filters>
                    <filter type="*" level="1" />
                </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.EventLogPublisher
{
    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 at your EventLog!");
            Console.ReadKey();
        }
    }
}

 

Step 4: Open Windows Application Event Log

After tracing with the above configuration the trace message appears in the Event Log.