在Enterprise Library Logging中获取当前日志文件内容

时间:2014-02-14 23:41:42

标签: c# logging enterprise-library logging-application-block

我已经设置了企业库日志记录应用程序块,以便在我的应用程序的执行路径中登录名为“app.log”的文件。此应用程序是一个Windows服务,它在其上运行配置网站,我现在想要显示日志文件的内容。

获取日志文件是一项相当容易的任务:

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

但是,RollingFlatFileTraceListener我不断设置BLOCKS我要读取的文件。有没有可能访问它?

1 个答案:

答案 0 :(得分:0)

检查this answer。这不是File.ReadAllText的默认行为,超出了我......

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

另请注意,您正在混合filePath_logFilePath

相关问题