I'm using Enterprise Library 3.1 and want to programmatically access the Logging Block (runtime, object model) specifically its Trace Listeners and Sources.
For example, I want to access the Filename
property of a trace listener object so I can know where the log file is located on disk.
Update: Looking for answers that use the runtime object model, not by parsing the XML configuration.
Web.config file is as follow:
For Windows application, you can open the .config file with
System.Configuration.ConfigurationManager.OpenExeConfiguration
instead of WebConfigurationManager.You can access the logging configuration programmatically using the object model (used for configuration).
To get the specific data for the trace listener you should look at TraceListenerData (and the specific subclasses).
This example shows how to read in the configuration and then get the TraceListeners:
Apparently some needed info is privately encapsulated in a
LogWriterStructureHolder
instance (its field is named structureHolder) on the Enterprise Library Logger.Writer instance (of TypeLogWriter
).So I'm effectively looking for:
Logger.Writer.structureHolder
(but that field is private).I used reflection to pull it out....
These are the significant namespaces:
This is reflection code to pull out the needed private data:
If anybody can find a non-private entry point for this same data please post it in an answer. Thanks.
Kudos to .NET Reflector for facilitating this answer.
The other answers seem very verbose, here is my solution:
Once you've called this helper method you can cast the result to whatever type the listener is such as RollingFlatFileTraceListenerData, EmailTraceListenerData, FormattedEventLogTraceListenerData, FormattedDatabaseTraceListenerData