473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How To: Configuration File for your Application

Hello,

Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.

AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]

RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>
RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");

static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");

if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}

Many Thanks
Peter

Nov 16 '05 #1
2 1809
Hi,

I had trouble with this a while back - I found the MSDN documentation to be
a bit flaky and inconsistent for examples of the trace facility stuff.
Anyway, I got it to work and here are snippets from a working App.config file
and TraceSwitch instantiation code {note that the 'name' attribute in the
config file must match the 'name' parameter in the TraceSwitch constructor}:

----App.config
-------------------------------------------------------------------

<system.diagnostics>
<switches>
<add name="ServiceSwitch" value="2" />
<add name="XmlDocumentSwitch" value="2" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="AtlasLogProcessor.log"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Logs\LogProcessor.log" />
</listeners>
</trace>
</system.diagnostics>

---- Source code
----------------------------------------------------------------

TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");
......

Trace.WriteLineIf(ts.TraceError, "Error");

------------------------------------------------------------------------------------

--Richard

P.S. The documentation states that changes to the config file trace switch
levels at runtime will be reflected immediately in the running program. I
have not seen this work as advertised on Server 2003. I opened an MSDN
ticket on this issue but Microsoft never got back to me {months ago}...

"Peter" wrote:
Hello,

Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.

AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]

RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>
RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");

static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");

if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}

Many Thanks
Peter

Nov 16 '05 #2
Oops, make that construction call:

static TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");

As per the documentation my TraceSwitch instance is static as they recommend
- actually in one place I think they tell you it MUST be static and in
another place they say that it can be static to improve performance - dunno
what's up with that but static was good for my needs so I made it static...

Also I don't have anything particular in my AssemblyInfo.cs file related to
my TraceSwitch settings, here's my pertinent assembly info:

[assembly: AssemblyTitle("LogProcessor")]
[assembly: AssemblyConfiguration("")]
..... {etc. nothing special for TraceSwitch's }

--Richard

"Richard" wrote:
Hi,

I had trouble with this a while back - I found the MSDN documentation to be
a bit flaky and inconsistent for examples of the trace facility stuff.
Anyway, I got it to work and here are snippets from a working App.config file
and TraceSwitch instantiation code {note that the 'name' attribute in the
config file must match the 'name' parameter in the TraceSwitch constructor}:

----App.config
-------------------------------------------------------------------

<system.diagnostics>
<switches>
<add name="ServiceSwitch" value="2" />
<add name="XmlDocumentSwitch" value="2" />
</switches>
<trace autoflush="true" indentsize="2">
<listeners>
<add name="AtlasLogProcessor.log"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\Logs\LogProcessor.log" />
</listeners>
</trace>
</system.diagnostics>

---- Source code
----------------------------------------------------------------

TraceSwitch ts = new TraceSwitch("ServiceSwitch", "");
.....

Trace.WriteLineIf(ts.TraceError, "Error");

------------------------------------------------------------------------------------

--Richard

P.S. The documentation states that changes to the config file trace switch
levels at runtime will be reflected immediately in the running program. I
have not seen this work as advertised on Server 2003. I opened an MSDN
ticket on this issue but Microsoft never got back to me {months ago}...

"Peter" wrote:
Hello,

Thanks for reviewing my question. I am trying to understand how to use a
configuration file for my application, so I can turn debug or trace with a
switch. The following is what I put together from my understanding but
doesn't work.

AssemblyInfo.cs
------------------
[assembly: AssemblyConfiguration("RegConsole.xml")]

RegConsole.xml
------------------
<configuration>
<system.diagnostics>
<switches>
<add name="mySwitch" value="4" />
</switches>
</system.diagnostics>
</configuration>
RegConsole.exe
------------------
static TraceSwitch mySwitch = new TraceSwitch("General", "Entire
Application");

static public void Main()
{
if(mySwitch.TraceError)
Console.WriteLine("My error message.");

if(mySwitch.TraceVerbose)
Console.WriteLine("My second error message.");
}

Many Thanks
Peter

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexander Kienzle | last post by:
I'm new to Java programming. I'm developing a Servlet for tomcat which needs an external configuration file. With external I mean a file (in XML format) which is customizable and not contained in...
2
by: kathy | last post by:
What Application Configuration File is? And when/where/how to use it? Any documents on that? MSDN has somrthings scatter everywhere - hard to follow. thanks,
4
by: Jessard | last post by:
Hi Guys and Girls, I have a situation where I am wishing to deploy a .NET dll onto a number of servers. The classes in the DLL will be used by VBScripts. When one of the classes - Connection -...
3
by: Florida Coder | last post by:
I have the need to store some application specific configuration data to be used by a class library and or a windows service. I would like to do this in a fashion similar to the way we do with...
6
by: chokk | last post by:
All, I am getting the following error when I try to run a form page. Can someone tell me what this is about and show me how to fix it. Thnaks. Server Error in '/' Application...
1
by: Ramanfromoz | last post by:
Hi, Developing a new we application. Everything okay on my local WIN XP PROFESSIONAL, IIS 5.0 running locally. The website is running smoothly. Now, the same code I am copying over to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
7
by: Steven Cliff | last post by:
I have started to use the new Enterprise Library (Jan 06) and have set up a skeleton project using the DAAB. This all seems to work fine apart from when I come to secure the app.config file via...
9
by: KarlM | last post by:
After reading some articles regarding confuguration data I'm a bit confused. Where is the right place for storing configuration data? - XML-files? - registry? - INI-files? (from a users point...
4
by: =?Utf-8?B?SmFzb24gUmljaG1laWVy?= | last post by:
Is there a way to catch an application configuration error on startup? I have written a windows service. If there is an error in the configuration file (such as invalid xml) the exception is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.