diff --git a/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs b/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs index 893c724..c68e62f 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Manager - MSMQManager.cs @@ -232,19 +232,22 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { var msmq = p.Parameters as MSMQ; msmq.DisplayInfo(); ix++; - var sp = SendParameters.Get(msmq.Xml_Encoding, msmq.Xml_Formatter); + var sp = SendParameters.Get(msmq,msmq.Xml_Encoding, msmq.Xml_Formatter); if (sp != null) { - var fullpath = msmq.Xml_FullPath; - MSMQManagerCore.Send(fullpath, sp.messagebody, sp.messagelabel, sp.messageformatter, sp.encoding); - ColorConsole.WriteLine($"Sending to '{fullpath}' ....", ConsoleColor.Yellow); - Message msg = MSMQManagerCore.PeekLast(fullpath, sp.messageformatter, sp.encoding, out string body, out string label); - TestSendCheckMessage(fullpath, msg, sp, body, label, "message sending/peek back"); - var ans = ColorConsole.ReadLine($"Do You want to remove this test message (lookup id='{msg.LookupId}') from quue?", ConsoleColor.Yellow, validitylist: new List { "yes", "no" }, defaultvalue: "yes"); - if (ans.ToLower() == "yes") - { - msg = MSMQManagerCore.ReadById(fullpath, msg.LookupId, sp.messageformatter, sp.encoding, out body, out label); - TestSendCheckMessage(fullpath, msg, sp, body, label, "Read back."); + for (var msgindex=1; msgindex <= sp.numofmessages; msgindex++) + { + sp.Substitute(msgindex); + MSMQManagerCore.Send(msmq.Xml_FullPath, sp.messagebodysubstituted, sp.messagelabelsubstituted, sp.messageformatter, sp.encoding); + ColorConsole.WriteLine($"Sending to '{msmq.Xml_FullPath}' ....", ConsoleColor.Yellow); + Message msg = MSMQManagerCore.PeekLast(msmq.Xml_FullPath, sp.messageformatter, sp.encoding, out string body, out string label); + TestSendCheckMessage(msmq.Xml_FullPath, msg, sp, body, label, "message sending/peek back"); + var ans = ColorConsole.ReadLine($"Do You want to remove this test message (lookup id='{msg.LookupId}') from quue?", ConsoleColor.Yellow, validitylist: new List { "yes", "no" }, defaultvalue: "yes"); + if (ans.ToLower() == "yes") + { + msg = MSMQManagerCore.ReadById(msmq.Xml_FullPath, msg.LookupId, sp.messageformatter, sp.encoding, out body, out label); + TestSendCheckMessage(msmq.Xml_FullPath, msg, sp, body, label, "Read back."); + } } } } @@ -382,12 +385,14 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS { try { - var sp = SendParameters.Get(st.Xml_Encoding, st.Xml_Formatter); + var sp = SendParameters.Get(st,st.Xml_Encoding, st.Xml_Formatter); if (sp == null) { break; } - - var fullpath = st.Xml_FullPath; - MSMQManagerCore.Send(fullpath, sp.messagebody, sp.messagelabel, sp.messageformatter, sp.encoding); - ColorConsole.WriteLine($"Message sent to MSMQ:{st.Xml_Name}.", ConsoleColor.Green); + for (var msgindex = 1; msgindex <= sp.numofmessages; msgindex++) + { + sp.Substitute(msgindex); + MSMQManagerCore.Send(st.Xml_FullPath, sp.messagebodysubstituted, sp.messagelabelsubstituted, sp.messageformatter, sp.encoding); + ColorConsole.WriteLine($"Message sent to MSMQ:{st.Xml_Name}.", ConsoleColor.Green); + } } catch (ApplicationException ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } catch (Exception ex) { ColorConsole.WriteLine(ex.Message, ConsoleColor.Red); } @@ -419,32 +424,68 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS } private class SendParameters { + public string messagebodysubstituted; + public string messagelabelsubstituted; public string messagebody; public string messagelabel; public IMessageFormatter messageformatter; public Encoding encoding; + public int numofmessages; public SendParameters() { } - public static SendParameters Get(string defaultencoding, string defaultformatter) + public SendParameters Substitute(int index) + { + var dtnow = DateTime.Now; + messagebodysubstituted = messagebody.Replace("{IX}", $"{index}").Replace("{TS}", dtnow.ToString("HHmmss")).Replace("{DS}", dtnow.ToString("yyyyMMdd")).Replace("{DTS}", dtnow.ToString("yyyyMMddHHmmss")); + messagelabelsubstituted = messagelabel.Replace("{IX}", $"{index}").Replace("{TS}", dtnow.ToString("HHmmss")).Replace("{DS}", dtnow.ToString("yyyyMMdd")).Replace("{DTS}", dtnow.ToString("yyyyMMddHHmmss")); + return this; + } + public static SendParameters Get(MSMQ st, string defaultencoding, string defaultformatter) { - ColorConsole.WriteLine("Enter test message data!", ConsoleColor.Yellow); + ColorConsole.Write("Enter test message data", ConsoleColor.Yellow); + if (st.MessageTemplates != null && st.MessageTemplates.Any()) { ColorConsole.Write(", or select one from the predefined list!", ConsoleColor.Yellow); } + ColorConsole.WriteLine("!", ConsoleColor.Yellow); + ColorConsole.WriteLine("Use {IX},{DS},{TS},{DTS} variables in the message text to substitute them with the message number index,date stamp,time stamp and date-time stamp, respectively.", ConsoleColor.Yellow); + Console.WriteLine(); + var templatekeylist = new Dictionary(); + if (st.MessageTemplates != null && st.MessageTemplates.Any()) + { + var index = 1; + foreach (var mt in st.MessageTemplates) + { + var tk = $"S{index}"; templatekeylist.Add(tk, index - 1); + ColorConsole.WriteLine(mt.Xml_TemplateString, ConsoleColor.White, prefix:$"[{tk}] "); + index++; + } + } + var sp = new SendParameters() { }; var now = DateTime.Now.ToString(); - sp.messagebody = ColorConsole.ReadLine("Enter message body:", ConsoleColor.Yellow, defaultvalue: "TEST MESSAGE " + now); - if (sp.messagebody.ToUpper() == "EX") { return null; } + var messagebody = ColorConsole.ReadLine("Enter message body (text or template key):", ConsoleColor.Yellow, defaultvalue: "TEST MESSAGE " + now); + if (messagebody.ToUpper() == "EX") { return null; } + if (templatekeylist.ContainsKey(messagebody)) messagebody = st.MessageTemplates.ElementAt(templatekeylist[messagebody]).Xml_TemplateString; + sp.messagebody = messagebody; sp.messagelabel = ColorConsole.ReadLine("Enter message label:", ConsoleColor.Yellow, defaultvalue: "TESTLABEL" + now); if (sp.messagelabel.ToUpper() == "EX") { return null; } + const string VALUEISINCORRECT = "{0} value is incorrect! Value:{1}. Repeat data entry!"; + getformatter: var messageformatterstring = ColorConsole.ReadLine("Enter formatter:", ConsoleColor.Yellow, validitylist: MSMQManagerCore.GetFormatterNames(), defaultvalue: defaultformatter); if (messageformatterstring.ToUpper() == "EX") { return null; } sp.messageformatter = MSMQManagerCore.SetFormatter(messageformatterstring); - if (sp.messageformatter == null) { var exmsg = $"Message formatter value is incorrect! Value:{messageformatterstring }."; throw new ApplicationException(exmsg); } + if (sp.messageformatter == null) { Console.WriteLine(string.Format(VALUEISINCORRECT ,"Message formatter",messageformatterstring),ConsoleColor.Red); goto getformatter; } + getencoder: string encodingstring = ColorConsole.ReadLine("Enter encoding:", ConsoleColor.Yellow, validitylist: MSMQManagerCore.GetEncodingNames(), defaultvalue: defaultencoding); if (encodingstring.ToUpper() == "EX") { return null; } sp.encoding = MSMQManagerCore.SetEncoding(encodingstring); - if (sp.encoding == null) { var exmsg = $"Message encoder value is incorrect! Value:{encodingstring}."; throw new ApplicationException(exmsg); } + if (sp.encoding == null) { Console.WriteLine(string.Format(VALUEISINCORRECT, "Message encoder",encodingstring), ConsoleColor.Red); goto getencoder; } + getnumofmessages: + string numofmessagesstring = ColorConsole.ReadLine("Enter number of messages:", ConsoleColor.Yellow, defaultvalue: "1"); + if (numofmessagesstring.ToUpper() == "EX") { return null; } + if (!int.TryParse(numofmessagesstring, out sp.numofmessages) || sp.numofmessages<=0) { sp.numofmessages = -1; } + if (sp.numofmessages == -1) { Console.WriteLine(string.Format(VALUEISINCORRECT, "Number of messages",numofmessagesstring), ConsoleColor.Red); goto getnumofmessages; } return sp; } } @@ -836,7 +877,14 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS var msmqxmllist = GetAllXElements(nameof(MSMQ.XmlStructure.MSMQManager.MessageQueues), nameof(MSMQ.XmlStructure.MSMQManager.MessageQueues.MessageQueue)); if (msmqxmllist != null && msmqxmllist.Any()) { - foreach (var msmqxml in msmqxmllist) { var st = new MSMQ(msmqxml, new MSMQDefaults(RootElement)); if (st.Valid) { _msmqlist.Add(st); } } + var msmqdefaults = new MSMQDefaults(RootElement); + var msmqmsgtemplates = new MSMQMessageTemplates(RootElement); + foreach (var msmqxml in msmqxmllist) + { + var msmq = new MSMQ(msmqxml, msmqdefaults); + msmq.AddTemplates(msmqmsgtemplates.GetTemplatesForMSMQ(msmq.Xml_Key)); + if (msmq.Valid) { _msmqlist.Add(msmq); } + } } } #endregion constructor @@ -876,6 +924,35 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public MessageQueueAccessRights ar; public AccessControlEntryType ac; } + public class MSMQMessageTemplates : XmlLinqBase + { + public List Xml_MessageTemplateList=new List(); + public MSMQMessageTemplates(XElement rootxml) + { + var messagetemplatesxmls = GetXElement(rootxml, nameof(MSMQ.XmlStructure.MSMQManager.MessageTemplates)); + var messagetemplatexmllist = GetAllXElements(messagetemplatesxmls,nameof(MSMQ.XmlStructure.MSMQManager.MessageTemplates.MessageTemplate)); + if (messagetemplatexmllist == null || !messagetemplatexmllist.Any()) return; + foreach (var mtxml in messagetemplatexmllist) { var mt = new MSMQMessageTemplate(mtxml); if (mt.isValid) { Xml_MessageTemplateList.Add(mt); } } + } + public List GetTemplatesForMSMQ(string msmqid) + { + return this.Xml_MessageTemplateList.Where(mt=> mt.Xml_QueueIdList==null || !mt.Xml_QueueIdList.Any() || mt.Xml_QueueIdList.Contains("*") || mt.Xml_QueueIdList.Contains(msmqid)).ToList(); + } + } + public class MSMQMessageTemplate : XmlLinqBase + { + public string Xml_TemplateString; + public bool isValid = true; + public List Xml_QueueIdList; + public MSMQMessageTemplate(XElement messagetemplatexml) + { + Xml_TemplateString = GetValue(messagetemplatexml, ""); + if (string.IsNullOrWhiteSpace(Xml_TemplateString)) { isValid = false; return; } + Xml_QueueIdList = new List(); + var queueidlistcsvstring = GetValue(nameof(MSMQ.XmlStructure.MSMQManager.MessageTemplates.MessageTemplate.Attributes.QueueIdList), messagetemplatexml, MSMQ.XmlStructure.MSMQManager.MessageTemplates.MessageTemplate.Attributes.QueueIdList.Values.DEFAULT); + if (!string.IsNullOrWhiteSpace(queueidlistcsvstring)) Xml_QueueIdList = queueidlistcsvstring.Split(new char[] { ',', ';' },StringSplitOptions.RemoveEmptyEntries).ToList(); + } + } public class MSMQDefaults : XmlLinqBase { public string Xml_MessageFilesDirectory; @@ -920,6 +997,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public string Xml_Formatter = XmlStructure.MSMQManager.General.DefaultFormatter.Values.DEFAULT; public string Xml_Encoding = XmlStructure.MSMQManager.General.DefaultEncoding.Values.DEFAULT; public List Xml_AccessRights = new List(); + public List MessageTemplates = new List(); public string Status; public string StatusDescription = null; @@ -964,6 +1042,7 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS } #endregion constructor from MessageQueue + public MSMQ AddTemplates(List mtl) { MessageTemplates = mtl; return this; } #region xml constructor public MSMQ(XElement msmqxml, MSMQDefaults msmqdefaults) { @@ -1291,6 +1370,16 @@ namespace Vrh.Log4Pro.MaintenanceConsole.MSMQManagerNS public static class DefaultEncoding { public static class Values { public const string DEFAULT = nameof(System.Text.Encoding.UTF8); } } } + public static class MessageTemplates + { + public static class MessageTemplate + { + public static class Attributes + { + public static class QueueIdList { public static class Values { public const string DEFAULT = ""; } } + } + } + } public static class MessageQueues { public static class MessageQueue diff --git a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs index 3f7de57..e8255f4 100644 --- a/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs +++ b/Vrh.Log4Pro.MaintenanceConsole/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.16.3.0")] -[assembly: AssemblyFileVersion("1.16.3.0")] +[assembly: AssemblyVersion("1.17.0.0")] +[assembly: AssemblyFileVersion("1.17.0.0")] -- libgit2 0.21.2