app.config configuration file

T

Tony Johansson

Hello!

Here is a complete program.that is using app.config file. I have added this
app.config to the project.
The file app.config looks like this
<?xml version="1.0" encoding="utf-8" ?>
<system.diagnostics>
<switches>
<add name="DataTraceSwitch" value="1"/>
<add name="MessageTraceSwitch" value="3"/>
</switches>
</system.diagnostics>

This program run into ConfigurationErrorException when the first row in
method TestTraceSwitches
is being executed. The Exception dialog box says it was not possible to
initialize the configurationsystem.

Can somebody give me a hint why I get this exception ?

using System;
using System.Collections;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Collections.Generic;

namespace ConsoleApplication20
{
class Program
{
private static TraceSwitch dataSwitch;
private static TraceSwitch messageSwitch;

private static void TestTraceSwitches(int Input)
{
Trace.WriteLineIf(messageSwitch.Level == TraceLevel.Info,
"Entering TestTraceSwitches method.");
Trace.WriteLineIf(dataSwitch.Level == TraceLevel.Info,
"Input: " + Input);

if (Input <= 0)
{
Trace.WriteLine(dataSwitch.Level == TraceLevel.Error,
"Input cannot be less then or equal to 0.");
Trace.WriteLine(messageSwitch.Level == TraceLevel.Error,
"Exception thrown in TestTraceSwitches method.");
throw new ArgumentException("Invalid value.", "Input");
}
Trace.WriteLineIf(messageSwitch.Level == TraceLevel.Info,
"Exiting TestTraceswitches method");
}


static void Main(string[] args)
{
dataSwitch = new TraceSwitch("DataTraceSwitch",
"Display argument information.");
messageSwitch = new TraceSwitch("MessageTraceSwitch",
"Display method calling information.");
TestTraceSwitches(0);
}
}
}
 
W

Willem van Rumpt

<?xml version="1.0" encoding="utf-8" ?>
<system.diagnostics>
<switches>
<add name="DataTraceSwitch" value="1"/>
<add name="MessageTraceSwitch" value="3"/>
</switches>
</system.diagnostics>

Assuming that the above (as you posted it) is the actual content of your
app.config file:

Yes, I do have a hint. And some advice.

*The hint part:*
The contents of the app.config file are not valid. Making it valid is
enough to make the program work.

*The advice part:*
If you can't see what's wrong with it, and you're not able to figure
this out by yourself by now (given your time spent on preparing for a
..NET exam), then I strongly advice you to postpone the exam (at least),
or forget about taking the exam completely.
 
J

Jeff Johnson

Assuming that the above (as you posted it) is the actual content of your
app.config file:

Yes, I do have a hint. And some advice.

*The hint part:*
The contents of the app.config file are not valid. Making it valid is
enough to make the program work.

*The advice part:*
If you can't see what's wrong with it, and you're not able to figure this
out by yourself by now (given your time spent on preparing for a .NET
exam), then I strongly advice you to postpone the exam (at least), or
forget about taking the exam completely.

Okay, perhaps I'm not qualified to be a programmer after all. I can't see
anything wrong with that XML, at least as far as being well-formed XML. What
am I missing? Is it something only someone who has been studying the
System.Configuration namespace would know? System.Diagnostics casing?
 
W

Willem van Rumpt

Can somebody give me a hint why I get this exception ?

Rereading my first reply, I apologize for the inappropriate tone
of my response. It was unnecessary.

I still stick with the general sentiment of my response, that this
should be solvable by someone attempting to take an exam for .NET,
but I could've done so in more appropriate and helpful way.

If you still haven't found the solution:

The outer <container> element is missing.
Wrap the <system.diagnotics>...</system.diagnostics> part in it, and it
will work.
 
W

Willem van Rumpt

The XML is fine. I assume that Willem is referring to the lack of a
<configuration> element, which is the root-level element found in a
correct app.config file.

That would be it, yes.
the form of the reply gives me an impression
very similar to the classic "well, if you don't know what's wrong, I'm
certainly not going to tell you!" line from TV sit-com dysfunctional
relationships.

Sadly, I have to agree with this: I was unnecessarily harsh. I still
stand by my feeling that this issue should not pose any problem, but I
should've phrased it more appropriately.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top