1

I am creating a public facing web site and one of the requirement is to write all the errors/warning in a log file.

I have try catch block everywhere and also I can catch the errors in the application error event.

Being a public facing web site, I am afraid that log file may grow exponentially (even if I create new files every day) and it may decrease the site performance. Is there any way to write the error files asynchronously, so it will not have any impact on the users?

Any sample or guidance is highly appricated.

My site is in ASP.NET Web forms and c#.

Thanks

Tippu
  • 1,191
  • 4
  • 16
  • 36
  • 2
    See various approaches here: http://stackoverflow.com/questions/1181561/how-to-effectively-log-asynchronously – rro Nov 20 '12 at 04:13

1 Answers1

2

Recommendation: Tippu I highly recommend you use ELMAH instead. It is as easy as possible to set up and will make your life so much easier (You will never write your own logging program again). It even catches unhandled exceptions!

Check out the following website on what it is and how easy it is to set up

ELMAH

Security: The only thing you MUST be aware of is that you must secure ELMAH otherwise your session can be hijacked. However, it is very easy to secure it. Have a look at the following website on the security issue and how to secure it.

ASP.NET Session Hijacking


5-10 Minute Setup Steps

  1. Go to Nuget, search for "ELMAH SQL" and install "ELMAH on MS SQL Server".

  2. In your "App_Readme" folder there will be a "Elmah.SQLServer.sql" file which installs a table and 3 stored procedures. Run it in the database you want this connected too. (You will have to set permissions for the Stored procedure in a live server so that it can read and write to it)

  3. Go to your web config (You will notice that ELMAH has automatically updated it for you). Find , if you don't want to have remote access leave it as is and I believe it will be secured.

If you want to allow remote access so that you can view the Error Log website anywhere you need to set it to true. You then MUST secure it. The below will secure it for Admin and deny all other users. Check out this website for more information Secure ELMAH

<location path="elmah.axd">  
    <system.web>
       <httpHandlers>
            <add verb="POST,GET,HEAD" 
                path="elmah.axd" 
                type="Elmah.ErrorLogPageFactory, Elmah" />
       </httpHandlers>
     <authorization>
        <allow roles="admin" />  
        <deny users="*" />
     </authorization>  
   </system.web>
   <system.webServer>
     <handlers>
         <add name="ELMAH" 
             verb="POST,GET,HEAD"
             path="elmah.axd" 
             type="Elmah.ErrorLogPageFactory, Elmah"
             preCondition="integratedMode" />
     </handlers>
   </system.webServer>

  1. Go to your connection strings and point the "elmah-sqlserver" connection string to your specific Database.

  2. Go to your try catches and in the catch section add "ErrorSignal.FromCurrentContext().Raise(ex);"

  3. Your application will now catch Handled and Unhandled Exceptions and write the errors to a SQL Server Table. To view the errors open up a browser and go to "yourwebsite/elmah.axd"

  4. You can also set it up so the errors are sent to you. Check out the following website on how to do that: Setup ELMAH Email

Ignore the above number ordering

MVCKarl
  • 1,283
  • 9
  • 7