我有一个使用Enterprise Library 6.0进行日志记录的C#.NET应用程序。 我已经为数据库日志记录了一个跟踪侦听器,为平面文件日志记录声明了另一个跟踪侦听器并准备他们处理相同的日志类别“一般”。
在我的应用程序中,当我调用LogWriter.Write(“测试日志”,“常规”); 一切都很好我也看到了数据库和平面文件中的“测试日志”。
但如果其中一个失败了;例如,当我调用LogWriter.Write(“Test Log”,“General”)时;假设数据库不可访问。我希望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");
}