Programmatic Access to Enterprise Library Logging Configuration (Object Model)?

I am using Enterprise Library 3.1 and want to programmatically access the Logging block (runtime, object model), in particular, its listeners and sources.

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

Update: Search for answers that use the runtime object model rather than parsing the XML configuration.

+3
source share
4 answers

( ).

, 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 
    {
    }
}
+2

-, - LogWriterStructureHolder ( structHolder) Enterprise 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 .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 .config System.Configuration.ConfigurationManager.OpenExeConfiguration WebConfigurationManager.

0

, :

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

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

Once you have called this helper method, you can distinguish the result from any type that the listener has, such as RollingFlatFileTraceListenerData, EmailTraceListenerData, FormattedEventLogTraceListenerData, FormattedDatabaseTraceListenerData p>

0
source

Source: https://habr.com/ru/post/1759703/


All Articles