Email Trace Listener Subject Dynamic Values

Topics: Logging Application Block
Nov 23, 2011 at 3:48 PM

I m using EL 5.0 Logging Blocks in my App.

I have added Email trace Listener as One of the Listener for my Category Sources for sending mails on exception.

Now this code gets uploaded to Staging, QA & Production Server with the same configuration but I want to add the machine name in subject line , so that people in my team can identify wher the exception is coming from by looking at the subject line & set filters iun mail client, ignore certain errors on certain machine etc..

How can I have the dynamic values in my Subject Prefix/Suffix ?

Like can I use the {localMachine} variable from the formatter so I can get the value dynamically? I tried using it but the whole thig got printed as string.

Is there any way I can dynamic values in Subject??

Nov 23, 2011 at 11:15 PM

The short answer is no.  You can't have dynamic subject values from configuration.  The EmailTraceListener has properties subjectLineStarter and subjectLineEnder but they do not perform any dynamic substitutions (like a formatter would).

To me this seems more like a configuration management issue where the same approach and policies that are used for other configuration changes (e.g. mail server, password, connection strings etc.) should be used for managing this configuration.  Machine name is also present in the body of the message -- perhaps a filter can be setup to inspect the message body?

If this is a burning issue then another approach would be to move to a code based configuration where you can dynamically set the configuration based on the runtime environment.  E.g.:

                ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder();

                         .SendTo.Email("Email Trace Listener")
                          .WithSubjectStart("MACHINE: " + System.Net.Dns.GetHostName() + " ")
                DictionaryConfigurationSource configSource = new DictionaryConfigurationSource();
                coreExtension = new EnterpriseLibraryCoreExtension(configSource);

                IUnityContainer container = new UnityContainer();

                var configurator = new UnityContainerConfigurator(container);
                IServiceLocator locator = new UnityServiceLocator(container);
                EnterpriseLibraryContainer.Current = locator;

Randy Levy
Enterprise Library support engineer