Getting current log file content in Enterprise Library Logging

667 Views Asked by At

I've set up the Enterprise Library Logging Application Block to log in a file called 'app.log' residing in the execution path of my application. This application is a Windows service which runs a configuration website on top of it, where I now want to show the contents of the log file.

Getting the log file was a rather easy task:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var logSection = config.GetSection("loggingConfiguration") as LoggingSettings;

var lookup = logSection.TraceListeners
                .Where(x => x is RollingFlatFileTraceListenerData).FirstOrDefault() as RollingFlatFileTraceListenerData;
if(lookup != null) {
    var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, lookup.FileName);
    return File.ReadAllText(_logFilePath);
}

However, the RollingFlatFileTraceListener I've set up constantly BLOCKS the file I want to read from. Is there any possibility to access it?

1

There are 1 best solutions below

0
On

Check this answer. That this is not the default behavior for File.ReadAllText is beyond me...

using (var logFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var logFileReader = new StreamReader(logFileStream))
{
    return logFileReader.ReadToEnd();
}

Also note that you are mixing filePath and _logFilePath.