Skip to content

The search box knows all the secrets -- try it!

Polecat is part of the Critter Stack ecosystem.

JasperFx Logo JasperFx provides formal support for Polecat and other Critter Stack libraries. Please check our Support Plans for more details.

Diagnostics and Instrumentation

Polecat provides several tools for monitoring, debugging, and understanding what's happening in your application.

Session Logging

IPolecatLogger

Implement IPolecatLogger at the store level to create per-session loggers:

cs
public class ConsolePolecatLogger : IPolecatLogger
{
    public IPolecatSessionLogger StartSession(IQuerySession session)
    {
        return new ConsoleSessionLogger();
    }
}

public class ConsoleSessionLogger : IPolecatSessionLogger
{
    public void OnBeforeExecute(string sql)
    {
        Console.WriteLine($"Executing: {sql}");
    }

    public void LogSuccess(string sql)
    {
        Console.WriteLine($"Success: {sql}");
    }

    public void LogFailure(string sql, Exception ex)
    {
        Console.WriteLine($"Failed: {sql} - {ex.Message}");
    }

    public void RecordSavedChanges(IDocumentSession session)
    {
        Console.WriteLine($"Saved changes ({session.RequestCount} requests)");
    }
}

Register the logger:

cs
opts.Logger(new ConsolePolecatLogger());

Request Counting

Track database requests per session:

cs
await using var session = store.LightweightSession();
await session.LoadAsync<User>(id);
Console.WriteLine(session.RequestCount); // 1

SQL Preview

ToSql

Preview the SQL generated by a LINQ query:

cs
var sql = session.Query<User>()
    .Where(x => x.LastName == "Smith")
    .ToSql();

Console.WriteLine(sql);
// SELECT data FROM pc_doc_user WHERE JSON_VALUE(data, '$.lastName') = @p0

Schema Diagnostics

ToDatabaseScript

Generate the complete DDL script for all Polecat tables:

cs
var script = await store.Advanced.ToDatabaseScript();
Console.WriteLine(script);

WriteCreationScriptToFileAsync

Save the schema script to a file:

cs
await store.Advanced.WriteCreationScriptToFileAsync("/path/to/schema.sql");

Data Cleanup

CleanAllDocumentsAsync

Delete all data from all document tables:

cs
await store.Advanced.CleanAllDocumentsAsync();

CleanAsync

Delete all data from a specific document table:

cs
await store.Advanced.CleanAsync<User>();

CleanAllEventDataAsync

Delete all event data (events, streams, progressions):

cs
await store.Advanced.CleanAllEventDataAsync();

WARNING

These cleanup methods permanently delete data. They are intended for testing and development, not production use.

Released under the MIT License.