ASP.NET 5 Core MVC 应用程序在带有 Apache 的 Debian 中运行。 下面的控制器用于读取日志。 Reding syslog 工作正常。 读取访问和错误日志导致错误
Permission denied
Access to the path '/var/log/apache2/error.log' is denied.
System.UnauthorizedAccessException: Access to the path '/var/log/apache2/error.log' is denied.
---> System.IO.IOException: Permission denied
--- End of inner exception stack trace ---
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
...
/var/log/apache2/error.log 文件,/var 和 /log 目录被标记为每个人都可读。 如何阅读这些日志?读取系统日志有效。
public async Task<IActionResult> Syslog()
{ // this works
return await Logi("/var/log/syslog");
}
public async Task<IActionResult> Errorlog()
{ // this throws permission denied error
return await Logi("/var/log/apache2/error.log");
}
static async Task<IActionResult> Logi( string fail)
{
StringBuilder sb = new("<html><head></head><body><pre>");
using (StreamReader reader = new(fail))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
sb.AppendLine(line);
}
sb.AppendLine("</pre></body></html>");
return new ContentResult()
{
Content = sb.ToString(),
ContentType = "text/html"
};
}