首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以编程方式访问企业库日志配置(对象模型)?

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

Stack Overflow用户
提问于 2010-08-14 22:46:35
回答 4查看 6.6K关注 0票数 4

我正在使用企业图书馆3.1,并希望以编程方式访问测井区块 (运行时、对象模型),特别是它的跟踪侦听器和源。

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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-08-16 05:36:09

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

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

此示例演示如何在配置中读取,然后获取TraceListeners:

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2010-08-15 01:38:16

显然,一些需要的信息是在LogWriterStructureHolder实例(其字段命名为structureHolder)中私下封装在企业库Logger.Writer实例(LogWriter类型)中的。

因此,我实际上是在寻找:Logger.Writer.structureHolder (但该字段是私有的)。

我用反射把它拉出来..。

这些是重要的名称空间:

代码语言:javascript
复制
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Logging;

这是提取所需私有数据的反射代码:

代码语言:javascript
复制
// 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反射镜为这一回答提供了便利。

票数 0
EN

Stack Overflow用户

发布于 2013-08-08 02:37:27

代码语言:javascript
复制
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文件如下:

对于.config应用程序,可以使用System.Configuration.ConfigurationManager.OpenExeConfiguration而不是WebConfigurationManager打开WebConfigurationManager文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3485438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档