代码之家  ›  专栏  ›  技术社区  ›  John K

以编程方式访问企业库日志记录配置(对象模型)?

  •  3
  • John K  · 技术社区  · 14 年前

    我在用 Enterprise Library 3.1 并希望以编程方式访问 Logging Block (运行时,对象模型)特别是它的跟踪侦听器和源。

    例如,我想访问 Filename

    更新: 寻找使用运行时对象模型的答案,而不是通过解析XML配置。

    4 回复  |  直到 14 年前
        1
  •  2
  •   Randy Levy    14 年前

    您可以使用对象模型(用于配置)以编程方式访问日志记录配置。

    TraceListenerData (以及特定的子类)。

    此示例显示如何读入配置,然后获取TraceListener:

    // Open config file
    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    fileMap.ExeConfigFilename = @"MyApp.exe.config";
    
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    
    // Get EL log settings
    LoggingSettings log = config.GetSection("loggingConfiguration") as LoggingSettings;
    
    // Get TraceListener info
    foreach(TraceListenerData listener in log.TraceListeners)
    {
        // Check for listener types you care about
        if (listener is RollingFlatFileTraceListenerData)
        {
            RollingFlatFileTraceListenerData data = listener as RollingFlatFileTraceListenerData;
            Console.WriteLine(string.Format("Found RollingFlatFileLIstener with Name={0}, FileName={1}, Header={2}, Footer={3}, RollSizeKB={4}, TimeStampPattern={5},RollFileExistsBehavior={6}, RollInterval={7}, TraceOutputOptions={8}, Formatter={9}, Filter={10}",
                data.Name, data.FileName, data.Header, data.Footer, data.RollSizeKB, 
                data.TimeStampPattern, data.RollFileExistsBehavior, data.RollInterval,
                data.TraceOutputOptions, data.Formatter, data.Filter);
        }
        else // other trace listener types e.g. FlatFileTraceListenerData 
        {
        }
    }
    
        2
  •  0
  •   John K    14 年前

    显然,一些需要的信息被私下封装在一个 LogWriterStructureHolder 实例(其字段名为 结构持有者 )论企业图书馆 Logger.Writer 实例(类型) LogWriter ).
    所以我在寻找: Logger.Writer.structureHolder

    我用倒影把它拔出来。。。。

    using System.Reflection;
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    

    // Get the private field.
    FieldInfo fiLogStructHolder 
        = typeof(LogWriter).GetField("structureHolder", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
    
    // Obtain field value to get the private data.
    LogWriterStructureHolder structureHolder 
        = (LogWriterStructureHolder)fiLogStructHolder.GetValue(Logger.Writer);
    
    // Access the value's .TraceSources property of Type Dictionary<string, LogSource>.
    // The string is the name of the category from configuration. 
    int numSources = structureHolder.TraceSources.Count;
    
    // Furthermore, access the listeners of any logging source by specifying:
    int numListeners = structureHolder.TraceSources[0].Listeners.Count
                                                 // ^-- Note: Picked first source for example.
    

    如果有人能找到相同数据的非私人入口点,请在回复中发布。谢谢。

    .NET Reflector 为了促成这个答案。

        3
  •  0
  •   Chris Voon    11 年前
    public static EmailTraceListenerData GetEmailLogConfiguration()
    {
        var rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("/");
        var section = rootWebConfig1.GetSection("loggingConfiguration");
        var loggingSection = section as Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings;
    
        if (loggingSection != null) {
            // Reference to Microsoft.Practices.EnterpriseLibrary.Logging.dll and
            // Microsoft.Practices.EnterpriseLibrary.Common.dll required for the code below
            foreach (Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData listener in loggingSection.TraceListeners) {
                var emailTraceListenerData = listener as EmailTraceListenerData;
                if (emailTraceListenerData != null) {
                    // Can obtain FromAddress, ToAddress, SmtpServer and SmtpPort 
                    // as property of  emailTraceListenerData;
                    return emailTraceListenerData;
                }
            }
        }
        return null;
    }
    

    Web.config 文件如下:

    web.config file

    对于Windows应用程序,可以使用打开.config文件 System.Configuration.ConfigurationManager.OpenExeConfiguration

        4
  •  0
  •   Daniel    8 年前

    其他的答案似乎很冗长,下面是我的解决方案:

        public static TraceListenerData GetTraceListener(string name)
        {
            var log = ConfigurationManager.GetSection("loggingConfiguration") as LoggingSettings;
    
            return log.TraceListeners.Single(tl => tl.Name == name);
        }