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 8104587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:53:31+00:00 2026-06-05T23:53:31+00:00

I have a logger class that is used all across my application, both in

  • 0

I have a logger class that is used all across my application, both in requests and threads. The logger consists of a database insert for each Log.Insert(string text). The logger is a static class, and a new database context is created for each call to Insert. There might be many calls to Insert, and a lot of database contexts simultaneously is not good for optimization. (I sometimes get database timeout, because of too many sql operations across the application – so optimization is indeed needed).

Can this be optimized by creating and storing a single database context in a static member of the Logger class, that is only used for the Log.Inserts? Or will this fail?

Simplified version of the logger class;

[Table]
public class Log
{
   [Column]
   public string Text { get; set; }

   private static DataContext DatabaseContextInsert { get; set; }

   public static void Insert(string text)
   {
      if (DatabaseContextInsert == null)
      {
          DatabaseContextInsert = DataContextHelper.GetDataContext();            
      }

      var log = new Log { Text = text };          

      lock (DatabaseContextInsert)
      {
        DatabaseContextInsert.GetTable<Log>().InsertOnSubmit(log);
        DatabaseContextInsert.SubmitChanges();
      }
   }
}
  • 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-06-05T23:53:33+00:00Added an answer on June 5, 2026 at 11:53 pm

    Using a static logger would be a very bad idea. In addition to synchronization issues, you would have problems with all the items being held in the data-context forever (a data-context likes to keep hold of objects it has seen).

    You mention that you think that lots of data-contexts isn’t good for optimisation, but the connection is presumably already pooling (it does that by default), so the data-context isn’t actually very “heavy”.

    If the log entries do not need to be inserted synchronously, I would be tempted to do something like throwing new log items into a queue (synchronizing access), and having a worker thread that empties the queue every 10 seconds, essentially doing something like:

    // adding an item
    lock(queue) { queue.Enqueue(text); }
    
    
    // worker code, every 10 seconds
    List<string> items = new List<string>();
    lock(queue) {
        while(queue.Count != 0) items.Add(items.Dequeue);
    }
    using(var ctx = CreateContext()) {
        foreach(var text in items) {
            ctx.Logs.InsertOnSubmit(new Log { Text = text });
        }
        ctx.SubmitChanges();
    }
    

    Then only one thread is writing to the database, and you have far fewer transactions.

    If this isn’t an option, and you need to do it synchronously, then frankly I’d drop down a few levels to something like “dapper”, i.e.

    using(var conn = CreateOpenConnection()) {
        conn.Execute("insert [Log]([Text]) values(@text)", new {text});
    }
    

    which removes the need for a data-context at all, is much simpler, and removes the transaction from consideration.

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

Sidebar

Related Questions

I have a logger in a c++ application that uses defines as follows: #define
I have been coding an application which renders tiles and GUI and all that.
I have a C++ unmanaged class NativeDog that needs to be used from C#,
If I have a log4j logger in a class that implements Runnable: MyTask implements
I have a base class named 'baseScreen' as follows: digient.casino.ui.baseScreen = function() { goog.debug.Logger.getLogger('demo').info('baseScreen');
I've a problem with celery's logger. I have a function that renders frames. I
One trick I have found very handy in rails application programming is that class_eval
A quick description of the environment: I have a class that represents a chatroom
I'm writing an application that draws certain shapes. Before each shape is drawn, some
I have an entity class User that contains information such as username, first name,

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.