How can I make the server log file available through my ASP.NET MVC website?
I have an ASP.NET MVC website that works in tandem with a Windows Service that handles file uploads. For the sake of maintainability of the site, I would like the log file for Windows Service to be accessible (just to me) through the website so that I can click http: // myserver / logs / myservice to view the contents of the log file. How can i do this?
Guessing right, I could either get the service to write its log file to the Logs folder at the top level of the site, or I could leave it where it is and configure the virtual directory to point to it. Which one is better - or is there another, better way?
Where the file is stored, I see there will be a different problem. I tried the first option (Logs folder on my site), but when I try to access the file over HTTP, I get an error:
The process cannot access file 'foo' because it is being used by another process.
Now, from experience, I know that my service keeps the file locked for writing while it is running, but I can still open the file in Notepad to see the current content. (I'm surprised IIS insists on write access if this happens).
How can I get around this? Should I actually write a handler to read the file and itself in the browser? Or can I fix this with config or something similar?
PS. I am using IIS7 if that helps.
a source to share
Unfortunately, I'm afraid you will have to write a handler that will open the file and return it to the client. I wrote an IIS manager extension that displays the server log files and that I noticed that even a simple
System.IO.File.OpenRead("")
can still work on the same problem and return the same error. It was confusing. In the end I used
System.IO.File.Open("", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
and I could easily open the file while the server was writing logs to it :)
a source to share
I think the virtual directory is a "good" solution if you add a directory (application) with READ ONLY + permissions, possibly also "BROWSE-directory" (so you can see the contents of the folder displayed by IIS).
(But once you've done that, you should consider that you also have anonymous access to this folder - if you haven't enabled authentication, so keep an eye on the "secret" contents of the log files that you can open, just a thought.)
Another approach that I prefer is to create an MVC / ASP.NET page that searches a folder using regular code, so that you can 100% filter any data rendered in HTML. You can open files as TextStream and read-only.
If you need to access the log folder, I would use a READ-only virtual directory and then program something that makes the log files as HTML on my screen and with my verbose levels. Perhaps add some sort of "login" first. But it all depends on your security levels and the content of the log files.
does this make sense to you? if not, please explain more as I have done this thought several times for similar situations.
a source to share