views:

86

answers:

2

I have an ASP.NET MVC website that works in tandem with a Windows Service that processes file uploads. For easy maintenance of the site, I'd like the log file for the Windows Service to be accessible (to me, only) via the website, so that I can hit http://myserver/logs/myservice to view the contents of the log file. How can I do that?

At a guess, I could either have the service write its log file in a "Logs" folder at the top level of the site, or I could leave it where it is and set up a virtual directory to point to it. Which of these is better - or is there another, better way?

Wherever the file is stored, I can see that there's going to be another problem. I tried out the first option (Logs folder in my website), but when I try to access the file via HTTP I get an error:

The process cannot access the file 'foo' because it is being used by another process.

Now, I know from experience that my service keeps the file locked for writing while it's running, but that I can still open the file in Notepad to view the current contents. (I'm surprised that IIS insists on write access, if that's what's happening).

How can I get around that? Do I really have to write a handler to read the file and serve it to the browser myself? Or can I fix this with configuration or somesuch?

PS. I'm using IIS7 if that helps.

+1  A: 

I think the virtual directory is an "okay" solution, if you add the directory (application) with READ ONLY rights + perhaps "BROWSE directory" too (so you can see the folder contents rendered by the IIS).

(But once you do that, you should consider that you also anonymous access to that folder - unless you enable authentication, so watch out for "secret" contents of the logfiles that you might expose? just a thought.)

Another approach, I prefer myself, is to make a MVC/ASP.NET page that does the lookup in the folder by normal code, so that you 100% can filter whatever data is shown in the HTML. You can open the files as TextStream's and in Read Only mode.

If it's a problem to gain access to the logfolder, I would use the virtual directory with READ ONLY access and then program something that renders the logfiles as HTML on my screen and with my detail levels. Perhaps even add some sort of "login" first. But it all depends on your security levels and contents of logfiles.

is this meaningfull to you? if not, please explain more, as I've been through this thought a few times already for similar situations.

BerggreenDK
@BerggreenDK: thanks. I guess what I was really hoping for was that someone would tell me "just configure IIS like this and you won't get that error" :-) If I have to write code to read the file and stream it to the browser just to get round the read-only problem, then the actual location of the file is not really an issue, since my code will be running under the NETWORK SERVICE account. Note that there's only one log file, so I don't need folder browsing or anything like that.
Gary McGill
okay, so you need someone to write that little code that opens a textstream then?
BerggreenDK
@BerggreenDK: no, that's fine, thanks. It's not that I can't write the code, it's just that I hate to write code that merely duplicates what's available with a config change.
Gary McGill
+2  A: 

Unfortunately I'm afraid you'll have to write a handler that will open the file, and return it to the client. I've written an IIS Manager extension that displays server log files, and what I've noticed that even the simple

System.IO.File.OpenRead("")

can still run in the same problem, and return the same error.. It was kind of 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 :)

Artiom Chilaru