Get log file from system.diagnostic.sharedListeners section of a config file


Some times ago, I had to find problematically full path of Logging file, and open it through the Shell execute. I thought it will be easy, just get the file path from configuration file and open the file with shell execute. But things are different, because you cannot reach system.diagnostic section regularly.

Suppose we have configuration file with diagnostic section defined similar like picture above.

The solution must be very common. Open the config file, find section about logging, and read the path of logging file. The code snippet below shows how to achieve this.

string logFileName="";

//Define XMLDocument for reading config file in to XML doc.
XmlDocument xdoc = new XmlDocument();

//Filter desired section
XmlNode xnodes = xdoc.SelectSingleNode("/configuration/system.diagnostics/sharedListeners");

//enumerate nodes in the section
foreach (XmlNode xnn in xnodes.ChildNodes)
    //when the right node is met
    if (xnn.Name == "add")
        //enumerate all itc atrubutes
        foreach(var att in xnn.Attributes)
            var a= att as XmlAttribute;
            if (a != null)
               //and get the right one
                if (a.Name == "fileName")
                    //store file path in the variable
                    logFileName = a.Value;

//Open the log file from shell.
ProcessStartInfo psi = new ProcessStartInfo(openFileDialog1.FileName);
psi.UseShellExecute = true;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s