Get defaultCategory specified in config in Enterprise Library Logging programmatically?

409 Views Asked by At

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.

2

There are 2 best solutions below

0
Andrey Ershov On BEST ANSWER
        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;
        }
0
Stefan Fröhlich On

When Enterprise Library Logger is configured in web.config or app.config you can access the value of defaultCategory attribute with follwing code (in my example "General"):

<loggingConfiguration name="" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">....</loggingConfiguration>


string defaultCategory = string.Empty;
Logger.Writer.Configure(config => defaultCategory = config.DefaultSource);