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

Lock file

Rekommendera Poster

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

 

 

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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.

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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

 

 

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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.

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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.

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

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

 

Dela detta inlägg


Länk till inlägg
Dela på andra webbplatser

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Skapa ett nytt konto på vårt forum. Det är lätt!

Registrera ett nytt konto

Logga in

Redan medlem? Logga in här.

Logga in nu



×
×
  • Skapa nytt...