Click here to Skip to main content
15,886,660 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

I need to work with the listeners child nodes "add" (get and set attributes such as name, toAddress,fromAddress, subjectLineStarter, subjectLineEnder, smtpServer,smtpPort, filter, rollSizeKB, fileName) in "loggingConfiguration" Section by using Linq from a third party app.config (not my application's configuration file).
How can I do this?

Please find below the third party app.config from which I've to get information and save back to modifications.

Best Regards,
TEK001

XML
<loggingConfiguration name="ITE Logging Application Block" tracingEnabled="true" defaultCategory="Info" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add name="IteProjectEmailAdministrator" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.EmailTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" toAddress="aa@zz.com" fromAddress="noreply-ddsproject@zz.com" subjectLineStarter="ITEProject Critical Logs" subjectLineEnder="Urgent" smtpServer="smtp.zz.net" smtpPort="25" formatter="Text Formatter" traceOutputOptions="None" filter="Off" />
      <add name="IteProjectErrorRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectError.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Error" />
      <add name="IteProjectExceptionRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectException.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="All" />
      <add name="IteProjectInformationRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectInfo.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Information" />
      <add name="IteProjectWarningRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectWarning.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Warning" />
      <add name="IteProjectCriticalRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectCritical.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Critical" />
      <add name="IteProjectDebugRollingFlatFile" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=45fr1254ee458e89" fileName="C:\logs\LogIteProjectDebug.log" footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment" rollInterval="None" rollSizeKB="100" timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="Verbose" />
    </listeners>
Posted
Updated 24-Apr-15 1:14am
v3
Comments
Maciej Los 24-Apr-15 7:19am    
What have you tried? Where are you stuck?
tek001 24-Apr-15 7:28am    
Hi,

I tried treating the app.config file as an XMLFIle so I did this to get the chlidNode of the node <listeners>

:

public void getListeners(string appName)
{
try

{


string FILE_NAME = string.Concat(appName, ".config");

XmlTextReader reader = new XmlTextReader(FILE_NAME);

XmlDocument doc = new XmlDocument();

doc.Load(reader);

reader.Close();

string nodeRoute = "loggingConfiguration";


XmlNodeList elemList = doc.GetElementsByTagName(nodeRoute);

{
foreach (XmlNode elem in elemList)
{

foreach (XmlNode elemChild in elem.ChildNodes)
{
if (elemChild.Name.ToString() == "listeners")
{
int cpt = 0;

foreach (XmlNode item in elemChild.ChildNodes)
{
MessageBox.Show(item.OuterXml.ToString());

cpt++;
}

}
}

}



}





}
catch (Exception ex)
{

errorHandler(ex);

}


}
Now how can I get the different attributes separately?
And since the OuterXml property is read only, how can I do to set values?


Thanks for your help.

Best regards,
Tek001
John C Rayan 24-Apr-15 8:20am    
Hi tek001
Your code is not LINQ. Check my solution

1 solution

I have shown you how to change fromAddress attribute. You take the idea and can apply to other attributes. Change the file location to your config file.

XML
string xFile = @"C:\John Rayan\LogConfig.xml";
     XDocument xDoc = XDocument.Load(xFile);
     List<XElement> adds = (from add in xDoc.Descendants("add")
                            select add).ToList();
     adds.ForEach(add => { if(add.Attribute("fromAddress") != null) add.Attribute("fromAddress").Value = "myname@xx.com"; });
     xDoc.Save(xFile);
 
Share this answer
 
v2
Comments
tek001 24-Apr-15 8:29am    
Thanks a lot, it works!
John C Rayan 24-Apr-15 9:13am    
Happy to help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900