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

  • Home
  • SEARCH
  • 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 8614555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:08:53+00:00 2026-06-12T05:08:53+00:00

I have a LoggerBase class, it looks like: public class BatchLoggerBase : IDisposable {

  • 0

I have a LoggerBase class, it looks like:

    public class BatchLoggerBase : IDisposable
    {
        protected string LogFilePath { private get; set; }
        protected object _synRoot;

        BatchLoggerBase(string logFilePath)
        {
            LogFilePath = logFilePath;
        }

        protected virtual void WriteToLog(string message)
        {
            Task.Factory.StartNew(() =>
            {
                lock (_synRoot)
                {
                    System.IO.File.AppendAllText(LogFilePath, message);
                }
            });
        }
        //Other code... 
    }

I have another class inherit from this base class, like:

public sealed class TransactionBatchLogger : BatchLoggerBase
{
    public TransactionBatchLogger()
    {
         _synRoot = new object();
         string directory = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["Batch.TransactionLog.Path"];
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            LogFilePath = string.Format("{0}{1}_{2}.txt", directory, "TransactionLog", DateTime.Now.ToString("yyyy-MM-dd"));
    }
public void LogLoyaltyPointProcess(IEnumerable<CustomerTierOverrideItem> listOfCustomerTierItem)
        {
            Task.Factory.StartNew(() =>
            {
                //Construct message...
                WriteToLog(message);
            });
        }
}

public sealed class LoyaltyPointBatchLogger : BatchLoggerBase
{
    public LoyaltyPointBatchLogger()
        {
            _synRoot = new object();

            string directory = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["Batch.LoyaltyPointLog.Path"];
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            LogFilePath = string.Format("{0}{1}_{2}.txt", directory, "LoyaltyPointLog", DateTime.Now.ToString("yyyy-MM-dd"));
        }
public void LogLoyaltyPointProcess(IEnumerable<CustomerTierOverrideItem> listOfCustomerTierItem)
        {
            Task.Factory.StartNew(() =>
            {
                //Construct message...
                WriteToLog(message);
            });
        }
}

The LoyaltyPointBatchLogger and TransactionBatchLogger write log content to different log files(one is for transactionLog, another one is for LoayltyPointLog), but they all call same virtual method from base class.

The batch program process data batch by batch(like, 45000 total data and 10000 each batch) these two logger may called by different batches successively, so I do not want the log files are accessed by different batches logger thread.

The Question is:
Should I instantiate the _synRoot in derived class LoyaltyPointBatchLogger and TransactionBatchLogger, or in base class ?

_synRoot instantiated in LoyaltyPointBatchLogger and TransactionBatchLogger are different references, so the LoyaltyPointBatchLogger and TransactionBatchLogger will not wait each other when they go into lock statement, right?

  • 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-12T05:08:54+00:00Added an answer on June 12, 2026 at 5:08 am

    I haven’t had any experience using a “syncroot” locking pattern, but if I understand your question correctly, if I would attack this by having each subclass type declare its own unique SyncRoot statically by type. This way all instances of that type share the same SyncRoot. Additionally, I would require that the base logger be provided a SyncRoot object via the constructor rather than hope that the subclass assigns it (and assigns it in time). Additionally, I would make it immutable so subclasses can’t do evil stuff.

    BatchLoggerBase

    public abstract class BatchLoggerBase
    {
        protected readonly object SyncRoot;
    
        protected BatchLoggerBase(object syncRoot)
        {
            if (syncRoot == null)
                throw new ArgumentNullException("syncRoot");
    
            this.SyncRoot = syncRoot;
        }
    }
    

    LoyaltyPointBatchLogger

    public class LoyaltyPointBatchLogger : BatchLoggerBase
    {
        private static readonly object LOYALTY_SYNC_ROOT = new object();
    
        public LoyaltyPointBatchLogger()
            : base(LOYALTY_SYNC_ROOT)
        {
    
        }
    }
    

    TransactionBatchLogger

    public class TransactionBatchLogger : BatchLoggerBase
    {
        private static readonly object TRANSACTION_SYNC_ROOT = new object();
    
        public TransactionBatchLogger()
            : base(TRANSACTION_SYNC_ROOT)
        {
    
        }
    }
    

    EDIT: Note that this still means subclasses can ignore your intent. For example:

    public class EvilBatchLogger : BatchLoggerBase
    {
        public EvilBatchLogger()
            : base(new object())
        {
    
        }
    }
    
    var evil1 = new EvilBatchLogger();
    var evil2 = new EvilBatchLogger();
    

    In this case, evil and evil2 will not share the same locking object and can interfere. But if you have control over the logger implementations, you can avoid shooting yourself in the foot.

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

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have a text box which get data for price. If someone enter something like
I have a chicken-egg problem. I would like too implement a system in PHP
I have the following code and want to use it as an object. How
have a php code like this,going to convert it in to C#. function isValid($n){
Have a look at this HTML code: <div class=overlay> <a href=#>1</a> <a href=#>1</a> <a
I have a class to whose every instance i create a new logger and
I have a struct like this: [StructLayout(LayoutKind.Sequential)] internal struct EVENT_TRACE_PROPERTIES { internal WNODE_HEADER WNode;
Have a look at the menu link Produkter on http://marckmann.se/ I would like to

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.