Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Tracing Sync Framework
Message
From
13/07/2017 08:28:58
 
 
General information
Forum:
C#
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01652635
Message ID:
01652644
Views:
47
>>Can someone better versed in C# and MS documentation assist me with understanding what coding I need in order to trace MS Synchronization Framework?
>>
>>This is the site with the documentation:
>>
>>https://msdn.microsoft.com/en-us/library/cc807160.aspx
>>
>>I understand I need to add the following (or similar) to my sync app's config file:
>>
>>
	<system.diagnostics>
>>		<switches>
>>			<!--  0-off, 1-error, 2-warn, 3-info, 4-verbose. -->
>>			<add name="SyncTracer" value="4" />
>>		</switches>
>>
>>		<trace autoflush="true">
>>			<listeners>
>>				<add name="TestListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="c:\temp\SyncLog.txt"/>
>>			</listeners>
>>		</trace>
>>	</system.diagnostics>
>>
>>
>>Now, what do I need to do in my code to make the trace actually write information to the text file I specified?
>
>Just Trace.TraceInformation : https://msdn.microsoft.com/en-us/library/system.diagnostics.trace.traceinformation(v=vs.110).aspx
>
>That should output to all registered listeners....

Thanks Viv,
but I am afraid I don't understand.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.SqlServer;

using System.Configuration;
using System.IO;

namespace SyncBPO
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string clientConnection = ConfigurationManager.ConnectionStrings["Client"].ConnectionString;
                string serverConnection = ConfigurationManager.ConnectionStrings["Server"].ConnectionString;
                string timeoutvalue = ConfigurationManager.AppSettings.GetValues("Timeout").ToString();

                int timeout;
                if (int.TryParse(timeoutvalue, out timeout) == false)
                {
                    timeout = 300;
                }
    
                // create a connection to the client database
                SqlConnection clientConn = new SqlConnection(clientConnection);

                // connect to server database
                SqlConnection serverConn = new SqlConnection(serverConnection);


                // create the sync orhcestrator
                Console.WriteLine("Starting SyncOrchestrator");
                SyncOrchestrator syncOrchestrator = new SyncOrchestrator();

                // set local provider of orchestrator to a sync provider associated with the 
                // ProductsScope in the SyncExpressDB express client database
                SqlSyncProvider SQLProviderLocal = new SqlSyncProvider("CompleteScope", clientConn);
                SQLProviderLocal.CommandTimeout = timeout;

                syncOrchestrator.LocalProvider = SQLProviderLocal;   //new SqlSyncProvider("CompleteScope", clientConn);
                

                // set the remote provider of orchestrator to a server sync provider associated with
                // the ProductsScope in the SyncDB server database
                SqlSyncProvider SQLProvider = new SqlSyncProvider("CompleteScope", serverConn);
                SQLProvider.CommandTimeout = timeout;

                syncOrchestrator.RemoteProvider = SQLProvider; // new SqlSyncProvider("CompleteScope", serverConn);

                // set the direction of sync session to Upload and Download
                syncOrchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

                // subscribe for errors that occur when applying changes to the client
                ((SqlSyncProvider)syncOrchestrator.LocalProvider).ApplyChangeFailed += new EventHandler<DbApplyChangeFailedEventArgs>(Program_ApplyChangeFailed);

                // execute the synchronization process
                Console.WriteLine("Starting Synchronisation");
                SyncOperationStatistics syncStats = syncOrchestrator.Synchronize();

                using (StreamWriter tw = new StreamWriter("BPOSync.log", true))
                {
                    tw.WriteLine("Start Time: " + syncStats.SyncStartTime);
                    tw.WriteLine("Total Changes Uploaded: " + syncStats.UploadChangesTotal);
                    tw.WriteLine("Total Changes Downloaded: " + syncStats.DownloadChangesTotal);
                    tw.WriteLine("Complete Time: " + syncStats.SyncEndTime);
                    tw.WriteLine("---");

                    // close the stream
                    tw.Close();
                }              
            }
            catch (Exception error)
            {
                using (StreamWriter tw = new StreamWriter("BPOSyncError.log", true))
                {
                    // write a line of text to the file
                    tw.WriteLine(DateTime.Now);
                    tw.WriteLine("Error");
                    tw.WriteLine(error.ToString());
                    //tw.WriteLine(error.InnerException); 
                    tw.WriteLine("---");
                    
                    // close the stream
                    tw.Close();                    
                }
            }
        }

        static void Program_ApplyChangeFailed(object sender, DbApplyChangeFailedEventArgs e)
        {
            // create a writer and open the file
            //TextWriter tw = new StreamWriter("BPOSync.log");
            using (StreamWriter tw = new StreamWriter("BPOSyncChangeFailed.log", true))
            {
                // write a line of text to the file
                tw.WriteLine(DateTime.Now);
                tw.WriteLine(e.Conflict.Type);
                tw.WriteLine(e.Error);
                tw.WriteLine("---");
                // close the stream
                tw.Close();
            }
            
            // display conflict type
            Console.WriteLine(e.Conflict.Type);

            // display error message 
            Console.WriteLine(e.Error);
        }
    }
}
Where do I put the Trace.Information?
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Reply
Map
View

Click here to load this message in the networking platform