Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:04:50+00:00 2026-05-31T04:04:50+00:00

I have a asp.net application in a web farm (3 servers). Alongside that application

  • 0

I have a asp.net application in a web farm (3 servers). Alongside that application I have a module that logs every request made of the website to a database. Currently the inserts are sychronous. I would like to change it so the inserts are sent to a queue which will insert them as it is able.

Would it be better to

  1. Attempt to insert each request on a background thread (would too
    many background threads get used up if the database hiccups?)

  2. Start an in-process queue on a single background thread that just
    reads from queue and performs insert.

  3. Create an out-of-process queue on the database server that each server sends page request logs to. Would the built in MSMQ be the thing to use for something like this? – or would that be overkill?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:04:51+00:00Added an answer on May 31, 2026 at 4:04 am

    Option 2 sounds the best. Option 1 would definitely create way too many background threads, and option 3 sounds more complex than it needs to be.

    You might try something like this.

    Given this class:

    class LogEntry
    {
        public string IpAddress { get; set; }
        public string UserAgent { get; set; }
        public DateTime TimeStamp { get; set; }
        public string Url { get; set; }
        //whatever else you need
    }
    

    Use this class to perform the logging:

    class SiteLogger
    {
        private static object loggerLock = new object();
        private static List<LogEntry> _pendingEntries = new List<LogEntry>();
        private static Thread savingThread;
    
        public static void AddEntry(LogEntry entry)
        {
            // lock when accessing the list to avoid threading issues
            lock (loggerLock)
            {
                _pendingEntries.Add(entry);
            }
    
            if (savingThread == null)
            {
                // this should only happen with the first entry
                savingThread = new Thread(SaveEntries);
                savingThread.Start();
            }
        }
    
        private static void SaveEntries()
        {
            while (true)
            {
                while (_pendingEntries.Count > 0)
                {
                    // lock around each individual save, not the whole loop
                    // so we don't force one web request to wait for
                    // all pending entries to be saved.
                    lock (loggerLock)
                    {
                        // save an entry to the database, however the app does that
                        MyDatabase.SaveLogEntry(_pendingEntries[0]);
                        _pendingEntries.RemoveAt(0);
                    }
                }
    
                Thread.Sleep(TimeSpan.FromSeconds(2));
                // 2 seconds is a bit of an arbitrary value.  Depending on traffic levels,
                // it might need to go up or down.
            }
        }
    }
    

    I ran this with a simple command line test app without any database involvement (simulated a database call by sleeping for 10 ms) and it seemed to work great, but it should obviously be tested more before going into a production environment. Also, problems will of course arise if the requests come faster than you can save them to the database (which is unlikely but should be considered).

    Update, February 2018: Looking at this now, I realize that you could end up with two savingThread instances if you get unlucky with thread timing (which you should assume you will). And new Thread() is kind of an old way to do this kind of thing in C# now. I’ll leave modern, more thread-safe implementations of this as an exercise to the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an ASP.NET web application hosted by a web farm of many instances
I have an ASP.NET web application that is using forms authentication. Everything is configured
I have an ASP.NET web application that, for whatever reason, when it is deployed
We have an ASP.NET web application that we offer as a Service (it's hosted
I have a asp.net web application that I was hosting somewhere. I backed up
I have an asp.net web form application running in a web farm? All the
I have an ASP.NET web application that presents data retrieved from a web service.
I have a ASP.NET web application that uses Themes. Inside the theme directory are
I have an ASP.NET Web Application that constantly monitors for new RSS Feed from
I have an asp.net web application (c#) in visual studio 2010 that is heavily

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.