Add new category sources dynamically

Topics: Logging Application Block
Sep 1, 2009 at 2:25 PM

Is there a way to dynamically add new category sources?

Something like :

mylogger.write("Message to log", "CategoryZ")

And then in the method if "CategoryZ" doesn't exist in the Category Sources, create it.


Is it possible?




Sep 2, 2009 at 7:10 AM
Edited Sep 2, 2009 at 7:10 AM

You may use the code snippet below:

            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = "pathtotheconfigfile"; 
            Configuration entLibConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

	   //adds a new category
            loggingSettings.TraceSources.Add(new TraceSourceData("MyNewCategory", System.Diagnostics.SourceLevels.All,
	   //create a new trace listener	
            FlatFileTraceListenerData flatFileData = new FlatFileTraceListenerData("Flat File", @"MyFlatFile.log", string.Empty);
	   //add the trace listener
	   //associate the trace listener to the new added category
            loggingSettings.TraceSources.Get("MyNewCategory").TraceListeners.Add(new TraceListenerReferenceData("Flat File"));



Valiant Dudan
Global Technology and Solutions
Avanade, Inc.