0
我有一個使用Enterprise Library 6.0進行日誌記錄的C#.NET應用程序。 我已經爲數據庫日誌記錄聲明瞭一個跟蹤偵聽器,併爲平面文件日誌記錄聲明瞭另一個跟蹤偵聽器。並準備他們處理相同的日誌類別「常規」。在我的應用程序中,當我調用LogWriter.Write(「測試日誌」,「常規」); 一切都很好,我也看到數據庫和平面文件中的「測試日誌」。使用Microsoft企業庫進行連續日誌記錄LogWriter
但如果其中一個失敗;例如,當我調用LogWriter.Write(「測試日誌」,「常規」);假定數據庫不可訪問。我期望從LogWriter將數據寫入平面文件。
不幸的是我沒有看到任何寫在平面文件和數據庫中的日誌。它通常嗎? 是否可以配置連續日誌記錄的日誌機制? (例如,如果跟蹤偵聽器的一個失敗,繼續下一個跟蹤監聽器)
這裏是我的日誌配置
public static LoggingConfiguration BuildConfig()
{
string baseFilePath = @"C:\LogDirectory";
DatabaseProviderFactory factory = new DatabaseProviderFactory(new SystemConfigurationSource(false).GetSection);
DatabaseFactory.SetDatabaseProviderFactory(factory, false);
// Formatters
TextFormatter briefFormatter = new TextFormatter("Timestamp: {timestamp(local)}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}ActivityId: {property(ActivityId)}{newline}Severity: {severity}{newline}Title:{title}{newline}");
TextFormatter extendedFormatter = new TextFormatter("Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title: {title}{newline}Activity ID: {property(ActivityId)}{newline}Machine: {localMachine}{newline}App Domain: {localAppDomain}{newline}ProcessId: {localProcessId}{newline}Process Name: {localProcessName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}");
var databaseTraceListener = new FormattedDatabaseTraceListener(DatabaseFactory.CreateDatabase("DefaultDB"), "WriteLog", "AddCategory", extendedFormatter);
var flatFileTraceListener = new FlatFileTraceListener(baseFilePath + @"\FlatFile.log", "----------------------------------------", "----------------------------------------", briefFormatter);
LoggingConfiguration config = new LoggingConfiguration();
//Log Sources
config.AddLogSource("General",SourceLevels.All,true);
//Match Trace Listeners For Log Sources
config.LogSources["General"].AddTraceListener(databaseTraceListener);
config.LogSources["General"].AddTraceListener(flatFileTraceListener);
return config;
}
這裏是我做我的應用程序登錄
public void DoSampleLogging()
{
LogWriter defaultWriter;
LoggingConfiguration loggingConfiguration;
loggingConfiguration = LoggingHelper.BuildConfig();
defaultWriter = new LogWriter(loggingConfiguration);
defaultWriter.Write("Test Log", "General");
}
Thanks @ Tron5000 另外,我在entlib論壇中打開了相同的線程。看起來這是企業圖書館中一項活躍的工作項目,作爲一項需要開發的功能。答案在這裏https://entlib.codeplex.com/discussions/474408 – Ahmet