The Sharp Factory
Esc
↑ ↓ to moveEnter to open
Getting Started / Query Logging

Query Logging

See the SQL each query sends to the database while your application runs.

On this page

Log SQL Queries#

Sometimes you will want to log the SQL Queries issued by your application. The Sharp Factory Framework builds the queries dynamically at runtime.

We provided a way for you to intercept the SQL output and log it in which ever way you see fit. It is recommended to only turn on query logging during development, testing or, debugging a runtime issue.

During production query logging should be turned off unless really necessary.

The steps to setup query logging are as follows:

  1. First create a C# method that takes these 2 parameters:

  • string sqlObject this will hold the name of the Table, View, Stored Procedure, Function or, Sequence being called.

  • string query this is the actual SQL query exactly how it was sent to the database.

The following code sample shows the signature of such method:

C#
private static void WriteLog(string dbObjectName, string query){       // your logging logic goes here    // dbObjectName: name of the Table, Stored Procedure, Function or, Sequence    // query: the actual query how it was issued to the database.}
  1. Second enable logging and hookup your method to the Database class.

Look at the code samples below:

Example using Program.cs#

C#
// Replace SFDemo with the root namespace// specified when you first generated the modelusing SFDemo.Repository.Common;using SFDemo.Repository.Container;namespace ConsoleClient{    class Program    {        static void Main(string[] args)        {            // Database is a static class            // it was generated by The Sharp Factory App            Database.RegisterModelConnectionString(RepoLookup.ModelId.AppDb,             "Your Application's Connection String Goes Here");               // Setup query logging after registering your connection string            // WriteLog is the method YOU wrote to perform logging            // Setup query logging before instantiating your RepositoryContainer            Database.Logger = WriteLog;            Database.LogEnabled = true;            // This is your repository container            // Use it to perform operations against the data store            var repository = new RepositoryContainer();        }    }}

Example using Program.cs in .NET 10#

C#
// Replace SFDemo with the root namespace// specified when you first generated the modelusing SFDemo.Repository.Common;using SFDemo.Repository.Container;using SFDemo.Repository.Container.Interfaces;var builder = WebApplication.CreateBuilder(args);// Database is a static class// it was generated by The Sharp Factory AppDatabase.RegisterModelConnectionString(RepoLookup.ModelId.AppDb,"Your Application's Connection String Goes Here");// Setup query logging after registering your connection string// WriteLog is the method YOU wrote that will receive the query information// Setup query logging before instantiating your RepositoryContainerDatabase.Logger = WriteLog;Database.LogEnabled = true;// setup dependency injection as a singletonbuilder.Services.AddSingleton<IRepositoryContainer, RepositoryContainer>();// the rest of your code goes here..........var app = builder.Build();app.Run();