以编程方式获取Enterprise Library Logging中config中指定的defaultCategory?

时间:2016-04-07 12:36:22

标签: c# .net app-config enterprise-library enterprise-library-5

我无法找到以编程方式查找Logging的defaultCategory,这是在App.Config中指定的

// var txt = 'myImage123.gif'; // works ok
// var txt = 'folder1/folder2/123/myImage123.gif'; // replaces numbers out of scope
var txt = 'folder1/folder2/123/my123Image123.gif'; // replaces numbers out of scope
txt = txt.replace(/\d+/,'999');

$('h3').text(txt);

关键是要为流程中的每个日志添加默认类别,因此即使将类别重定向到不同的文件,每个服务仍将在其日志文件中包含所有日志。

2 个答案:

答案 0 :(得分:0)

        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;
        }

答案 1 :(得分:0)

在web.config或app.config中配置Enterprise Library Logger时,您可以使用以下代码访问defaultCategory属性的值(在我的示例中为“常规”):

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


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