【问题标题】:Getting current log file content in Enterprise Library Logging在 Enterprise Library Logging 中获取当前日志文件内容
【发布时间】:2014-02-14 23:41:42
【问题描述】:

我已设置企业库日志记录应用程序块来登录一个名为“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 会不断阻止我要读取的文件。有没有可能访问它?

【问题讨论】:

  • 适用于 4.1 version ,不适用于 5.0 不编译 LoggingSettings log = config.GetSection("loggingConfiguration") as LoggingSettings;

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


【解决方案1】:

检查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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2014-06-15
    相关资源
    最近更新 更多