protected override void ValidateConfiguration() { using (StreamReader sr = new StreamReader(DiagnosticsConfigurationPath)) { string header = sr.ReadLine(); // make sure it is the header if (!header.Trim().StartsWith("<?xml")) { throw new ArgumentException(Resources.DiagnosticsExtensionWrongHeader); } } XmlDocument doc = new XmlDocument(); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("ns", XmlNamespace); doc.Load(DiagnosticsConfigurationPath); // Make sure the configuration elements exist var publicConfigElement = doc.SelectSingleNode("//ns:PublicConfig", ns); if (publicConfigElement == null) { throw new ArgumentException(Resources.DiagnosticsExtensionNullPublicConfig); } var wadConfigElement = doc.SelectSingleNode("//ns:WadCfg", ns) ?? doc.SelectSingleNode("//ns:WadCfgBlob", ns); if (wadConfigElement == null) { throw new ArgumentException(Resources.DiagnosticsExtensionPaaSConfigElementNotDefined); } // The element <StorageAccount> is not meant to be set by the user in the public config. // Make sure it matches the storage account in the private config. var storageAccountElement = doc.SelectSingleNode("//ns:StorageAccount", ns); if (storageAccountElement == null) { // The StorageAccount element is not there, we create one storageAccountElement = doc.CreateElement("StorageAccount", XmlNamespace); wadConfigElement.ParentNode.AppendChild(storageAccountElement); } if (!string.IsNullOrEmpty(storageAccountElement.InnerText) && string.Compare(storageAccountElement.InnerText, StorageAccountName, true) != 0) { WriteWarning(Resources.DiagnosticsExtensionNoMatchStorageAccount); } storageAccountElement.InnerText = StorageAccountName; PublicConfiguration = publicConfigElement.OuterXml; // Make sure the storage account name in PrivateConfig matches. var privateConfigStorageAccountName = DiagnosticsHelper.GetStorageAccountInfoFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.PrivConfNameAttr); if (!string.IsNullOrEmpty(privateConfigStorageAccountName) && !string.Equals(StorageAccountName, privateConfigStorageAccountName, StringComparison.OrdinalIgnoreCase)) { WriteWarning(Resources.DiagnosticsExtensionNoMatchPrivateStorageAccount); } PrivateConfigurationXml = new XDocument(PrivateConfigurationXmlTemplate); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfNameAttr, StorageAccountName); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfKeyAttr, StorageAccountKey); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfEndpointAttr, StorageAccountEndpoint); PrivateConfiguration = PrivateConfigurationXml.ToString(); }
protected override void ValidateConfiguration() { using (StreamReader sr = new StreamReader(DiagnosticsConfigurationPath)) { string header = sr.ReadLine(); // make sure it is the header if (!header.Trim().StartsWith("<?xml")) { throw new ArgumentException(Resources.DiagnosticsExtensionWrongHeader); } } var publicConfigElem = DiagnosticsHelper.GetPublicConfigXElementFromXmlFile(this.DiagnosticsConfigurationPath); if (publicConfigElem == null) { throw new ArgumentException(Resources.DiagnosticsExtensionNullPublicConfig); } publicConfigElem.SetAttributeValue("xmlns", XmlNamespace); PublicConfiguration = publicConfigElem.ToString(); // The element <StorageAccount> is not meant to be set by the user in the public config. // Make sure it matches the storage account in the private config. XmlDocument doc = new XmlDocument(); XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("ns", XmlNamespace); doc.Load(DiagnosticsConfigurationPath); var node = doc.SelectSingleNode("//ns:StorageAccount", ns); if (node != null) { // The StorageAccount is empty, we must set it if (string.IsNullOrEmpty(node.InnerText)) { var insertIndex = PublicConfiguration.IndexOf("</StorageAccount>"); PublicConfiguration = PublicConfiguration.Insert(insertIndex, StorageAccountName); } else if (!string.IsNullOrEmpty(node.InnerText) && string.Compare(node.InnerText, StorageAccountName, true) != 0) { throw new ArgumentException(Resources.DiagnosticsExtensionNoMatchStorageAccount); } } else { // the StorageAccount is not there. we must set it string storageAccountElem = "\n<StorageAccount>" + StorageAccountName + "</StorageAccount>\n"; // insert it after </WadCfg> int wadCfgEndIndex = PublicConfiguration.IndexOf("</WadCfg>"); PublicConfiguration = PublicConfiguration.Insert(wadCfgEndIndex + "</WadCfg>".Length, storageAccountElem); } // Make sure the storage account name in PrivateConfig matches. var privateConfigStorageAccountName = DiagnosticsHelper.GetStorageAccountInfoFromPrivateConfig(this.DiagnosticsConfigurationPath, DiagnosticsHelper.PrivConfNameAttr); if (!string.IsNullOrEmpty(privateConfigStorageAccountName) && !string.Equals(StorageAccountName, privateConfigStorageAccountName, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(Resources.DiagnosticsExtensionNoMatchPrivateStorageAccount); } PrivateConfigurationXml = new XDocument(PrivateConfigurationXmlTemplate); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfNameAttr, StorageAccountName); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfKeyAttr, StorageAccountKey); SetPrivateConfigAttribute(DiagnosticsHelper.StorageAccountElemStr, DiagnosticsHelper.PrivConfEndpointAttr, StorageAccountEndpoint); PrivateConfiguration = PrivateConfigurationXml.ToString(); }