Query Logging

Table Of Contents

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 model

using 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 Startup.cs in .NET Core

C#
// Replace SFDemo with the root namespace

// specified when you first generated the model

using SFDemo.Repository.Common;
using SFDemo.Repository.Container;
using SFDemo.Repository.Container.Interfaces;

// code omitted for brevity......


public void ConfigureServices(IServiceCollection services)
{
    // 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 that will receive the query information

    // Setup query logging before instantiating your RepositoryContainer    

    Database.Logger = WriteLog;
    Database.LogEnabled = true;


    // setup dependency injection as a singleton

    services.AddSingleton<IRepositoryContainer, RepositoryContainer>();
    
    // the rest of your code goes here..........


}