Create a Custom Publisher
Download source code
This snippet demonstrates how to implement a custom publisher for the Trace Subsystem. In this sample the custom publisher sends Trace Messages with email.
Step 1: Create a Custom Publisher
Basically the only steps you have to go through to write a new publisher are the following:
1. Create a new library project.
2. Add a reference to the Css.Diagnostics.TraceComLib assembly in your project.
3. Create a new class which implements ITracePublisher (e.g. “EmailTracePublisher”).
4. Add the using Css.Diagnostics statement to your code.
5. Add the using System.Collections.Specialized to your code.
Write your own code into the “Publish” method which handles Trace Messages processing.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using Css.Diagnostics;
using System.Net;
using System.Collections.Specialized;
namespace Css.Coyote.Samples.CustomPublisherLib
{
class EmailTracePublisher : ITracePublisher
{
public void Publish(TraceMessage traceMsg, NameValueCollection config)
{
try
{
// To
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add(config["mailTo"]);
// From
MailAddress mailAddress = new MailAddress(config["mailFrom"]);
mailMsg.From = mailAddress;
// Subject and Body
mailMsg.Subject = String.Format("{0} from {1}", traceMsg.Level,
traceMsg.MachineName );
mailMsg.Body = traceMsg.ToXmlString();
// Init SmtpClient and send
SmtpClient smtpClient;
NetworkCredential credentials;
smtpClient = new SmtpClient(config["smtpHost"]);
credentials = new NetworkCredential(config["username"],
config["password"]);
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Step 2: 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 use the Custom EmailTracePublisher. Make sure that the configuration is adapded to your individual email settings.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="traceManagement"
type="Css.Diagnostics.TraceManagementSection,
Css.Diagnostics.TraceComLib" />
</configSections>
<traceManagement mode="On">
<publishers>
<publisher key="EmailTracePublisher" mode="On"
assembly="Css.Coyote.Samples.CustomPublisherLib"
type="Css.Coyote.Samples.CustomPublisherLib.EmailTracePublisher">
<attributes>
<attribute name="mailTo" value="TraceMessageDestination"/>
<attribute name="mailFrom" value="YourEmail"/>
<attribute name="username" value="YourUserName" />
<attribute name="password" value="YourPassword" />
<attribute name="smtpHost" value="YourSmtpHost" />
</attributes>
<filters>
<filter type="*" level="4"></filter>
</filters>
</publisher>
</publishers>
<filters>
<filter type="*" level="4"></filter>
</filters>
</traceManagement>
</configuration>
Step 3: 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 4: 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;
using System.Net.Mail;
using System.Net;
namespace Css.Coyote.Samples.EmailPublisher
{
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 your mailbox!");
Console.ReadKey();
}
}
}
Step 6: Look into your Inbox
After tracing with the above configuration the trace messages where send via email.
