How to add listeners through code?

Topics: Logging Application Block
Aug 10, 2009 at 11:18 AM

can any one tell me How to add listeners  in loggingConfiguration  tag through code?


Aug 10, 2009 at 11:42 AM
Edited Aug 10, 2009 at 12:01 PM


You can do it programmatically by modifying your config at runtime. Here is a sample code snippet that will add a flatFile trace listener. Assuming that you have configured logging and your just adding some trace listeners.

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourConfig.config";
Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
//Create the formatter
string formatterTemplate =  @"Timestamp: {timestamp}Message: {message}Category: {category}Priority: {priority}EventId: {eventid}Severity: {severity}Title:{title}Machine: {machine}Application Domain: {appDomain}Process Id: {processId}Process Name: {processName}Win32 Thread Id: {win32ThreadId}Thread Name: {threadName}Extended Properties: {dictionary({key} - {value})}";

TextFormatterData formatterData = new TextFormatterData("myFormatter", typeof(TextFormatter),
//Add the formatter
//Create the trace listener
FlatFileTraceListenerData flatFileData = new FlatFileTraceListenerData("MyFlatFile", @"C:\MyFlatFilePath.txt",
//Add the trace listener
//Save the changes

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.


Aug 10, 2009 at 12:57 PM

Hi Valiant,

Am getting "Object reference not set to an instance of an object." error at loggingSettings.Formatters.Add(formatterData); line of code.

Am using this logging in a web service.While building that web service am not getting any Errors.

While using this Web service from web application am getting this error.

Plz tell me how to resolve this...


Aug 11, 2009 at 12:55 AM

Which one is the null object, the loggingSettings or its Formatters property?  Do you have the Logging Application Block defined in your web service config file?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.


Aug 11, 2009 at 5:06 AM

I have added Logging Application Block in web.config.

loggingSettings object is null.

Aug 11, 2009 at 5:11 AM

Just to clarify, that web.config is the config file of the web service project, right?


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Aug 11, 2009 at 5:20 AM


Aug 11, 2009 at 5:39 AM

I can't think of a reason why would it result to null if you have your config and the logging section defined.  There just must be a simple mistake we are missing.  Make sure you specified the correct file path of the config in the ExeConfigFileName. 


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Aug 11, 2009 at 6:01 AM

It got solved...There is some extra space got added in the path.

Now the formatter and trace listerners are added..How to add a Category of our own(not Default  caregoty)..

Aug 11, 2009 at 6:11 AM

Just add a TraceSourceData object:

TraceSourceData category = new TraceSourceData();

category.Name = "MyCategory";



Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Aug 11, 2009 at 6:42 AM
Edited Aug 11, 2009 at 7:51 AM

Thanks Sarah,

category is also getting added.

 But Logging is not happening...

After saving the config file ,I wrote code for logging as follows..

        //Save the changes
            LogEntry le = new LogEntry();
            ////le = Category(le);
            le.Message = "Testing Logging Message";
            le.EventId = 1112;
            le.Title = "Title";
            le.Priority = 1;

Will it take sometime to make changes in config?


Aug 11, 2009 at 6:59 AM
Edited Apr 29, 2010 at 3:46 AM

Yes, there is a slight delay for the config changes to take effect, take a look at this thread -



Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.