Listen for trace events
3 min
To receive events raised from the tracing API, you are required to register a listener by implementing the ITraceListener interface. The initialization code in Optimizely Search & Navigation searches assemblies located in the current app domain and registers the implementations, if any, that exist.
Below is an example of a trace events listener which sends an email in case of an error.
using System.Collections.Generic;
using EPiServer.Find.Tracing;
public class TestTraceListener: ITraceListener {
public TestTraceListener() {}
public virtual void Add(ITraceEvent traceEvent) {
if (traceEvent.IsError) {
MailAdmin(traceEvent.Exception, traceEvent.Message)
}
}
}The interface of ITraceEvent looks like this:
public interface ITraceEvent {
DateTime Timestamp {
get;
}
ITraceable Source {
get;
}
string Message {
get;
}
bool IsError {
get;
}
Exception Exception {
get;
set;
}
}Example – Network issues when connecting to the index
You can use tracing to generate notifications of service exceptions. To do so, handle the following exceptions like this:
public class TestTraceListener: ITraceListener {
public virtual void Add(ITraceEvent traceEvent) {
if (traceEvent.IsError && traceEvent.Exception is typeof (ServiceException)) {
// inspect Response code to determine course of action
AlertOperations(traceEvent.Exception, traceEvent.Message, "Find Index potentially down.")
}
}
}Example – Document storage limit reached
This example generates a notification when the number of indexed items reaches the limit configured in your Optimizely Search & Navigation service subscription.
public class TestTraceListener: ITraceListener {
public virtual void Add(ITraceEvent traceEvent) {
if (traceEvent.IsError && traceEvent.Exception is typeof (ServiceException)) {
// if response code is 403 and message contains "Your account is not authorized to index more than"
AlertAccountMgr(traceEvent.Exception, traceEvent.Message, "limit reached for Find License")
}
}
}