Why do we need singleton class to store the log information in the log file using C#?

  • Thread starter Thread starter aditya satya prakash
  • Start date Start date
A

aditya satya prakash

Guest
Hello All,

I am new to the C# programming language. And in my C# project Logger class was created to to log the exceptions and to store in the log file. Below is the Logger Class.

EMRLogger.cs file

public class EMRLogger
{
private static volatile EMRLogger objLogger;
private static object syncRoot = new object();
private static RegistryHandler regHandler = null;

private const string LOGFILE = "PCPELog.txt";

public static EMRLogger Instance
{
get
{
if (objLogger == null)
{
lock (syncRoot)
{
if (objLogger == null)
objLogger = new EMRLogger();
}
}

return objLogger;
}
}

While creating an instance from objLogger = new EMRLogger(); in the above function, it is calling the constructor like as shown below:

private EMRLogger()
{
regHandler = RegistryHandler.GetRegistryHandlerInstance();
CheckForCleanup();
}

From this EMRLogger constructor, we are calling CheckForCleanup() function like as shown below:

private static void CheckForCleanup()
{
try
{
RegistryKey regKey = regHandler.GetBaseRegKeyHandle();
string fileNamePath = Convert.ToString(regHandler.GetRegKeyValue(Helper.REGKEY_INSTALL_PATH, regKey));
fileNamePath += LOGFILE;

if( File.Exists(fileNamePath) )
{

FileInfo objFileInfo = new FileInfo(fileNamePath);
long fileSize = objFileInfo.Length;
if (fileSize > Helper.EMR_LOG_MAX_SIZE_1_MB) // If filesize is more than 1MB, truncate the contents of the logfile.
{
using (FileStream logStream = new FileStream(fileNamePath, FileMode.Truncate))
{
if (logStream != null)
{
StreamWriter sw = new StreamWriter(logStream);
sw.Close();
}
}
}

}
}
catch (IOException ioE)
{
throw ioE;
}
catch (Exception ge)
{
throw ge;
}
}

}


And also from above EMRLogger constructor, we are calling GetRegistryHandlerInstance() function that is there in the class RegistryHandler. And this RegistryHandler class is also a Singleton class.


}

RegistryHandler.cs file:

class RegistryHandler
{
bool regWritable = false;
String BASE_REG_KEY = string.Empty;
private static RegistryHandler registryHandlerObj = null;

//singleton class
private RegistryHandler()
{
BASE_REG_KEY = "SOFTWARE\\EMR\\4.00.00";
}

public static RegistryHandler GetRegistryHandlerInstance()
{
if (registryHandlerObj == null)
{
registryHandlerObj = new RegistryHandler();
}
return registryHandlerObj;
}
}

Please help me to understand this code snippet as I am confusing with the above code snippet?

1. Why do we need singleton class (class EMRLogger) to store the log information?

2. Why do we need singleton class (class RegistryHandler) while Initializing Registry key in the above class?

3. Can we call another singleton class from the Signleton class constructor?

4. Why to use volatile when creating singleton class object?

private static volatile EMRLogger objLogger;

5. Here when implementing the Singleton class it was used thread-safety using double-check locking, What is the advantage of this over other ways?

Thanks & Regards
Y A S Prakash

Continue reading...
 
Back
Top