Skip to content

Commit 3ccb99f

Browse files
committed
fix: enhance logging configuration with append option and improve debug messages
1 parent 40d2abe commit 3ccb99f

File tree

2 files changed

+167
-91
lines changed

2 files changed

+167
-91
lines changed

XMLDiff/Program.cs

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ static void Main(string[] args)
2222

2323
private static void RunOptionsAndReturnExitCode(Options opts)
2424
{
25-
ConfigureLogging(opts.LogToFile);
25+
ConfigureLogging(opts.LogToFile, opts.AppendToLog);
2626

2727
var originalXmlPath = opts.OriginalXml;
2828
var modifiedXmlPath = opts.ModifiedXml;
2929
var diffXmlPath = opts.DiffXml;
30-
var diffXsdPath = opts.Xsd;
30+
var diffXsdPath = opts.Xsd ?? "diff.xsd";
3131
var pathOptions = new PathOptions { OnlyFullPath = opts.OnlyFullPath, UseAllAttributes = opts.UseAllAttributes };
3232

3333
bool originalIsDir = Directory.Exists(originalXmlPath);
3434
bool modifiedIsDir = Directory.Exists(modifiedXmlPath);
3535
bool diffIsDir = Directory.Exists(diffXmlPath);
3636

37-
XmlReaderSettings diffReaderSettings = CreateXmlReaderSettings(diffXsdPath!);
37+
XmlReaderSettings? diffReaderSettings = CreateXmlReaderSettings(diffXsdPath!);
3838
if (originalIsDir && modifiedIsDir && diffIsDir)
3939
{
4040
Logger.Info("Processing directories recursively.");
@@ -81,6 +81,7 @@ public class Options
8181
private bool logToFile;
8282
private bool onlyFullPath;
8383
private bool useAllAttributes;
84+
private bool appendToLog;
8485

8586
[Option('o', "original_xml", Required = true, HelpText = "Path to the original XML file or directory.")]
8687
public string? OriginalXml
@@ -117,6 +118,13 @@ public bool LogToFile
117118
set => logToFile = value;
118119
}
119120

121+
[Option('a', "append-to-log", Required = false, HelpText = "Append logs to the existing log file.", Default = false)]
122+
public bool AppendToLog
123+
{
124+
get => appendToLog;
125+
set => appendToLog = value;
126+
}
127+
120128
[Option("only-full-path", Required = false, HelpText = "Generate only full path.", Default = false)]
121129
public bool OnlyFullPath
122130
{
@@ -136,7 +144,7 @@ public bool UseAllAttributes
136144

137145
#region Logging Configuration
138146

139-
private static void ConfigureLogging(bool logToFile)
147+
private static void ConfigureLogging(bool logToFile, bool appendToLog = false)
140148
{
141149
var config = new NLog.Config.LoggingConfiguration();
142150

@@ -146,7 +154,7 @@ private static void ConfigureLogging(bool logToFile)
146154
{
147155
var logFile = new NLog.Targets.FileTarget("logFile")
148156
{
149-
FileName = "${basedir}/${processname}.log",
157+
FileName = Path.Combine(Environment.CurrentDirectory, "${processname}.log"),
150158
Layout = "${longdate} ${level} ${message} ${exception}",
151159
KeepFileOpen = true,
152160
DeleteOldFileOnStartup = true, // Overwrite the log file on each run
@@ -172,10 +180,11 @@ private static void ProcessSingleFile(
172180
string originalXmlPath,
173181
string modifiedXmlPath,
174182
string diffXmlPath,
175-
XmlReaderSettings diffReaderSettings,
183+
XmlReaderSettings? diffReaderSettings,
176184
PathOptions pathOptions
177185
)
178186
{
187+
Logger.Info($"Comparing original XML '{originalXmlPath}' with modified XML '{modifiedXmlPath}' to the diff XML at '{diffXmlPath}'");
179188
if (!File.Exists(originalXmlPath))
180189
{
181190
Logger.Error($"Original XML file does not exist: {originalXmlPath}");
@@ -201,11 +210,9 @@ PathOptions pathOptions
201210
string? diffXmlDir = Path.GetDirectoryName(diffXmlPath);
202211
if (string.IsNullOrEmpty(diffXmlDir))
203212
{
204-
Logger.Error("Failed to determine the directory for diffXmlPath.");
205-
return;
213+
Logger.Info("Output directory is null or empty. Using current directory.");
206214
}
207-
208-
if (!Directory.Exists(diffXmlDir))
215+
else if (!Directory.Exists(diffXmlDir))
209216
{
210217
try
211218
{
@@ -287,7 +294,7 @@ private static void ProcessDirectories(
287294
string originalDir,
288295
string modifiedDir,
289296
string diffDir,
290-
XmlReaderSettings diffReaderSettings,
297+
XmlReaderSettings? diffReaderSettings,
291298
PathOptions pathOptions
292299
)
293300
{
@@ -405,7 +412,7 @@ private static bool CompareElements(
405412
}
406413
XElement replaceOp = new XElement("replace", new XAttribute("sel", sel), modifiedText);
407414
diffRoot.Add(replaceOp);
408-
Logger.Debug($"Replaced text in element '{originalElem.Name}' from '{originalText}' to '{modifiedText}'.");
415+
Logger.Debug($"Replaced text in element '{GetElementInfo(originalElem)}' from '{originalText}' to '{modifiedText}'.");
409416
}
410417
else
411418
{
@@ -415,7 +422,7 @@ private static bool CompareElements(
415422
}
416423
XElement removeOp = new XElement("remove", new XAttribute("sel", $"{sel}/text()"));
417424
diffRoot.Add(removeOp);
418-
Logger.Debug($"Removed text from element '{originalElem.Name}'.");
425+
Logger.Debug($"Removed text from element '{GetElementInfo(originalElem)}'.");
419426
}
420427
}
421428
}
@@ -576,7 +583,7 @@ private static bool CompareElements(
576583
{
577584
var addedChild = modifiedChildren[l];
578585
addOp.Add(addedChild);
579-
Logger.Debug($"Added element '{addedChild.Name}' to parent '{originalElem.Name}'.");
586+
Logger.Debug($"Added element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
580587
}
581588
diffRoot.Add(addOp);
582589
j = k;
@@ -590,7 +597,7 @@ private static bool CompareElements(
590597
string sel = GenerateXPath(originalChild, pathOptions);
591598
XElement removeOp = new XElement("remove", new XAttribute("sel", sel));
592599
diffRoot.Add(removeOp);
593-
Logger.Debug($"Removed element '{originalChild.Name}' from parent '{originalElem.Name}'.");
600+
Logger.Debug($"Removed element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
594601
i++;
595602
}
596603
}
@@ -602,7 +609,7 @@ private static bool CompareElements(
602609
string sel = GenerateXPath(originalChild, pathOptions);
603610
XElement removeOp = new XElement("remove", new XAttribute("sel", sel));
604611
diffRoot.Add(removeOp);
605-
Logger.Debug($"Removed element '{originalChild.Name}' from parent '{originalElem.Name}'.");
612+
Logger.Debug($"Removed element '{GetElementInfo(originalChild)}' from parent '{GetElementInfo(originalElem)}'.");
606613
i++;
607614
}
608615

@@ -622,7 +629,7 @@ private static bool CompareElements(
622629
{
623630
var addedChild = modifiedChildren[j];
624631
addOp.Add(addedChild);
625-
Logger.Debug($"Added element '{addedChild.Name}' to parent '{originalElem.Name}'.");
632+
Logger.Debug($"Added element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
626633
j++;
627634
}
628635
diffRoot.Add(addOp);
@@ -768,6 +775,36 @@ private static string GenerateXPath(XElement element, PathOptions pathOptions)
768775

769776
#endregion
770777

778+
#region Element and attr info
779+
private static string GetElementInfo(XElement? element)
780+
{
781+
string info = "<";
782+
if (element != null)
783+
{
784+
info += $"{element.Name}";
785+
if (element.HasAttributes)
786+
{
787+
info += $"{element.FirstAttribute?.Name}=\"{element.FirstAttribute?.Value}\"";
788+
if (element.Attributes().Count() > 1)
789+
{
790+
info += " ...";
791+
}
792+
}
793+
info += ">";
794+
}
795+
return info;
796+
}
797+
798+
private static string GetAttributeInfo(XAttribute? attr)
799+
{
800+
if (attr == null)
801+
{
802+
return "";
803+
}
804+
return $"{attr.Name}=\"{attr.Value}\"";
805+
}
806+
#endregion
807+
771808
#region Diff Validation
772809

773810
private static bool ValidateXsdPath(string? xsdPath)
@@ -782,10 +819,9 @@ private static bool ValidateXsdPath(string? xsdPath)
782819
return true;
783820
}
784821

785-
private static XmlReaderSettings CreateXmlReaderSettings(string xsdPath)
822+
private static XmlReaderSettings? CreateXmlReaderSettings(string xsdPath)
786823
{
787-
XmlReaderSettings settings = new XmlReaderSettings();
788-
824+
XmlReaderSettings? settings = null;
789825
if (ValidateXsdPath(xsdPath))
790826
{
791827
try
@@ -801,6 +837,7 @@ private static XmlReaderSettings CreateXmlReaderSettings(string xsdPath)
801837

802838
// Add the schema using the XmlReader
803839
schemaSet.Add("", reader);
840+
settings = new XmlReaderSettings();
804841
settings.DtdProcessing = DtdProcessing.Parse; // Enable DTD processing
805842
settings.ValidationType = ValidationType.Schema; // Optional, for validation during reading
806843
settings.Schemas = schemaSet;
@@ -810,10 +847,12 @@ private static XmlReaderSettings CreateXmlReaderSettings(string xsdPath)
810847
{
811848
Logger.Error($"Schema Exception: {ex.Message}");
812849
Logger.Error($"Line: {ex.LineNumber}, Position: {ex.LinePosition}");
850+
return settings;
813851
}
814852
catch (Exception ex)
815853
{
816854
Logger.Error($"General Exception: {ex.Message}");
855+
return settings;
817856
}
818857
}
819858
return settings;

0 commit comments

Comments
 (0)