2010-08-14 91 views
3

我正在使用Enterprise Library 3.1并希望以编程方式访问Logging Block(运行时,对象模型),特别是其跟踪侦听器和来源。以编程方式访问企业库日志记录配置(对象模型)?

例如,我想访问跟踪侦听器对象的Filename属性,以便我可以知道日志文件在磁盘上的位置。

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

+0

在2007年CodePlex上提出了一个类似的问题:http://entlib.codeplex.com/Thread/View.aspx?ThreadId = 16380 – 2010-08-15 00:57:21

+0

使用EL配置对象模型来确定属性是否可以接受?您是否使用EL的程序化配置而不使用XML配置? – 2010-08-16 02:37:17

+0

@Tuzo:谢谢你的提问。我正在使用XML配置,并且对于您使用EL配置对象模型是可以接受的。为了进一步阐明什么是不可接受的,可以使用任何EL对象模型,例如,拉出一个普通的旧XML解析器并用它加载配置。希望这可以帮助。 – 2010-08-16 04:38:58

回答

2

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

要获取跟踪侦听器的特定数据,您应该查看TraceListenerData(以及特定的子类)。

这个例子显示了如何在配置读取然后拿到TraceListeners:

// 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 
    { 
    } 
} 
+0

我比我的运行时/反射尝试更好。谢谢。 – 2010-09-03 03:48:57

+0

注意:适用于** 4.1 **_versión_,不适用于*** 5.0 ** **不能编译** LoggingSettings log = config.GetSection(“loggingConfiguration”)作为LoggingSettings;' – Kiquenet 2015-10-07 10:28:22

0

显然一些必要的信息是私人封装在一个LogWriterStructureHolder实例(其字段命名为structureHolder)上(LogWriter型)企业库Logger.Writer实例。
所以我正在寻找: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为方便此答案。

0
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应用程序,您可以使用System.Configuration.ConfigurationManager.OpenExeConfiguration而不是WebConfigurationManager打开.config文件。

0

其他的答案似乎非常详细的,这里是我的解决方案:

public static TraceListenerData GetTraceListener(string name) 
    { 
     var log = ConfigurationManager.GetSection("loggingConfiguration") as LoggingSettings; 

     return log.TraceListeners.Single(tl => tl.Name == name); 
    } 

一旦你叫,你可以把结果到任何类型听者如RollingFlatFileTraceListenerData,EmailTraceListenerData,FormattedEventLogTraceListenerData这个辅助方法, FormattedDatabaseTraceListenerData

相关问题