编程方式访问企业库日志记录配置(对象模型)? [英] Programmatically access Enterprise Library Logging configuration (object model)?

查看:118
本文介绍了编程方式访问企业库日志记录配置(对象模型)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用企业库3.1 ,并希望以编程方式访问在日志块(运行时,对象模型),特别是其跟踪侦听器和源。

I'm using Enterprise Library 3.1 and want to programmatically access the Logging Block (runtime, object model) specifically its Trace Listeners and Sources.

例如,我要访问的文件名跟踪侦听器对象,这样我就可以知道该日志文件位于硬盘的性能。

For example, I want to access the Filename property of a trace listener object so I can know where the log file is located on disk.

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

Update: Looking for answers that use the runtime object model, not by parsing the XML configuration.

推荐答案

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

You can access the logging configuration programmatically using the object model (used for configuration).

要获得具体的数据来跟踪监听器,你应该看看<一href="http://msdn.microsoft.com/en-us/library/microsoft.practices.enterpriselibrary.logging.configuration.tracelistenerdata%28PandP.31%29.aspx"相对=nofollow> TraceListenerData (和具体的子类)。

To get the specific data for the trace listener you should look at TraceListenerData (and the specific subclasses).

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

This example shows how to read in the configuration and then get the 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 
    {
    }
}

这篇关于编程方式访问企业库日志记录配置(对象模型)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆