get the data of an app.config specifically from the listeners of the trace

0

I need to get the route that is in the InitialData of my app.config

        <sharedListeners>
        <add initializeData="c:\software\app_messages.svclog" 
             type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
            <filter type="" />
        </add>
    </sharedListeners>

string LogPathFile = < > app_messages.svclog How do I do this?

UPDATE I've posted all the app.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  
    <system.diagnostics>
      
      
        <sources>
            <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
                <listeners>
                    <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                        <filter type="" />
                    </add>
                    <add name="ServiceModelMessageLoggingListener">
                        <filter type="" />
                    </add>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add initializeData="c:\software\app_messages.svclog" 
                 type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
    <system.serviceModel>
      <diagnostics>
        <messageLogging      
          logMessagesAtTransportLevel="true"
          logMessagesAtServiceLevel="false"
          logMalformedMessages="true"
          logEntireMessage="true"
          maxSizeOfMessageToLog="65535000"
          maxMessagesToLog="500" />
      </diagnostics>
  
    </system.serviceModel>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
    </runtime>
  </configuration>
    
asked by ger 13.11.2018 в 23:11
source

3 answers

0

I solved it by getting the path where is the config of my app and then using linq you can find it!

      string ConfigFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;


        XDocument doc = XDocument.Load(ConfigFilePath);

        string RutaSvclog = doc.Descendants("add").Where(x => x.Attribute("initializeData") != null).Select(x => (string)x.Attribute("initializeData")).FirstOrDefault();
    
answered by 14.11.2018 / 15:28
source
0

If you are looking for the value of initializeData you can do it in the following way:

string logFileName="";

//Defiminimos un objeto XMLDocument y cargamos el archivo de configuración.
XmlDocument xdoc = new XmlDocument();
xdoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

//Filtramos al nodo que queremos, en este caso <sharedListeners>
XmlNode xnodes = xdoc.SelectSingleNode("/configuration/system.diagnostics/sharedListeners");

//Recorremos sus nodos hasta encontrar el <add>
foreach (XmlNode xnn in xnodes.ChildNodes)
{
    if (xnn.Name == "add")
    {
        //Recorremos los atributos hasta encontrar initializeData
        foreach(var att in xnn.Attributes)
        {
            var a= att as XmlAttribute;
            if (a != null)
            {
                if (a.Name == "initializeData")
                {
                    //store file path in the variable
                    logFileName = a.Value;
                }
            }
        }
    }
}
Console.WriteLine(logFileName);
    
answered by 13.11.2018 в 23:58
0

I would recommend using the configuration class and not xml to access the data, something like this

ConfigurationSection diagnosticsSection = (ConfigurationSection)ConfigurationManager.GetSection("system.diagnostics");
ConfigurationElementCollection listeners = diagnosticsSection.ElementInformation.Properties["sharedListeners"].Value as ConfigurationElementCollection; 

foreach (ConfigurationElement listener in listeners)
{
    string initializeData= listener.ElementInformation.Properties["initializeData"].Value.ToString();
}

This case is just an example where you get the property to see if this way you can get to the data

    
answered by 14.11.2018 в 15:49