Quantcast
Channel: User user2023861 - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 35

Answer by user2023861 for Implementing a thread safe log class with simple functionality

$
0
0

A comment about your Log(string) method. The lock keyword is just syntactic sugar for a call to the Monitor class. See here. Here is what lock becomes:

bool lockWasTaken = false;var temp = obj;try {     Monitor.Enter(temp, ref lockWasTaken);     {         //body     } }finally {     if (lockWasTaken) Monitor.Exit(temp); }

So your Log(string) method is actually this:

public static void Log(string logMessage){    try    {        bool lockWasTaken = false;        var temp = _syncObject;        try        {            Monitor.Enter(temp, ref lockWasTaken);             {                _buffer.Add(logMessage);                Save(_buffer);            }        }        finally         {             if (lockWasTaken) Monitor.Exit(temp);         }    }    catch (Exception ex)    {        throw;    }}

Did you put in your catch and throw statement because you were worried that a possible exception will get eaten somewhere? You can see from this that that's not the case.


Viewing all articles
Browse latest Browse all 35

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>