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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:48:30+00:00 2026-06-13T06:48:30+00:00

public static class LogWriter { private static ReaderWriterLockSlim writeLock = new ReaderWriterLockSlim(); public static

  • 0
public static class LogWriter
{
    private static ReaderWriterLockSlim writeLock = new ReaderWriterLockSlim();
    public static void WriteExceptionLog(string content)
    {
        #if DEBUG
        MessageBox.Show(content);
        #endif
        WriteLog(content, Constant.EXCEPTION_LOG_PATH);
    }
    public static void WriteLog(string content, string path)
    {
        try
        {
            writeLock.EnterWriteLock();
            string directory = Path.GetDirectoryName(path);
            if (!Directory.Exists(Path.GetDirectoryName(directory)))
                Directory.CreateDirectory(directory);
            using (StreamWriter writeFile = new StreamWriter(path, true))
            {
                content = DateTime.Now + " : " + content;
                writeFile.WriteLine(content);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            writeLock.ExitWriteLock();
        }
    }
}

I have a class that writes logs. Because I am writing to a log asynchronously, I need to put a lock and release it when write is done but this seems to be somewhat of a clunky solution and maybe even bad for performance.

What is a better way to handle this?

  • 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-13T06:48:31+00:00Added an answer on June 13, 2026 at 6:48 am

    For performance reason and also to avoid a very different behavior between log-ON and log-OFF, I suggest to run one buffered log file per thread.

    • One per thread to avoid locking: no contention
    • Buffered to avoid disk latency

    The counterparts are:

    • a merging tools based on time (milliseconds) is needed to see application activity as a whole (vs thread activity)
    • buffering may hide last log records in case of brutal termination

    To go one step more to real-time, you have to log in memory, and to develop a dedicated interface to extract log on request but this kind of log is generally reserved to hard real-time embedded application.

    Other solution for safe logging with low CPU consumption (low level C programming):

    • place a log record buffer into shared memory
    • the observed process act as log record producer
    • create a log manager process, with higher priority, which acts as log record consumer
    • manage the communication between consumer and producer behind a flip/flop mechanism: a pointer assignment under critical section.

    If the observed process crash, no log record will be lost since shared memory segment is attached to log manager process.

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

Sidebar

Related Questions

public static class TestTask extends AsyncTask<Void, Integer, Integer> { private String stacktrace; public TestTask
The class looks as follows: public static class CacheManager { private static Dictionary<string, object>
class LogWriter implements Runnable { Socket client; private static ThreadLocal<Date> date = new ThreadLocal<Date>()
public class Main { public static void main(String[] args){ System.out.println(X.Y.Z); } } class X
abstract class MyClass{ static class StaticClass{ public void showAdd(){ System.out.println( show add); } }
I have this: public static class Domain { private const string sessionKey = NHib.SessionKey;
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have public static class A { public static string ConnString; } [Serializable] public
I've got the following class: public static class Pages { public static string LoggedOut
public static class EmptyOrNullHelper public static string EmptyOrNull(this HtmlHelper helper, IQueryable(T) type) { //Code

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.