Just nu i M3-nätverket
Gå till innehåll

Lock file


Cecilia Hagglund

Rekommendera Poster

Cecilia Hagglund

Hi,

 

I am currently working on a web application written in ASP.net and C#. Am currently working with the error handler class. When an error occurs the error will be logged in the database but if the database is down the error will be written to a log file. Since it is a web application I need to lock the file so different users cannot write to the file at the same time. Using the "lock" keyword is not very good since it is a big application and many will try to write at the same time (timeouts will occur). Create one log file for each user is not the best solution either.

 

Does anyone know what I can do to solve this problem?

 

Thanks!

 

 

Kind Regards

 

Cecilia

 

 

 

Länk till kommentar
Dela på andra webbplatser

DanielForPresident

How about one file for each error? A new error creates a new error report file.

 

Länk till kommentar
Dela på andra webbplatser

jerker olofsson

As the write-calls to the file shouln't occur too often, a loop with a sleep might be sufficient.

 

Just try to open the file as "unshared" for writing and if it doesn't work, sleep for some millis and try again.

 

Länk till kommentar
Dela på andra webbplatser

Cecilia Hagglund

Thanks for your reply!

 

I have one more question....

 

It won't happen often that the database is down and errors have to be written to the file. But when it happens lots of errors will occur since the database is not working and many users will try to write to the file at the same time. Will your solution still work if this happens? Is there a risk that users will be caught in an infinite loop?

 

 

Kind Regards

 

Cecilia

 

 

 

Länk till kommentar
Dela på andra webbplatser

DanielForPresident

If this is a critical application I would recommend some warning system if the database goes down. Something like sending you (or the administrator) an e-mail, a SMS or something like that.

 

Länk till kommentar
Dela på andra webbplatser

jerker olofsson

It would still work, but the performance would decrease. And the implementation is not very nice.

 

I recomend using System.Diagnostics.EventLog to write the log to the windows error log. Check out: http://msdn2.microsoft.com/en-us/system.diagnostics.eventlog.aspx

 

The log file can be read using:

Control Panel / Administrative Tools / Event Viewer

 

or even be displayed on the web application:

http://www.aspheute.com/english/20000811.asp

 

Länk till kommentar
Dela på andra webbplatser

jerker olofsson

Oh, and another solution that should work, if there is a requirement to write to a file, is to create a webservice with a singleton object that handles writing the data to the file.

 

The singleton object can own the filestream and the class should be synchronized to handle several requests.

 

Länk till kommentar
Dela på andra webbplatser

  • 1 month later...

Another good solution is to create a worker thread that handles all file loging. This thread reads from an threadsafe FIFO queue accessed trough a singleton class.

 

/John

 

Länk till kommentar
Dela på andra webbplatser

Arkiverat

Det här ämnet är nu arkiverat och är stängt för ytterligare svar.

×
×
  • Skapa nytt...