我正在使用Enterprise Library 3.1並希望以編程方式訪問Logging Block(運行時,對象模型),特別是其跟蹤偵聽器和來源。以編程方式訪問企業庫日誌記錄配置(對象模型)?
例如,我想訪問跟蹤偵聽器對象的Filename
屬性,以便我可以知道日誌文件在磁盤上的位置。
更新:尋找使用運行時對象模型的答案,而不是解析XML配置。
我正在使用Enterprise Library 3.1並希望以編程方式訪問Logging Block(運行時,對象模型),特別是其跟蹤偵聽器和來源。以編程方式訪問企業庫日誌記錄配置(對象模型)?
例如,我想訪問跟蹤偵聽器對象的Filename
屬性,以便我可以知道日誌文件在磁盤上的位置。
更新:尋找使用運行時對象模型的答案,而不是解析XML配置。
您可以使用對象模型(用於配置)以編程方式訪問日誌記錄配置。
要獲取跟蹤偵聽器的特定數據,您應該查看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
{
}
}
我比我的運行時/反射嘗試更好。謝謝。 – 2010-09-03 03:48:57
注意:適用於** 4.1 **_versión_,不適用於*** 5.0 ** **不能編譯** LoggingSettings log = config.GetSection(「loggingConfiguration」)作爲LoggingSettings;' – Kiquenet 2015-10-07 10:28:22
顯然一些必要的信息是私人封裝在一個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爲方便此答案。
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文件是如下:
對於Windows應用程序,您可以使用System.Configuration.ConfigurationManager.OpenExeConfiguration
而不是WebConfigurationManager打開.config文件。
其他的答案似乎非常詳細的,這裏是我的解決方案:
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
在2007年CodePlex上提出了一個類似的問題:http://entlib.codeplex.com/Thread/View.aspx?ThreadId = 16380 – 2010-08-15 00:57:21
使用EL配置對象模型來確定屬性是否可以接受?您是否使用EL的程序化配置而不使用XML配置? – 2010-08-16 02:37:17
@Tuzo:謝謝你的提問。我正在使用XML配置,並且對於您使用EL配置對象模型是可以接受的。爲了進一步闡明什麼是不可接受的,可以使用任何EL對象模型,例如,拉出一個普通的舊XML解析器並用它加載配置。希望這可以幫助。 – 2010-08-16 04:38:58