Using Special Folders in App.Config

S

Stewart Berman

How do you define one of the special folders in an App.Config file? For example, if one wanted to
refer to the user's temp directory in code it would be:
My.Computer.FileSystem.SpecialDirectories.Temp.ToString()

I want to define a trace output file in the user's temp directory using the application's
configuration file. What should the xml look like?
 
J

Jie Wang [MSFT]

Hi Stewart,

I'm afraid there is no other way to create the trace log file in the temp
directory except using code.

Dim logFilename As String
Dim listener As TraceListener

logFilename = Path.Combine(Path.GetTempPath(), "my.log")
listener = New TextWriterTraceListener(logFilename)

Trace.Listeners.Add(listener)
Trace.WriteLine("App started: " & DateTime.Now)

I once tried using envrionment variable like this:
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener,
system, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="%temp%\my.log" />

But eventually the %temp% was not expanded to its real value at runtime
(the TextWriterTraceListener actually calls Path.GetFullPath of the path
specified in config file but that method won't expand the environment
variables).

So you need to add some code to config the listener at runtime to get the
log file written to user's temp directory.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Stewart Berman

system, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="%temp%\my.log" />

How do you know what to use for Version (or system version -- see below) and PublicKeyToken? For
example, http://msdn.microsoft.com/en-us/library/xe8whywc.aspx has:
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener, system version=1.0.3300.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" initializeData="c:\myListener.log" />
as an example.
 
S

Stewart Berman

(the TextWriterTraceListener actually calls Path.GetFullPath of the path
specified in config file but that method won't expand the environment
variables).

Sounds like an oversight.
So you need to add some code to config the listener at runtime to get the
log file written to user's temp directory.

If the trace file is defined in the application's configuration file it is availble to the first bit
of code that uses trace. To create the trace listener in code you have to find the earliest point
in the code that might use it -- which can be a bit of a problem.
 
S

Stewart Berman

If the trace file is defined in the application's configuration file it is availble to the first bit
of code that uses trace. To create the trace listener in code you have to find the earliest point
in the code that might use it -- which can be a bit of a problem.

<Forgive replying to my own post -- I need to add something>
I am trying to figure out where to put the trace listener's creation using the %Temp% environment
variable in the code.

I ran a trace of everything during application startup:

20090709 23:17:18.759: .ctor Tracing Started
20090709 23:17:18.790: MyApplication creator
20090709 23:17:18.790: .ctor Tracing Ended
20090709 23:17:18.821: OnCreateSplashScreen Tracing Started
20090709 23:17:18.821: .ctor Tracing Started
20090709 23:17:18.821: Splash Screen Creator
20090709 23:17:18.931: .ctor Tracing Ended
20090709 23:17:18.931: OnCreateSplashScreen Tracing Ended
20090709 23:17:18.931: MyApplication_Startup Tracing Started
20090709 23:17:18.931: MyApplication_Startup Tracing Ended
20090709 23:17:18.931: OnCreateMainForm Tracing Started
20090709 23:17:18.931: SplashScreen_Load Tracing Started
20090709 23:17:18.946: .ctor Tracing Started
20090709 23:17:18.946: MyApplication creator
20090709 23:17:18.946: .ctor Tracing Ended
20090709 23:17:18.946: Ver18ion: 1.0.0.72
20090709 23:17:18.946: SplashScreen_Load Tracing Ended

The first .ctor is the creator (New method) in the Application.Designer.vb file for the
MyApplication class. Unfortunately, this code is auto generated has a big warning:
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.

The MyApplicatoin_Startup method is not called until after the Splash Screen's creator (New method)
is called. However, it appears the earliest safe point to put the trace listener's creation code.


Stewart Berman said:
(the TextWriterTraceListener actually calls Path.GetFullPath of the path
specified in config file but that method won't expand the environment
variables).

Sounds like an oversight.
So you need to add some code to config the listener at runtime to get the
log file written to user's temp directory.

If the trace file is defined in the application's configuration file it is availble to the first bit
of code that uses trace. To create the trace listener in code you have to find the earliest point
in the code that might use it -- which can be a bit of a problem.

[email protected] ("Jie Wang [MSFT]") said:
Hi Stewart,

I'm afraid there is no other way to create the trace log file in the temp
directory except using code.

Dim logFilename As String
Dim listener As TraceListener

logFilename = Path.Combine(Path.GetTempPath(), "my.log")
listener = New TextWriterTraceListener(logFilename)

Trace.Listeners.Add(listener)
Trace.WriteLine("App started: " & DateTime.Now)

I once tried using envrionment variable like this:
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener,
system, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
initializeData="%temp%\my.log" />

But eventually the %temp% was not expanded to its real value at runtime
(the TextWriterTraceListener actually calls Path.GetFullPath of the path
specified in config file but that method won't expand the environment
variables).

So you need to add some code to config the listener at runtime to get the
log file written to user's temp directory.

Best regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

How do you know what to use for Version (or system version --
see below) and PublicKeyToken?

Several ways to get that, either by code or by tools. Let me give one
sample for each way.

By code:

Dim asmQualifiedName As String
asmQualifiedName = GetType(TextWriterTraceListener).AssemblyQualifiedName

You will now have the following string in asmQualifiedName:

System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089

By tool:

Use the .NET Reflector. If you don't have it, I strongly recommend it, a
must have everyday tool for .NET developers.
Select the System assembly in the .NET Reflector, you'll see the name of
the assembly:

System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

You can get .NET reflector here:
http://www.red-gate.com/products/reflector/

Disclaimer: the link above is a 3rd party website not owned nor controlled
by Microsoft, use it at your own risk.

Hope this helps.

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jie Wang [MSFT]

Let me share some of my thoughts:

1. The MyApplication.Startup event should be a safe place to initialize the
TraceListeners. This event is designed to let developers to put their
initialization logics in it. Usually we don't put our own code before this
point. The splash screen usually doesn't contain code that need to be
traced, or consider move the code to the Startup event handler.

2. If your application is single instance - that is, only one instance is
allowed to run for one logged on user, and you want to trace the follow up
instances (althought they will quite right away), you can add trace code in
the MyApplication.StartupNextInstance event. For more details, see:
http://msdn.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

3. If you use code to set the trace file path (into the %temp% dir), no
need to use environment variable, just use Path.GetTempPath method will do:
Path.Combine(Path.GetTempPath(), "my.log")

Regards,

Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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