Support > Samples > Text File Publisher

Text File Publisher

Download source code

 

This sample application demonstrates how to trace messages into a simple text file.

Please note that the file extension of the log file should be .crm in order to use Coyote Trace Monitor as log file reader.

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.

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="traceManagement"
type="Css.Diagnostics.TraceManagementSection,Css.Diagnostics.TraceComLib" />
</configSections>
<traceManagement mode="On">
<publishers>
<!--
TraceFileTextPublisher writes the trace messages as XML to a given file.
The XML file is a XML-Snippet. This means it has no root elment.

filename: Path and name of the file.
The path can be a relativ or an absolute path.

rolloversize: Is the size in kilo bytes when the rollover opertion happens

rollovermode: delete = file is deleted upon rollover,
move = file is move to file {guid}_{filename}.

If the filename attribute is not present no file will be created.
The file can be a relative or absolute path the TraceTextFilePublisher.
Relative paths will be computed starting from the
Application Domains base directory.

The optional attributs rollovermode and rolloversize determine the
rollovermode

If rollovermode and or size is omitted no rollover is done.
If rollovermode [delete] or [move] and the size is not omitted and
greater than 0

a rollover happens.
-->
<publisher key="TraceTextFilePublisher1"
mode="On"
assembly="Css.Diagnostics.TraceComLib"
type="Css.Diagnostics.Publishers.TraceTextFilePublisher">
<attributes>
<!-- NOTE: PLEASE USE THE FILE EXTENSION .crm!
SO YOU CAN OPEN THE FILE IN COYOTE TRACE MONITOR -->
  <attribute name="filename" value="..\..\MyTraceFile.crm"/>
<attribute name="rollovermode" value="delete"/>
<attribute name="rolloversize" value="1024"/>
</attributes>
<filters>
<filter type="*" level="2" />
</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.TextFilePublisher
{
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.