Consecutive logging with Microsoft Enterprise Library LogWriter

1.6k views Asked by At

I have a C# .NET application using Enterprise Library 6.0 for logging. I have declared one trace listener for database logging and another trace listener for flat file logging. And prepared them for handling same log category "General".

In my application when i call LogWriter.Write("Test Log", "General"); everything is fine i see the "Test Log" in database and flat file as well.

But if one of them fails; For example when i call LogWriter.Write("Test Log", "General"); assume that the database is inaccessible. I expect from LogWriter just write the data into flat file.

Unfortunately i don't see any log written both in flat file and database. Is it usual? And is it possible to configure logging mechanism for consecutive logging? ( like if one of trace listeners fails continue with next trace listener)

Here is my logging configuration

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;
}

Here is where I do logging in my app:

public void DoSampleLogging()
{
    LogWriter defaultWriter;
    LoggingConfiguration loggingConfiguration;

    loggingConfiguration = LoggingHelper.BuildConfig();
    defaultWriter = new LogWriter(loggingConfiguration);

    defaultWriter.Write("Test Log", "General");
}
1

There are 1 answers

1
Tron5000 On BEST ANSWER

Unfortunately, this behavior appears to be by design. If one listener fails the others for that category are not processed. See the answer on this page for reference.