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

由网友(一生酷到底)分享简介:我使用企业库3.1 ,并希望以编程方式访问在日志块(运行时,对象模型),特别是其跟踪侦听器和源。 I'm using Enterprise Library 3.1 and want to programmatically access the Logging Block (runtime, object model)...

我使用企业库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).

要获得具体的数据来跟踪监听器,你应该看看 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 
    {
    }
}
阅读全文

相关推荐

最新文章