xSQL Schema Compare SDK for SQL Server version 12
ExecutionLogger Class
Members  Example 


Provides logging during the execution of the schema synchronization script.
Object Model
ExecutionLogger Class
Syntax
'Declaration
 
Public NotInheritable Class ExecutionLogger 
   Inherits a.d.h
public sealed class ExecutionLogger : a.d.h 
public __gc __sealed class ExecutionLogger : public a.d.h 
Remarks
The execution log stores the script fragments that are sent to and executed by SQL Server. The filename and the path of the log is determined by calling the xSQL.Schema.SqlServer.SqlPublicSettings.GetExecutionLogFile method. The execution log is named ExecLog_yyyymmdd.txt and it cannot be changed. You can change however the log folder via the property xSQL.Schema.SqlServer.SqlPublicSettings.LogFolder.

By default the logging of synchronization script is disabled. To enable it, set the comparison option ComparisonOptions.LogScriptExecution to true.

Example
The following code shows how to execute the schema synchronization script with logging enabled.
using xSQL.Schema.Core;
using xSQL.Schema.SqlServer;
using xSQL.SchemaCompare.SqlServer;
            
namespace xSQL.Sdk.SchemaCompare.Samples
{
    class Test
    {
        public static void SyncWithLogging()
        {
            SqlServer server;
            SqlDatabase xDatabase, yDatabase;
            SqlSchemaCompare comparer;
            ScriptManager sqlScript;
            
            try
            {
            
                // create the SQL Server object
                server = new SqlServer(@"(local)");
                
                // create the left database
                xDatabase = server.GetDatabase("Source");
                
                // create the right database
                yDatabase = server.GetDatabase("Target");
                
                // create the comparer objects
                comparer = new SqlSchemaCompare(xDatabase, yDatabase);
                
                // turn on the script execution logging
                // this option creates a log file named ExecLog_yyyymmdd.txt under \Users\<current_user>\AppData\Local. 
                // the log contains the script batches that are sent to and executed by SQL Server.
                comparer.Options.LogScriptExecution = true;
                
                // attach event handlers to these events in order to get some progress information during the schema read and compare
                comparer.LeftDatabase.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                comparer.RightDatabase.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                comparer.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation);
                
                // step 1: read the schema 
                comparer.ReadSchema();
                
                // step 2: pair the database objects
                comparer.PairObjects();
                
                // step 3: compare the schema
                comparer.Compare();
                
                // check for errors that could have occurred during the schema compare. 
                // some errors are handled quietly and do not stop the process, those that are critical throw exceptions
                // quiet errors are collected and stored into the ErrorRepository object
                if (ErrorRepository.Instance.HasErrors())
                {
                    Console.WriteLine("Some errors occurred during the database compare");
                    Console.Write(ErrorRepository.Instance.GetErrors());
                }
                
                // check the database status; exit if no schema differences are found.
                if (comparer.SqlDatabasePair.ComparisonStatus == ComparisonStatusEnum.Equal)
                    return;
                    
                // step 4: get the T-SQL script intended for the right database; that is the script that should be executed 
                // on Target database in order to make it the same as the Source database
                sqlScript = comparer.GetRightDatabaseScript();
                
                if (!sqlScript.IsEmpty())
                {
                    //--use the event to get some progress during the script execution
                    sqlScript.SchemaScriptExecuting += new EventHandler<SchemaScriptEventArgs>(sqlScript_SchemaScriptExecuting);                    
            
                    //--execute the synchronization script 
                    sqlScript.Execute();
                    
                    //--check for errors
                    if (ErrorRepository.Instance.HasErrors())
                    {
                        Console.WriteLine("Some errors occurred during the script execution.");
                        Console.Write(ErrorRepository.Instance.GetErrors());
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
            }
        }
        
        private static void database_SchemaOperation(object sender, SchemaOperationEventArgs e)
        { 
            //--exclude verbose messages
            if (e.Message.MessageType != OperationMessageTypeEnum.Verbose)
                Console.WriteLine(e.Message.Text);
        }
        
        private static void sqlScript_SchemaScriptExecuting(object sender, SchemaScriptEventArgs e)
        {
            Console.WriteLine("{0} {1}", DateTime.Now.ToString("HH:mm:ss"), e.Script);
        }
    }
}
Inheritance Hierarchy

System.Object
   a.d.h
      xSQL.SchemaCompare.SqlServer.ExecutionLogger

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

ExecutionLogger Members
xSQL.SchemaCompare.SqlServer Namespace

 

 


©Copyright 2022 xSQL Software. All Rights Reserved.

Send Feedback