Hi all,
I have several threads running (basically they are copying files from one place to another). I am using log4net for logging...Howev er, sometimes i get a log4net error due to log file being used by other process. I thought about creating a method which will take care of the logging...all threads will used a delegate to call the method that does the logging
static readonly object lockerLog = new object();
public delegate void UpdateLogDelega te(string txt,string type)
My question is: as the code stands right now, I thread might have acquired the lock on the call to the logging, but a second thread might have come along and modified the method parameters "txt" and "type", right? Is there a way of locking the entire method?
I think that another option could be to create a class to hold the txt and type and pass the object to the method
Or just locking the logger
thus when I called it from another class I do:
Anyone knows what will be the proper way of doing this, if ANY of the ones I have mentioned? I am new to threading and feel a bit confused
Cheers
I have several threads running (basically they are copying files from one place to another). I am using log4net for logging...Howev er, sometimes i get a log4net error due to log file being used by other process. I thought about creating a method which will take care of the logging...all threads will used a delegate to call the method that does the logging
static readonly object lockerLog = new object();
public delegate void UpdateLogDelega te(string txt,string type)
Code:
public void LogInfo(string txt, string type)
{
lock (lockerLog)
{
switch (type)
{
case "ERROR":
Logging.log.Error(txt);
break;
case "DEBUG":
Logging.log.Debug(txt);
break;
}
}
}
I think that another option could be to create a class to hold the txt and type and pass the object to the method
Code:
public void LogInfo(LogText lt)
{
lock (lt)
{
switch (lt.type)
{
case "ERROR":
Logging.log.Error(lt.txt);
break;
case "DEBUG":
Logging.log.Debug(lt.txt);
break;
}
}
}
Code:
class Logging
{
public static readonly ILog log = LogManager.GetLogger(typeof(Logging));
Logging() {}
public string Initialize()
{
.....
}
}
Code:
lock(Logging.log)
{
Logging.log.Info("hopefully there is a lock in the log right now ...");
}
Cheers
Comment