Asked by:
How to read Apache logs

Question
-
User-1262787652 posted
ASP.NET 5 Core MVC application isa running in Debian with Apache.
Controllers below are used to read logs.
Reding syslog works OK.
Reading access and error logs causes errorPermission 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 file, /var and /log directories are marked as readable for everyone. How to read those logs ? Reading syslog works. 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" }; }
Sunday, April 18, 2021 2:29 PM
All replies
-
User-474980206 posted
Logrotate resets the file permissions during a rotation. Did you change its config file. See docs for syslogd on you version of Linux to set the create directive.
Sunday, April 18, 2021 3:25 PM -
User-1262787652 posted
I verified that both files have read by everyone bit:
<div>
root@c202-76:/var/log/apache2# ls -l error.log -rw-r--r-x 1 root adm 9884 apr 18 17:54 error.log
syslog is readable but error.log is not.
syslog r attribute is preserved by log rotate: syslog can read all the time.
For unknown reason apache logs are not readable. I restarted kestrel and Apache but problem persists. I set read by everyone attribute for log files, havent changed log rotation settings.
Sunday, April 18, 2021 5:06 PM -
User-474980206 posted
Probably missing all execute on the director(s), so the file can not be accessed.
Sunday, April 18, 2021 6:49 PM