This project has moved. For the latest updates, please go here.

Can I read the log while RollingFileTraceListener is using the file?

Oct 23, 2011 at 7:46 AM

I am using RollingFileTraceListener in an ASP.NET MVC app to write daily log. I want to see the content of today's log, but the log file is locked by RollingFileTraceListener. In order to read it I have to stop the application pool, copy the log file, and then restart the application pool.

Is there a better way than this? I may not be able to stop the application pool everytime I need to see today's log.

Coordinator
Oct 28, 2011 at 1:39 AM

Hmmm, endy_c -- I have confirmed that the log file is indeed locked. I ran the MonitorConfig example with a RollingFileTraceListener and can't open the file while it is being used.

There is probably a way to allow the file to be readable by other processes while appending to it (I may be opening the file with the wrong mode/lock settings), so I will list it in the issue tracker and you can vote for it (although I think it's pretty important anyway :-)

In the meanwhile, the only partial workaround I can think of off the top of my head is that you can set the file to rotate on an hourly basis, e.g. initializeData="{ApplicationName}-{DateTime:yyyy-MM-dd_HH}.log". This will generate 24 log files per day, but you can open up the previous hours' files. 

The current hours' file is still locked, so this doesn't let you see issues that just happened a few minutes ago.

I also checked TraceConfigurationMonitor and unfortunately it seems to still keep files locked, so not much help.

If you know before hand you want to check the logs, then you could use TraceConfigurationMonitor and by default have {DateTime:yyyy-MM-dd} and then change to {DateTime:yyyy-MM-dd_HH_mm} when needed (which generates a new file every minute) and then change back. This would allow you to switch between by-the-minute logging and by-the-day without resetting your application.

(TraceConfigurationMonitor still keeps currently locked files).

 

Sly

 

 

 

Oct 28, 2011 at 6:50 PM

This is definitely possible.  As near as I can tell, it is a one line change in RollingTextWriter.

Coordinator
Nov 1, 2011 at 11:45 AM

Yeah, I don't know what I was thinking when I opened it with Shared = None. Will be fixed in the next release.

Nov 26, 2011 at 4:02 AM
sgryphon wrote:

Yeah, I don't know what I was thinking when I opened it with Shared = None. Will be fixed in the next release.

Do you plan to release a new version? Would love to get a nuget update for this issue.