You are here: Tuning > Diagnostics > Diagnostic Messages Filter

Diagnostic Messages Filter

The standard listeners can potentially produce quite a lot of messages. By writing your own DiagnosticListener you can filter that information.

On the stage of application tuning you can be interested in optimizing performance through indexing. Diagnostics can help you with giving information about queries that are running on un-indexed fields. By having this information you can decide which queries are frequent and heavy and should be indexed, and which have little performance impact and do not need an index. Field indexes dramatically improve query performance but they may considerably reduce storage and update performance.

In order to get rid of all unnecessary diagnostic information and concentrate on indexes let's create special diagnostic listener:

private class DiagnosticFilter : IDiagnosticListener
{
    private readonly ICollection<Type> filterFor;
    private readonly IDiagnosticListener target;

    public DiagnosticFilter(IDiagnosticListener target, params Type[] filterFor)
    {
        this.target = target;
        this.filterFor = new List<Type>(filterFor);
    }

    public void OnDiagnostic(IDiagnostic diagnostic)
    {
        Type type = diagnostic.GetType();
        if (filterFor.Contains(type))
        {
            target.OnDiagnostic(diagnostic);
        }
    }
}
DiagnosticsExamples.cs: A simple message filter
Private Class DiagnosticFilter
    Implements IDiagnosticListener
    Private ReadOnly filterFor As ICollection(Of Type)
    Private ReadOnly target As IDiagnosticListener

    Public Sub New(ByVal target As IDiagnosticListener, ByVal ParamArray filterFor As Type())
        Me.target = target
        Me.filterFor = New List(Of Type)(filterFor)
    End Sub

    Public Sub OnDiagnostic(ByVal diagnostic As IDiagnostic) _
        Implements IDiagnosticListener.OnDiagnostic

        Dim type As Type = diagnostic.[GetType]()
        If filterFor.Contains(type) Then
            target.OnDiagnostic(diagnostic)
        End If
    End Sub
End Class
DiagnosticsExamples.vb: A simple message filter

After that we can use the filter-listener. It takes two arguments. The first one is a regular listener, the second is a list of all messages which are passed through.

IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration();
configuration.Common.Diagnostic
    .AddListener(new DiagnosticFilter(new DiagnosticToConsole(), typeof(LoadedFromClassIndex)));
DiagnosticsExamples.cs: Filter for unindexed fields
Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration()
configuration.Common.Diagnostic _
    .AddListener(New DiagnosticFilter(New DiagnosticToConsole(), GetType(LoadedFromClassIndex)))
DiagnosticsExamples.vb: Filter for unindexed fields