Rakesh Sawant
Rakesh Sawant

Reputation: 281

creating error log file in asp.net

To Handle the error in my web application I am Creating a log file which will be created in Application_Error() event of global.asax and Those log file created based on date (1 per day)

I am Considering scenario where error occured and at same instance of time file getting write from multiple users at same time

so it may throw exception that file already being used by another user it may be like that i am not sure

can anybody help to deal with such scenario

many thanks

Upvotes: 2

Views: 1309

Answers (2)

Simon Martin
Simon Martin

Reputation: 4231

You could just use something like ELMAH which will give you pretty decent error logging. It is pretty flexible in terms of how and what it logs for you. Also you can add it with NuGet with minimal effort.

Or take a look at NLog

Upvotes: 1

Aristos
Aristos

Reputation: 66641

Use mutex to lock your writing procedure. I suggest mutex, and not lock() because can catch all pools/threads that may throw an error.

On MSDN there are the details about mutex and examples:

http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx

a simple example

public void LogThis(string LogDetails) 
{
    var mut = new Mutex(true, "LogMutexName");

    try
    {   
        // Wait until it is safe to enter.
        mut.WaitOne();

        // here you open write close your file
    }
    finally
    {
        // Release the Mutex.
        mut.ReleaseMutex();
    }   
}

Upvotes: 1

Related Questions