Define log path programmatically

Topics: Logging Application Block
Feb 8, 2010 at 5:57 AM

How may I change the path of the trace listener programmatically? Is this possible?

Feb 8, 2010 at 6:26 AM
Edited Feb 8, 2010 at 6:28 AM

You will need to modify the config during runtime.

ConfigurationFileMap configFileMap = new ConfigurationFileMap(); 
configFileMap.MachineConfigFilename = "myAppconfig.exe.config";

Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); 

LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName); 

FlatFileTraceListenerData fileTraceListener = loggingSettings.TraceListeners.Get("FlatFile TraceListener") as FlatFileTraceListenerData; 

fileTraceListener.FileName = "newFile.txt"; 

entLibConfig.Save();


Another approach to modifying the config at runtime - http://msdn.microsoft.com/en-us/library/dd203211.aspx.

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

 

Feb 8, 2010 at 7:48 AM

I have another problem too. It is that I don't know how many trace listeners I will need until run-time and for every trace listener I need a category so that I can route the logs based on the categories to the different trace listeners. So what would be the best solution to create the trace listeners plus the categories at run-time and then log with the the Logger class.

Feb 8, 2010 at 8:06 AM

The only way would be to modify the configuration during runtime.  Assign that responsibility to a helper class or something.  Currently, the API for doing this is not very intuitive but the fluent interface in the next version would address this.

 

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

Apr 15, 2010 at 8:35 PM
Edited Apr 15, 2010 at 9:28 PM

Hi all,

I have a CustomListener, is it possible modify this custom properties ?? Thanksin advanced.

The class of my custom listener defined as:

[Assembler(typeof(AuthenticatingEmailTraceListenerAssembler))]
public class AuthenticatingEmailTraceListenerData: TraceListenerData

[ConfigurationElementType(typeof(AuthenticatingEmailTraceListenerData))]

public class AuthenticatingEmailTraceListener: Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedTraceListenerBase

in loggingconfiguration.config...

add name="AuthEmailTraceListener" 
 
type="GrupoBackup.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListener, GrupoBackup.Frk.Logging.AuthEmailTraceListener" 
listenerDataType="GrupoBackup.Frk.Logging.AuthEmailTraceListener.AuthenticatingEmailTraceListenerData, GrupoBackup.Frk.Logging.AuthEmailTraceListener" 
formatter="Exception Formatter" 
traceOutputOptions="None" 
toAddress=incidenciasxxxx@gmail.com 
fromAddress=incidenciasxxx@gmail.com 
subjectLineStarter="OGF Expediente Electronico. Excepci¢n detectada - " 
subjectLineEnder="IncidenciasOGF" 
smtpServer="smtp.gmail.com" 
smtpPort="587" authenticate="true"
username=incidenciasxxxx@gmail.com 
password="xxxxx" 
enableSsl="true"

/>

Apr 16, 2010 at 12:46 AM

You mean modify it during runtime?

 

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

Apr 16, 2010 at 7:37 AM

Yes, I have winforms app for configure windows services.

I need open the app.config file of a windows services, in my Winforms app, and in runtime modify properties in listeners, and categories perhaps.

Thanks mister Urmeneta.

Apr 16, 2010 at 7:42 AM

You would use the same approach as the code I pasted above.

ConfigurationFileMap configFileMap = new ConfigurationFileMap(); 
configFileMap.MachineConfigFilename = "myAppconfig.exe.config";

Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); 

LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName); 

AuthenticatingEmailTraceListenerData emailTraceListener = loggingSettings.TraceListeners.Get("Auth Email TraceListener") as AuthenticatingEmailTraceListenerData 

 

you can then modify here the properties of the emaiTraceListener object. 

That's miss by the way, not mister :)

 

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