J
Jeff0803
Guest
I made a class which have common methods and declared locker object, Instance method and WriteLog method like following.
public class CDMrCommon
{
private readonly object locker = new object();
public string exePath;
public string applicationpath;
public void GetConfig()
{
exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
applicationpath = System.IO.Path.GetDirectoryName(exePath);
}
public static CDMrCommon Instance
{
get{
if (m_CDMrCommon != null){
return m_CDMrCommon;
}
else{
m_CDMrCommon = new CDMrCommon();
return m_CDMrCommon;
}
}
}
public void WriteLog(string strlog)
{
lock (locker)
{
try
{
using (FileStream file = new FileStream(applicationpath + @"\Log\" + DateTime.Now.Date.ToString("MMddyyyy") + ".log", FileMode.Append, FileAccess.Write, FileShare.Read))
using (StreamWriter w = new StreamWriter(file, Encoding.Unicode))
{
w.WriteLine(DateTime.Now.ToLongTimeString() + "\t" + strlog);
}
}
catch
{
}
}
}
}
Two EXEs reference this DLL and call WriteLog() method to write any log like this way.
GetConfig()//called at the begining of the process
..
CDMrCommon.Instance.WriteLog("Some log");
It works but I'm not sure if "private readonly object locker = new object();" is correct or not.
Should it be static to ensure two different EXEs call the WriteLog() method.
Can anybody give me some advice?
Continue reading...
public class CDMrCommon
{
private readonly object locker = new object();
public string exePath;
public string applicationpath;
public void GetConfig()
{
exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
applicationpath = System.IO.Path.GetDirectoryName(exePath);
}
public static CDMrCommon Instance
{
get{
if (m_CDMrCommon != null){
return m_CDMrCommon;
}
else{
m_CDMrCommon = new CDMrCommon();
return m_CDMrCommon;
}
}
}
public void WriteLog(string strlog)
{
lock (locker)
{
try
{
using (FileStream file = new FileStream(applicationpath + @"\Log\" + DateTime.Now.Date.ToString("MMddyyyy") + ".log", FileMode.Append, FileAccess.Write, FileShare.Read))
using (StreamWriter w = new StreamWriter(file, Encoding.Unicode))
{
w.WriteLine(DateTime.Now.ToLongTimeString() + "\t" + strlog);
}
}
catch
{
}
}
}
}
Two EXEs reference this DLL and call WriteLog() method to write any log like this way.
GetConfig()//called at the begining of the process
..
CDMrCommon.Instance.WriteLog("Some log");
It works but I'm not sure if "private readonly object locker = new object();" is correct or not.
Should it be static to ensure two different EXEs call the WriteLog() method.
Can anybody give me some advice?
Continue reading...