Search code examples
c#.netapp-configenterprise-libraryenterprise-library-5

Get defaultCategory specified in config in Enterprise Library Logging programmatically?


I am not being able to find programmatic way to find defaultCategory for Logging, that is specified in App.Config

  <loggingConfiguration name="loggingConfiguration" tracingEnabled="true"
    defaultCategory="Service1">
    <listeners>

The point is to add default Category to each log within the process, so even categories are redirected to different file, each service will still have all it's logs in his log file.


Solution

  •         private static string TryGetDefaultCategory()
            {
                string result = null;
                try
                {
                    var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
                    var loggingSection =
                            configuration.Sections.OfType<Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings>().First();
                    result = loggingSection?.DefaultCategory;
                }
                catch (Exception ex)
                {
                    // Error("[Logging] Failed to get Default Category", ex);
                }
                return result;
            }