Skip to content

Commit 13a1503

Browse files
committed
Substitui Console.WriteLine por Logger.Log
Atualiza o sistema de log para centralizar mensagens de depuração e erro, substituindo chamadas de `Console.WriteLine` por `Logger.Log` em várias classes, incluindo `RefreshSPOLists` e `SQLInteraction`. Melhorias na formatação das mensagens de log foram implementadas, com a inclusão de informações adicionais e padronização do estilo. Também foram feitas alterações no arquivo de configuração XML, adicionando um comentário sobre como usar o aplicativo.
1 parent 76de40c commit 13a1503

4 files changed

Lines changed: 161 additions & 157 deletions

File tree

SPOtoSQL-Snapshots/ConsoleApp1/SPODataQuality/RefreshSPOLists.cs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class RefreshSPOLists
1515
// Main entry point of the application, executed when the program starts
1616
private static void Main(string[] args)
1717
{
18-
Console.WriteLine("DEBUG: Usig the Default config");
18+
Logger.Log(1, "DEBUG: Usig the Default config");
1919
string configPath = "XmlConfig\\UserConfig.xml"; // Default path for the configuration file
2020

2121
int verbose = 0;
@@ -47,13 +47,13 @@ private static void Main(string[] args)
4747

4848
try
4949
{
50-
Console.WriteLine("DEBUG: Starting Main");
51-
Console.WriteLine("CURRENT TIME: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
50+
Logger.Log(1, "DEBUG: Starting Main");
51+
Logger.Log(2, "CURRENT TIME: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
5252

5353
try
5454
{
5555
TestSQLConnection();
56-
Console.WriteLine("DEBUG: Main: SQL connection test completed");
56+
Logger.Log(1, "DEBUG: Main: SQL connection test completed");
5757
}
5858
catch (Exception ex)
5959
{
@@ -80,7 +80,7 @@ private static void Main(string[] args)
8080
try
8181
{
8282
spoUser = new SPOUser(credentials.username, credentials.password);
83-
Console.WriteLine("DEBUG: Main: SPOUser created");
83+
Logger.Log(1, "DEBUG: Main: SPOUser created");
8484
}
8585
catch (Exception ex)
8686
{
@@ -95,11 +95,11 @@ private static void Main(string[] args)
9595
{
9696
list1 = new SPOList();
9797
list1.SPOUser = spoUser;
98-
Console.WriteLine("DEBUG: Main: First SPOList configured");
98+
Logger.Log(3, "DEBUG: Main: First SPOList configured");
9999

100100
list2 = new SPOList();
101101
list2.SPOUser = spoUser;
102-
Console.WriteLine("DEBUG: Main: Second SPOList configured");
102+
Logger.Log(1, "DEBUG: Main: Second SPOList configured");
103103
}
104104
catch (Exception ex)
105105
{
@@ -114,11 +114,11 @@ private static void Main(string[] args)
114114
if ((uint)args.Length > 0U)
115115
{
116116
string lower = args[0].ToLower();
117-
Console.WriteLine("DEBUG: Main: Received argument - " + lower);
117+
Logger.Log(1, "DEBUG: Main: Received argument - " + lower);
118118

119119
if (lower == "daily")
120120
{
121-
Console.WriteLine("DEBUG: Main: Executing daily");
121+
Logger.Log(1, "DEBUG: Main: Executing daily");
122122
try
123123
{
124124
RefreshSQLLists.SPOtoSQLUpdate(true);
@@ -132,7 +132,7 @@ private static void Main(string[] args)
132132
}
133133
else if (lower == "monthly")
134134
{
135-
Console.WriteLine("DEBUG: Main: Executing monthly");
135+
Logger.Log(1, "DEBUG: Main: Executing monthly");
136136
try
137137
{
138138
RefreshSQLLists.SPOtoSQLUpdate(false);
@@ -146,7 +146,7 @@ private static void Main(string[] args)
146146
}
147147
else
148148
{
149-
Console.WriteLine("Unrecognized argument, please use daily or monthly as the argument");
149+
Logger.Log(2, "Unrecognized argument, please use daily or monthly as the argument");
150150
}
151151
}
152152
}
@@ -157,8 +157,8 @@ private static void Main(string[] args)
157157
Console.WriteLine("Stack Trace: " + ex.StackTrace);
158158
}
159159

160-
Console.WriteLine("End of requests.");
161-
Console.WriteLine();
160+
Logger.Log(2, "End of requests.");
161+
Logger.Log(2, "");
162162
}
163163
catch (Exception ex)
164164
{
@@ -170,23 +170,23 @@ private static void Main(string[] args)
170170

171171
private static void TestSQLConnection()
172172
{
173-
Console.WriteLine("Testing SQL Server connection...");
173+
Logger.Log(2, "Testing SQL Server connection...");
174174
try
175175
{
176176
string connectionString = ConfigurationReader.GetSqlConnectionString();
177-
Console.WriteLine($"Attempting to connect to SQL server...");
177+
Logger.Log(2, "Attempting to connect to SQL server...");
178178

179179
using (var connection = new SqlConnection(connectionString))
180180
{
181-
try
181+
try
182182
{
183183
connection.Open();
184184
}
185185
catch (SqlException ex) when (ex.Number == -1 || ex.Number == 10060 || ex.Number == 0 || ex.Number == 53)
186186
{
187187
Console.WriteLine("ERROR: Unable to connect to the SQL Server.");
188188
Console.WriteLine("Please check if your VPN connection is active and try again.");
189-
Console.WriteLine($"Technical details: {ex.Message}");
189+
Console.WriteLine("Technical details: " + ex.Message);
190190
Console.WriteLine("Press any key to exit...");
191191
Console.ReadKey();
192192
Environment.Exit(1);
@@ -198,9 +198,9 @@ private static void TestSQLConnection()
198198
throw;
199199
}
200200

201-
Console.WriteLine($"Server: {connection.DataSource}");
202-
Console.WriteLine($"Database: {connection.Database}");
203-
Console.WriteLine("SQL connection established successfully!");
201+
Logger.Log(2, "Server: " + connection.DataSource);
202+
Logger.Log(2, "Database: " + connection.Database);
203+
Logger.Log(2, "SQL connection established successfully!");
204204

205205
// Basic permissions test
206206
using (var command = connection.CreateCommand())
@@ -210,7 +210,7 @@ private static void TestSQLConnection()
210210
{
211211
command.CommandText = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES";
212212
int tableCount = (int)command.ExecuteScalar();
213-
Console.WriteLine($"Number of tables in database: {tableCount}");
213+
Logger.Log(2, "Number of tables in database: " + tableCount);
214214
}
215215
catch (Exception ex)
216216
{
@@ -223,11 +223,11 @@ private static void TestSQLConnection()
223223
{
224224
command.CommandText = "CREATE TABLE #TempTest (ID int); DROP TABLE #TempTest;";
225225
command.ExecuteNonQuery();
226-
Console.WriteLine("CREATE TABLE permission: OK");
226+
Logger.Log(2, "CREATE TABLE permission: OK");
227227
}
228228
catch (SqlException ex)
229229
{
230-
Console.WriteLine($"Warning: No CREATE TABLE permission: {ex.Message}");
230+
Logger.Log(2, "Warning: No CREATE TABLE permission: " + ex.Message);
231231
}
232232
catch (Exception ex)
233233
{
@@ -240,16 +240,16 @@ private static void TestSQLConnection()
240240

241241
catch (SqlException ex)
242242
{
243-
Console.WriteLine($"SQL connection error: {ex.Message}");
244-
Console.WriteLine($"Error number: {ex.Number}");
245-
Console.WriteLine($"State: {ex.State}");
246-
Console.WriteLine($"Procedure: {ex.Procedure}");
243+
Console.WriteLine("SQL connection error: " + ex.Message);
244+
Console.WriteLine("Error number: " + ex.Number);
245+
Console.WriteLine("State: " + ex.State);
246+
Console.WriteLine("Procedure: " + ex.Procedure);
247247
Console.WriteLine("The Execution is stoping.");
248248
throw; // Re-throw to stop execution
249249
}
250250
catch (Exception ex)
251251
{
252-
Console.WriteLine($"General error testing SQL connection: {ex.Message}");
252+
Console.WriteLine("General error testing SQL connection: " + ex.Message);
253253
throw;
254254
}
255255
}
@@ -258,7 +258,7 @@ private static void TestSQLConnection()
258258
// Method to retrieve and display all lists from a specific SharePoint site
259259
public static void GetAllLists()
260260
{
261-
Console.WriteLine("DEBUG: Entering GetAllLists");
261+
Logger.Log(1, "DEBUG: Entering GetAllLists");
262262
try
263263
{
264264
// Get SharePoint credentials
@@ -275,18 +275,18 @@ public static void GetAllLists()
275275
{
276276
try
277277
{
278-
Console.WriteLine("DEBUG: Loading list - " + allList.Title);
278+
Logger.Log(1, "DEBUG: Loading list - " + allList.Title);
279279
// Load the IsSystemList property to determine if the list is a system list
280280
context.Ctx.Load<List>(allList, new Expression<Func<List, object>>[1]
281281
{
282282
(Expression<Func<List, object>>) (l => (object) l.IsSystemList)
283283
});
284284
context.Ctx.ExecuteQuery(); // Execute the query to retrieve the data
285-
Console.WriteLine("List Name: " + allList.Title + "; is: " + allList.IsSystemList.ToString());
285+
Logger.Log(2, "List Name: " + allList.Title + "; is: " + allList.IsSystemList.ToString());
286286
}
287287
catch (Exception ex)
288288
{
289-
Console.WriteLine($"ERROR: Failed to load or display list '{allList.Title}'.");
289+
Console.WriteLine("ERROR: Failed to load or display list '" + allList.Title + "'.");
290290
Console.WriteLine("Exception: " + ex.Message);
291291
}
292292
}
@@ -302,7 +302,7 @@ public static void GetAllLists()
302302
// Debugging method to inspect the properties of a specific SharePoint list
303303
private static void SPODebug(string listName, string ctxURL, SPOUser user)
304304
{
305-
Console.WriteLine("DEBUG: Entering SPODebug");
305+
Logger.Log(1, "DEBUG: Entering SPODebug");
306306
try
307307
{
308308
// Create a SharePoint list object with a CAML query limiting to 1 item
@@ -314,7 +314,7 @@ private static void SPODebug(string listName, string ctxURL, SPOUser user)
314314
CAMLQuery = "<View><RowLimit>1</RowLimit></View>"
315315
};
316316

317-
Console.WriteLine("DEBUG: Executing Build");
317+
Logger.Log(1, "DEBUG: Executing Build");
318318
try
319319
{
320320
spoList.Build(); // Build the list to load its data
@@ -326,14 +326,14 @@ private static void SPODebug(string listName, string ctxURL, SPOUser user)
326326
return;
327327
}
328328

329-
Console.WriteLine("DEBUG: Executing PropsToString");
329+
Logger.Log(1, "DEBUG: Executing PropsToString");
330330
try
331331
{
332332
// Print the properties of the first item in the list for debugging
333333
if (spoList.ItemCollection != null && spoList.ItemCollection.Count > 0)
334334
spoList.PropsToString(spoList.ItemCollection[0]);
335335
else
336-
Console.WriteLine("No items found in the list.");
336+
Logger.Log(2, "No items found in the list.");
337337
}
338338
catch (Exception ex)
339339
{
@@ -354,13 +354,13 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
354354
{
355355
try
356356
{
357-
Console.WriteLine("DEBUG: Starting RefreshListsSPO");
357+
Logger.Log(1, "DEBUG: Starting RefreshListsSPO");
358358

359359
try
360360
{
361361
// Build the source list to load its data
362362
sourceList.Build();
363-
Console.WriteLine("DEBUG: sourceList.Build completed");
363+
Logger.Log(1, "DEBUG: sourceList.Build completed");
364364
}
365365
catch (Exception ex)
366366
{
@@ -373,7 +373,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
373373
{
374374
// Build the destination list to load its data
375375
destList.Build();
376-
Console.WriteLine("DEBUG: destList.Build completed");
376+
Logger.Log(1, "DEBUG: destList.Build completed");
377377
}
378378
catch (Exception ex)
379379
{
@@ -390,7 +390,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
390390
{
391391
// Get the field mappings between the source and destination lists
392392
actualFields = GetActualFields(sourceList, destList);
393-
Console.WriteLine("DEBUG: Fields obtained");
393+
Logger.Log(1, "DEBUG: Fields obtained");
394394
}
395395
catch (Exception ex)
396396
{
@@ -417,7 +417,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
417417
// If the destination list has fewer items, add new items to match the source
418418
if (num2 < num1)
419419
{
420-
Console.WriteLine("DEBUG: Adding new items");
420+
Logger.Log(1, "DEBUG: Adding new items");
421421
try
422422
{
423423
do
@@ -427,9 +427,9 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
427427
}
428428
while (num2 < num1);
429429

430-
Console.WriteLine("Adding new items...");
430+
Logger.Log(2, "Adding new items...");
431431
destList.Update(); // Update the destination list with the new items
432-
Console.WriteLine("Done adding items.");
432+
Logger.Log(2, "Done adding items.");
433433
}
434434
catch (Exception ex)
435435
{
@@ -453,7 +453,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
453453
}
454454
catch (Exception ex)
455455
{
456-
Console.WriteLine($"ERROR: Failed to copy field '{actualFields[index2, 1]}' to '{actualFields[index2, 0]}' for item ID {id}.");
456+
Console.WriteLine("ERROR: Failed to copy field '" + actualFields[index2, 1] + "' to '" + actualFields[index2, 0] + "' for item ID " + id + ".");
457457
Console.WriteLine("Exception: " + ex.Message);
458458
}
459459
}
@@ -463,7 +463,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
463463
}
464464
catch (Exception ex)
465465
{
466-
Console.WriteLine($"ERROR: Failed to update item ID {id} in destination list.");
466+
Console.WriteLine("ERROR: Failed to update item ID " + id + " in destination list.");
467467
Console.WriteLine("Exception: " + ex.Message);
468468
}
469469
}
@@ -478,7 +478,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
478478
{
479479
// Execute the query to apply all changes to the SharePoint site
480480
destList.Ctx.ExecuteQuery();
481-
Console.WriteLine(sourceList.Site + " " + sourceList.Name + " -> " + destList.Site + " " + destList.Name + ": Done!");
481+
Logger.Log(2, sourceList.Site + " " + sourceList.Name + " -> " + destList.Site + " " + destList.Name + ": Done!");
482482
}
483483
catch (Exception ex)
484484
{
@@ -497,13 +497,13 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
497497
// Helper method to create a mapping of fields between two lists based on their titles
498498
private static string[,] GetActualFields(SPOList listone, SPOList listtwo)
499499
{
500-
Console.WriteLine("DEBUG: Entering GetActualFields");
500+
Logger.Log(1, "DEBUG: Entering GetActualFields");
501501
try
502502
{
503503
// Get fields from both lists
504504
List<Field> fields1 = GetFields(listone);
505505
List<Field> fields2 = GetFields(listtwo);
506-
506+
507507
// Create a 2D array to store the field mappings (internal names)
508508
string[,] strArray = new string[fields1.Count, 2];
509509
int index1 = 0;
@@ -520,7 +520,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
520520
{
521521
strArray[index1, 0] = field2.InternalName; // Destination field
522522
strArray[index1, 1] = field1.InternalName; // Source field
523-
Console.WriteLine($"DEBUG: Match found - {field1.Title}");
523+
Logger.Log(1, "DEBUG: Match found - " + field1.Title);
524524
}
525525
++index2;
526526
}
@@ -543,7 +543,7 @@ private static void RefreshListsSPO(SPOList sourceList, SPOList destList)
543543
// Helper method to retrieve fields from a SharePoint list, excluding base type fields except "Title"
544544
private static List<Field> GetFields(SPOList list)
545545
{
546-
Console.WriteLine("DEBUG: Entering GetFields");
546+
Logger.Log(1, "DEBUG: Entering GetFields");
547547
List<Field> fieldList = new List<Field>();
548548
try
549549
{
@@ -554,7 +554,7 @@ private static List<Field> GetFields(SPOList list)
554554
if (!field.FromBaseType || field.InternalName == "Title")
555555
{
556556
fieldList.Add(field);
557-
Console.WriteLine($"DEBUG: Field added - {field.Title}");
557+
Logger.Log(1, "DEBUG: Field added - " + field.Title);
558558
}
559559
}
560560
}

0 commit comments

Comments
 (0)