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:
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:
opts.Logger(new ConsolePolecatLogger());Request Counting
Track database requests per session:
await using var session = store.LightweightSession();
await session.LoadAsync<User>(id);
Console.WriteLine(session.RequestCount); // 1SQL Preview
ToSql
Preview the SQL generated by a LINQ query:
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') = @p0Schema Diagnostics
ToDatabaseScript
Generate the complete DDL script for all Polecat tables:
var script = await store.Advanced.ToDatabaseScript();
Console.WriteLine(script);WriteCreationScriptToFileAsync
Save the schema script to a file:
await store.Advanced.WriteCreationScriptToFileAsync("/path/to/schema.sql");Data Cleanup
CleanAllDocumentsAsync
Delete all data from all document tables:
await store.Advanced.CleanAllDocumentsAsync();CleanAsync
Delete all data from a specific document table:
await store.Advanced.CleanAsync<User>();CleanAllEventDataAsync
Delete all event data (events, streams, progressions):
await store.Advanced.CleanAllEventDataAsync();WARNING
These cleanup methods permanently delete data. They are intended for testing and development, not production use.

JasperFx provides formal support for Polecat and other Critter Stack libraries. Please check our