Change path of trace listener programmatically

Topics: Exception Handling Application Block, Logging Application Block
Feb 26, 2011 at 1:59 PM

How can I change the path of the trace listener programmatically? I use the same program on different computers. So I want to use a string variable called strErrorLogFile. For each computer I have a different log file name. I am using visual basic 2010 and Enterprise Library 5.0.

 

Thanks!

Feb 27, 2011 at 3:11 AM
Edited Feb 27, 2011 at 3:13 AM

EntLib 5.0 provides you the Fluent Interface API.  This allows you to create your logging configuration at runtime.  One thing though is that you can't edit your existing configuration file using this API, it recreates the entire configuration file.  If you don't like this, your other option would be to use a similar code posted in this thread.

 

Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 28, 2011 at 6:09 PM
I converted the code on the thread to VB.Net but I get a error on the first line. Here is the error: Error 14 Type 'ConfigurationFileMap' is not defined. And here is my code: 
	Dim configFileMap As New ConfigurationFileMap()
        configFileMap.MachineConfigFilename = "myAppconfig.exe.config"

        Dim entLibConfig As Configuration = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)

        Dim loggingSettings__1 As LoggingSettings = DirectCast(entLibConfig.GetSection(LoggingSettings.SectionName), LoggingSettings)

        Dim fileTraceListener As FlatFileTraceListenerData = TryCast(loggingSettings__1.TraceListeners.[Get]("FlatFile TraceListener"), FlatFileTraceListenerData)

        fileTraceListener.FileName = "newFile.txt"

        entLibConfig.Save()

Mar 1, 2011 at 12:02 AM

You need to reference System.Configuration in your project and add "using System.Configuration" to the top of your file.

Mar 2, 2011 at 4:07 PM

I got it to work. Thank you very much Sarah and ctavares for your help. I thought I would post my code if anybody needs it since its vb.net.

First I added the following references:

System.Configuration

microsoft.practices.unity.interception.configuration

And here is my code:

Imports System.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Logging.Configuration


Public Sub ChangeConfigFile()

        Dim configFileMap As ConfigurationFileMap = New ConfigurationFileMap
        configFileMap.MachineConfigFilename = "MyAppName.exe.config"
        Dim entLibConfig As Configuration = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
        Dim loggingSettings As LoggingSettings = CType(entLibConfig.GetSection(loggingSettings.SectionName), LoggingSettings)
        Dim fileTraceListener As FlatFileTraceListenerData = CType(loggingSettings.TraceListeners.Get("Flat File Trace Listener"), FlatFileTraceListenerData)
        fileTraceListener.FileName = strErrorLogFile
        entLibConfig.Save()

    End Sub