【问题标题】:How to Download log files generated from log4Net如何下载从 log4Net 生成的日志文件
【发布时间】:2015-03-12 11:28:08
【问题描述】:

我在 ASP.NET MVC 4 应用程序中使用 log4net,我正在尝试从 log4Net 下载生成的日志文件。

我想用 FileResult 下载日志文件,例如:

[Authorize(Roles = "Admin")]
public FileResult DownloadUserInterfaceLog()
{
    // Get the path of the log file
    string path = (LogManager.GetCurrentLoggers()[0].Logger.Repository.GetAppenders()[0] as FileAppender).File;
    // Get the file
    byte[] fileBytes = System.IO.File.ReadAllBytes(path);
    string fileName = "Log.txt";
    // Return the expected file
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

我的问题是当我调用System.IO.File.ReadAllBytes(path);我得到一个异常,导致日志文件已被另一个进程使用。

我已经输入<lockingModel value="log4net.Appender.FileAppender+MinimalLock" /> (which should release the file after any modification) in my Web.Config,但没有成功。

在 Web.Config 中开始 log4Net 配置:

<log4net>
    <appender name="Appender-root" type="log4net.Appender.RollingFileAppender">
        <lockingModel value="log4net.Appender.FileAppender+MinimalLock" />
        <file value="Path..." />
        <appendToFile value="true" />

【问题讨论】:

    标签: c# asp.net-mvc logging log4net


    【解决方案1】:

    访问文件前需要关闭Appender:

    ((FileAppender)LogManager.GetCurrentLoggers()[0].Logger.Repository.GetAppenders()[0]).Close()
    

    【讨论】:

    • 似乎有效!太感谢了 !!你也知道,我怎样才能重新建立 log4net 跟踪器? (在我关闭 Appender 之后)
    【解决方案2】:

    Here是一篇关于类似问题的文章。

    File.ReadAllBytes() 函数是这样编写的,它使用类似的调用 下面来实例化一个 FileStream:

    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
    

    请注意,文件共享被指定为读取。这意味着如果你有 另一段代码同时写入同一个文件,你 即使另一段代码创建了 FileStream 通过将共享模式指定为“ReadWrite”,如下所示:

    FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)
    

    解决方案是编写自己的自定义 ReadAllBytes:

    public static byte[] ReadAllBytes(String path)
    {
        byte[] bytes;
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            int index = 0;
            long fileLength = fs.Length;
            if (fileLength > Int32.MaxValue)
                throw new IOException(“File too long”);
            int count = (int)fileLength;
            bytes = new byte[count];
            while (count > 0)
            {
                int n = fs.Read(bytes, index, count);
                if (n == 0)
                    throw new InvalidOperationException(“End of file reached before expected”);
                index += n;
                count -= n;
            }
        }
    return bytes;
    }
    

    【讨论】:

      【解决方案3】:

      这对我有用

                  var logFile = System.Web.HttpContext.Current.Server.MapPath("/Log.txt");
                  if (System.IO.File.Exists(logFile))
                  {
                      var copyFile = logFile.Replace(".txt", "");
                      System.IO.File.Copy(logFile, copyFile);
                      var bytes = System.IO.File.ReadAllBytes(copyFile);
                      System.IO.File.Delete(copyFile);
                      return bytes;
      
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-22
        • 1970-01-01
        • 2011-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多